Operating System - HP-UX
1828316 Members
3647 Online
109976 Solutions
New Discussion

Pass Variables to the second script

 
SOLVED
Go to solution
Stephen Ng
Occasional Advisor

Pass Variables to the second script

I would like to write a script that will execute another script by passing variable as parameter. e.g.
usage s1 a b

#s1
echo $1 $2
v=$1
s2 $v $2

#s2
echo $1 $2

but the result is null.

Rgds,
Stephen
3 REPLIES 3
Michael Tully
Honored Contributor
Solution

Re: Pass Variables to the second script

When a process terminates i.e. script1, it passes back an 'exit status' to it's parent process. An exit status is actually a number between 0 and 255. The shell stores the last exit status it received in the $? metacharacter.
So adding say exit $? to your script should keep the 'exit status' for the next script.
Anyone for a Mutiny ?
Michael Tully
Honored Contributor

Re: Pass Variables to the second script

Here is a nice piece of code I borrowed.

XXX=`somecommand.sh`
STAT=$?

Now $STAT is the exit status of somecommand.sh while $XXX contains its output.

This also should be extended to functions within your scripts.

somefunc()
{
FUNCSTAT=0
echo "This is somefunc output \c:"
while [ $# -ge 1 ]
do
echo "Arg ${1} \c"
shift
done
echo
return(${FUNCSTAT})
}

XXX=`somefunc 111 222`
STAT=$?

Here the same rules as above apply. Typically, you test the exit(return) status before ever doing anything with the script(function) output.


Anyone for a Mutiny ?
Stephen Ng
Occasional Advisor

Re: Pass Variables to the second script

Michael,
It works.
Tks,
Stephen