Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting variables outside of parenthesis.

Status
Not open for further replies.

eldarion99

Programmer
Mar 4, 2003
6
US
I have inherited this wierd code, in which I execute a child script. I'm trying to pass back the return code, as follows:

#/bin/ksh
bunch of initialization stuff
integer saverc

(date
...
child_script.sh
saverc=$?
echo "saverc is $saverc" #produces saverc is 3
if [ $saverc -ne 0 ]
then
exit $saverc # this exits the parenthesis with a 3
fi


date
) 2>&1 > $LOGFILE

echo "saverc is $saverc" #produces saverc is 0

I've tried using an export of saverc both at the very top, and at the top of the parenthesis. Anybody know what I'm doing wrong? All responses are appreciated very much!!

Thanks.
 
Well () create a subshell of sorts, with basically a self-contained environment.

After this line
Code:
) 2>&1 > $LOGFILE

Put
Code:
saverc=$?

To re-save the exit status of the () enclosed subshell in your variable.

Here's a short example
Code:
#!/bin/ksh

(
  echo sub
  exit 2 # exit status from the () subshell
)
echo $?
 
That worked. Sometimes the answer is SO OBVIOUS you can't see it. Thank you very much.

Summary for anyone else who has this problem:

( ) - this is a "command grouping", in which all commands executed within the parenthesis are executed in a subshell. If you hit and exit stmt in the 3rd line out of 100 in the subshell, the end of the paren's will be reached at which point you can query the $? value to preserve your return code in the calling shell.

Thanks very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top