HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   [SHELL] script in script (http://www.howtoforge.com/forums/showthread.php?t=14563)

mopsos 7th August 2007 17:04

[SHELL] script in script
 
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.

mopsos 7th August 2007 17:49

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?

geekman 8th August 2007 11:32

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.

falko 8th August 2007 13:42

Quote:

Originally Posted by mopsos
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'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



All times are GMT +2. The time now is 22:13.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.