1752805 Members
5604 Online
108789 Solutions
New Discussion юеВ

return code

 
SOLVED
Go to solution
Derek Kuzdub
Occasional Contributor

return code

I run the following script
which executes the command and prints it.
Is there a way to capture proper return code from the command.
It awlays returns RC=0.

LST=cmd.lst; >$LST
RC=0
runcmd()
{
echo "`date '+%H:%M:%S '`\c" |tee -a $LST
echo "\n$CMD \n$(eval $CMD) RC=$?" |tee -a $LST
};
CMD="grep -il admm /etc/passwd*"
runcmd
echo $RC
if [ "$RC" != 0 ]; then
echo "Error - $RC " |tee -a $LST
fi

5 REPLIES 5
melvyn burnard
Honored Contributor

Re: return code

the return code for any command is obtained by examining the variable ?
echo $?
or
if [ $? !-eq 0 ]
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
harry d brown jr
Honored Contributor

Re: return code

I just ran it and I got this:

# ./tmp/dah
16:00:34
grep -il admm /etc/passwd*
RC=1
0
#


???

live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: return code

The command

echo "\n$CMD \n$(eval $CMD) RC=$?" |tee -a $LST


is not setting RC to $?, it's just echo'ing "RC="$?

Is that what you want it to do?

live free or die
harry
Live Free or Die
Derek Kuzdub
Occasional Contributor

Re: return code

Yes
I was not clear perhaps
I want to set RC so I can examine it later
harry d brown jr
Honored Contributor
Solution

Re: return code

Make these changes:

echo "\n$CMD " |tee -a $LST
whatgoes=$(eval $CMD)
RC=$?
echo $whatgoes RC=$RC |tee -a $LST


If you do it after the command is executed and you "tee" the output, then $? will be equal to the return code of the "tee", which is 99.999% always 0.


live free or die
harry
Live Free or Die