Operating System - HP-UX
1748010 Members
4984 Online
108757 Solutions
New Discussion юеВ

Re: How to get the correct exit status of a shell

 
Suman_7
Frequent Advisor

How to get the correct exit status of a shell

I am trying to execute a shell script a.ksh which returns an exit status of 1 since it fails.

I am calling this script a.ksh in another script b.ksh eg.

#script b.ksh
a.ksh

if [ $? -ne 0 ]; then
echo failed
else echo success
fi

My problem is even through a.ksh fails when I call in my my script b.ksh I get $?=0 ( success ).

How do I get the correct exit status?

Thanks
10 REPLIES 10
Mel Burslan
Honored Contributor

Re: How to get the correct exit status of a shell

I am not sure if this can solve your problem but instead of following your a.ksh with an 'if' statement, which eventually will complete successfully and set $? to 0, use something like this structure :

a.ksh
result=`echo $?`

if [ $result -ne 0 ]
then
echo failed
else
echo success
fi

hope this helps
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor

Re: How to get the correct exit status of a shell

First of all, your a.ksh needs to explicitly exit with a value.

e.g.

exit ${STATUS}

Secondly, you need to immediately capture ${?}. It's best to assign it to a variable because the next shell command will alter ${?}.

a.ksh
STAT=${?}
date # just some dummy commands to illustrate
ls # my point

if [ ${STAT} -eq 0 ]
then
echo "Ok"
else
echo "a.ksh failed; status ${STAT}"
fi
If it ain't broke, I can fix that.
Suman_7
Frequent Advisor

Re: How to get the correct exit status of a shell

Thanks a Lot!! That worked like magic!!

Can you please also tell me in UNIX how do I comment multiple lines.

Thanks
Bharat Katkar
Honored Contributor

Re: How to get the correct exit status of a shell

Use HASH # to comment the line in the begining.
You need to know a lot to actually know how little you know
Suman_7
Frequent Advisor

Re: How to get the correct exit status of a shell

Yes but I need to comment some 30 lines, Is they any way in UNIX I can commnet line 1.. 30 without commenting all the 30 lines using #.

Thanks
Mel Burslan
Honored Contributor

Re: How to get the correct exit status of a shell

open up your file in vi
go to the command mode by hitting esc
type the following command

:1,30s/^/#/

then hit enter
you will see # marks appear as the first character in first 30 lines of your file

don't forget to save your file before exiting
________________________________
UNIX because I majored in cryptology...
Bharat Katkar
Honored Contributor

Re: How to get the correct exit status of a shell

No,it is not.
Uou have to use # in the begining for every line.
You need to know a lot to actually know how little you know
Gary L. Paveza, Jr.
Trusted Contributor

Re: How to get the correct exit status of a shell

Not really a method of "commenting" but you could surround the code to be commented with the following:

while false
do

done

Or any of a various things like this.
Mike Stroyan
Honored Contributor

Re: How to get the correct exit status of a shell

You could put the lines in a 'here-document' to nowhere. Normally a 'here-document' is used to provide input to the stdin of a command. It starts with <
# one line comment
echo start
<<'*/'
a really
long
multiple line
comment
*/
echo finish