1822544 Members
2883 Online
109642 Solutions
New Discussion юеВ

Correct exit status

 
SOLVED
Go to solution
panchpan
Regular Advisor

Correct exit status

I have a script say a.sh, which is calling b.sh and b is callign c.sh - Now if I put some validation in c.sh and then have to say 'exit' I am giving exit 1 , but looks like exit 1 is bringing the execution point in a.sh at the place I had called b.sh.

Could you please tell me , what command could I give -IF on some validation in c.sh I want to quit the execution completely. which means, the program must not execute further commands of any parent scripts also.

Please advice.
9 REPLIES 9
Steven Schweda
Honored Contributor
Solution

Re: Correct exit status

Things depend on what you mean by one script
"calling" another. If one script executes
another script in a subprocess, then, when
the subprocess exits, the first script
normally continues (although it could check
the exit status of the subprocess, and act
accordingly). For example:

td176> cat a1.sh
#!/bin/sh
echo 'Before.'
./b.sh
echo "After, status = $?."

td176> cat b.sh
#!/bin/sh
exit 1

td176> ./a1.sh
Before.
After, status = 1.
td176>

If one script includes (or "sources") the
second script as a script fragment, then any
"exit" in the script fragment is as good as
an "exit" in the first script. For example:

td176> cat a2.sh
#!/bin/sh
echo 'Before.'
. ./b.sh
echo "After, status = $?."

td176> ./a2.sh
Before.
td176>

This example runs the second script, but
tests its exit status:

td176> cat a3.sh
#!/bin/sh
echo 'Before.'
./b.sh
status=$?
if [ $status != 0 ]; then
exit $status
fi
echo "After, status = $?."

td176> ./a3.sh
Before.
td176>
Rajeev  Shukla
Honored Contributor

Re: Correct exit status

I dont think you can do that when you call the scripts from another scripts. The reason is that the logic and flow of the program is within the same shell but by having another script (shell) called will have control of that shell only.
The way to acheive what you require will be-
1. Write the whole program in one script
2. Validate the return code of c.sh in b.sh and then validate the return code of b.sh in a.sh and exit the program.

Did i make sence or confuse you

Cheers
Raj
Venkatesh BL
Honored Contributor

Re: Correct exit status

In your case, you have to check the exit status of the 'child' script and take appropriate action (may be 'exit') in the parent.
panchpan
Regular Advisor

Re: Correct exit status

Thank you Venkatesh and Steven, I believe your advices suits to my requirement. Let me share the code and share what I plan to modify after your suggestion, correct me if i am wrong. Actually, in main parent script if the particular command of .Copy gets failed, I dont want to run .EOD script and Just want to quit completely.

IPASMK2.Nightly(main parent shell):
$BIN1/IPASMK2.Copy -n pre-eod
$BIN1/IPASMK2.EOD
$BIN1/IPASMK2.Copy post-eod

$BIN1/IPASMK2.Copy:

$DLC/bin/probkup ipas $BACKUPPATH/ipasii.bkp -com >> $LOG
if [ $? -gt 0 ]
then
exit 1
fi

After Modification IPASMK2.Nightly:
$BIN1/IPASMK2.Copy -n pre-eod
if $?=1
then
exit 1
fi
$BIN1/IPASMK2.EOD
$BIN1/IPASMK2.Copy post-eod
if $?=1
then
exit 1
fi

Is it looking Okay now?
Venkatesh BL
Honored Contributor

Re: Correct exit status

Looks fine. May be you could add an 'echo' statement before 'exit' such that you can find out with child script actually failed.
Steven Schweda
Honored Contributor

Re: Correct exit status

> if $?=1
> then
> exit 1
> fi

Is "1" the only possible bad status value
from the script? Is "1" the only bad status
value you wish to return?

> status=$?
> if [ $status != 0 ]; then
> exit $status
> fi

This is longer and more complicated, but it
also stops on any non-zero status value, and
it returns that status value to the user, who
may care what it is.
Dennis Handly
Acclaimed Contributor

Re: Correct exit status

>if $?=1

This isn't valid syntax. The correct syntax is:
if [ $? -eq 1 ]; then

>Steven:
> if [ $status != 0 ]; then

While this may work, this is doing a string compare and not an integer compare. Instead do:
if [ $status -ne 0 ]; then
Steven Schweda
Honored Contributor

Re: Correct exit status

> Instead do:

Good point. (My shell script skills must be
oxidizing, like all my other skills.)
Dennis Handly
Acclaimed Contributor

Re: Correct exit status

>Steven: My shell script skills must be
oxidizing

You can of course go crazy and use C style expressions in a real shell:
if (( status != 0 )); then