[SHELL] script in script

Discussion in 'Programming/Scripts' started by mopsos, Aug 7, 2007.

  1. mopsos

    mopsos New Member

    Hi,

    I have a problem with the execution of a script in a script.
    I want recover a variable in a script and test this in a other .

    For exemple :
    A script : sc1.sh
    Code:
    #!/bin/sh
    var=3
    
    A script : sc2.sh
    Code:
    #!/bin/sh
    sh /folder/sc1.sh
    if [ var -eq 3 ]
    echo "goods"
    fi
    
    But this is bad.

    I try with export var , but it's not good too.
    Can you say me how can I do this?

    Thx.
     
  2. mopsos

    mopsos New Member

    I try this
    sc1.sh :
    Code:
    #!/bin/sh
    var=1
    sh /folder/sc2.sh $var
    
    sc2.sh :
    Code:
    #!/bin/sh
    echo $1
    
    But The system didn't accept a variable in the command sh only a "word" (
    ex: sh /folder/sc2.sh var .

    Where is the problem?
     
  3. geekman

    geekman New Member

    So if I understand you correctly, you want to pass one variable onto another script? Theres two ways that come to mind straight away:

    * Lets say the two scripts are in the same directory...
    Example 1
    ----------

    Script 1:

    Code:
    #!/bin/sh
    
    returnvar=1
    echo $returnvar
    
    Script 2:
    Code:
    #!/bin/sh
    
    # This should set $var to whatever script1 outputs.
    var=`./script1`
    
    echo Passed: $var
    
    Example 2:
    ----------

    Script 1:
    Code:
    #!/bin/sh
    
    returnvar=3
    #We are sending $returnvar as the first argument to script2
    ./script2 $returnvar
    
    Script 2:
    Code:
    #!/bin/sh
    
    echo Passed $1
    
    From my experience in a debian environment this would likely work, no idea what you are using, though none of this has been tested.

    Thanks, hope it helps.
     
  4. falko

    falko Super Moderator Howtoforge Staff

    I'd do it like this:

    sc1.sh:

    Code:
    var=3
    sc2.sh:

    Code:
    #!/bin/sh
    . /folder/sc1.sh
    if [ $var -eq 3 ]
    echo "goods"
    fi
    
     

Share This Page