Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I'm trying to create a shell script that creates other shell scripts. For example I have the following code within my main script Code: #Make script to run DFC ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    5

    Creating script within a script

    Hello,

    I'm trying to create a shell script that creates other shell scripts. For example I have the following code within my main script
    Code:
    #Make script to run DFC
    cat > run_dfc.sh << EOS
    #!/bin/bash
    ## ------------------------------------------------------------
    # User defined functions
    # ------------------------------------------------------------
    usage () {
            echo
            echo "Usage: `basename $0` <numClusters>"
            echo
    }
    
    # ------------------------------------------------------------
    # Check command-line arguments
    # ------------------------------------------------------------
    if [ $# != 3 ]; then
            usage
            exit 1
    fi
    
    C=$3
    L=$1
    N=$2
    
    ./dfc_lac --nproc $num_processors --stats $stats $filename $C
    EOS
    chmod +x run_dfc.sh
    the problem is certain variables, such as those like $#,$1,$2, etc. are replaced with the values of those in the main script. Is there a way I can actually have the exact phrase "$#" or "$1" in my second script.

    for example, when I actually run my main script, the sub-script that gets created is
    Code:
    #!/bin/bash
    ## ------------------------------------------------------------
    # User defined functions
    # ------------------------------------------------------------
    usage () {
            echo
            echo "Usage: runOnce.sh <numClusters>"
            echo
    }
    
    # ------------------------------------------------------------
    # Check command-line arguments
    # ------------------------------------------------------------
    if [ 5 != 3 ]; then    <--------this should be "$# != 3"
            usage
            exit 1
    fi
    
    C=10000   <--------this should be "C=$3"
    L=CS_002  
    N=2            
    
    ./dfc_lac --nproc 20 --stats 6 CS_002_10000_rev0
    thanks in advance for the help

  2. #2
    Just Joined!
    Join Date
    Aug 2007
    Posts
    5
    Found the solution:

    Code:
    #Make script to run DFC
    cat > run_dfc.sh << EOS
    #!/bin/bash
    ## ------------------------------------------------------------
    # User defined functions
    # ------------------------------------------------------------
    usage () {
            echo
            echo "Usage: `basename $0` <numClusters>"
            echo
    }
    
    # ------------------------------------------------------------
    # Check command-line arguments
    # ------------------------------------------------------------
    if [ \$# != 3 ]; then
            usage
            exit 1
    fi
    
    C=\$3
    L=\$1
    N=\$2
    
    ./dfc_lac --nproc $num_processors --stats $stats $filename $C
    EOS
    chmod +x run_dfc.sh

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    I'd use echo to generate the file, not cat and then you can use single-quoting to hide all the variables without having to do every one individually
    Code:
    echo >run_dfc.sh '#!/bin/bash
    stuff
    more stuff
    echo $HOME
    echo $PWD
    last stuff'
    Saves exec-ing a process, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...