1834458 Members
2925 Online
110067 Solutions
New Discussion

Re: Scripting $? issue

 
SOLVED
Go to solution
Chamitha Wijesekera
Frequent Advisor

Scripting $? issue

Hi, I have a shell script I execute on the bourne shell (script 1). It in turn calls a function of another shell script (script 2). script 2 does its processing and the net result of it is the $? being set to 0. A check immidiately before script 2 exits confirms that $? is indeed set to 0.
However a check within script 1 of $? immidiately after script 2 returns indicates that $? is equal to 1! No processing takes places between the last statement of script 2 and the statement in script 1 immidiately following the call of script 2! How can this be?
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting $? issue

One thing that can mislead you is that the value of $? id constantly changing. e.g echo itself might set it. The best way to do this is to do something like this:
XX=`some_cmd`
STAT=$?

you then test the value of ${STAT} rather than $?.

I would make sure that script2 exits with a value - this becomes the exit status that script1 sees (or if a function, returns with a value) so that you are positive that no values are 'floating'.
If it ain't broke, I can fix that.
Jordan Bean
Honored Contributor

Re: Scripting $? issue


Is script 2 explicitly terminating by exit 0, or at EOF where $? is that of the last full statement?

Sure the last status in the script may be 0, but what is happening when the script actually terminates? How was it invoked by script 1?

Can we see the appropriate code snippits?

Chamitha Wijesekera
Frequent Advisor

Re: Scripting $? issue

Hi, I have attached some code snipets below and will try to explain in more detail whats going on!

The main script (script 1) has the following statements,

rc=0
FileCreate
rc=$?
echo $rc

"FileCreate" is a function in another script (script 2) listed below.

FileCreate() {
VgnFileCreate $ARG1 $ARG2
rc=$?
echo $rc
return rc
}

The function VgnFileCreate successfully executes and the assignment rc=$? evaluates to 0 which is displayed by the echo statement.

However when the value of rc is echoed in script 1 after the FileCreate function of script 2 has returned, the value is different (say for example 1)

A point worth noting is that when I remove the return rc statement from the FileCreate function of script 2, rc evaluates correctly (to the value echoed from the function) within script 1! Any ideas on the reason for this anomaly?
Chamitha Wijesekera
Frequent Advisor

Re: Scripting $? issue

By the way both scripts are executed using "sh"
Ceesjan van Hattum
Esteemed Contributor
Solution

Re: Scripting $? issue

Why you say "return rc" within the function and not "return $rc".. or is it a typo..?

Ceesjan
Chamitha Wijesekera
Frequent Advisor

Re: Scripting $? issue

No its not a typo, but I did try including return $rc and it seemed to work! I'm still in the process of verifying it though. Fingers crossed! However the script has been used on other platforms such as Win2k, AIX and Solaris.
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting $? issue

If you were doing a return rc when you meant return $rc, it was working by accident regardless of the platform.
If it ain't broke, I can fix that.
Bill Hassell
Honored Contributor

Re: Scripting $? issue

Just a note: return rc will return the string "rc" and not the contents of the variable rc which is triggered by $rc. Another useful tip for understanding shell scripts is to enable tracing:

set -x

You'll need to add set -x inside each function too.


Bill Hassell, sysadmin
Chamitha Wijesekera
Frequent Advisor

Re: Scripting $? issue

Ya I think its been the missing '$' all along. Thanks for the help guys!