Operating System - HP-UX
1829144 Members
2033 Online
109986 Solutions
New Discussion

Return Codes -Shell Scripts -HP-UX

 
Vince Idone
New Member

Return Codes -Shell Scripts -HP-UX

I'm using HP-UX ver. B.10.20 E 9000/889

Has anyone run across this? I have a scriptA
which calls scriptB. If scriptB returns a status code of 0, no problem, execution continues with subsequent statements in scriptA.

However, if scriptB returns abnormal termination (anything other than zero, eg even exiting with "exit 1"), everything stops, subsequent statements in scriptB are
not executed.

What I want to do in scriptA is to check the
status code returned, and execute appropriate
logic.

I'm wondering, does a profile parameter need to be changed? Shell problem? HP-UX bug?

Thanx in advance to whoever can help.
Vince Idone
4 REPLIES 4
melvyn burnard
Honored Contributor

Re: Return Codes -Shell Scripts -HP-UX

How you do this is going to depend on how the script B works.
Basically, when B has finished, and returns control to A, you can check the return value from B, by doing a test on the value supplied by $?.
The problem is that you may want to have differing return values, and the best way to do this is to change script B to test at various stages, and depending on whether that stage has been succesfull, set the exit code to a value for that specific stage;
eg:
A calls B
B starts to run, does abc, test if ok, if yes continue, else exit 2
assming ok, next stage in B, test for correct result, ok then continue, or if not exit 3.
In A you would then have something like a case statement that tests $?, and does:

value 2) means erro x
value 3) means error y
and so on.

I hope this helps.
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
Paul Hite
Trusted Contributor

Re: Return Codes -Shell Scripts -HP-UX

This behavior results from setting the -e option when the shell is invoked. All shells have a -e option and most also allow you to do a "set -e" to turn on this benavior during execution. So you must be doing a

set -e

somewhere or you invoked the shell like:

#! /usr/bin/ksh -e
Emmanuel Eyer
Frequent Advisor

Re: Return Codes -Shell Scripts -HP-UX

Dummy idea: aren't you sourcing sciptB?

I got following problem a couple of days ago: scriptA sources scriptB (scriptA contains a line such as . scriptB). In scriptB if an error is detected, I used "exit 1" statements.

Bad idea, since "exit" ends the current process... and sourcing a script uses the SAME process for BOTH scripts, thus scriptB stops scriptA. (More funny: if you source scriptB from a remote session, it even closes the remote session!)

Since I replaced all "exit" statements by "return" statements, everything runs fine.

Hope it helps,
Emmanuel
Vince Idone
New Member

Re: Return Codes -Shell Scripts -HP-UX

I have managed to resolve my own problem with a little help from Unix books and peers where I work. For anyone else who may run into this, one solution is call a shell script from within an if statement rather than just calling it. For eg:

if seq_check.scrpt seff50 seff50.dat
then
echo "seq check was successful." else
echo "sequence check Failed!"
fi