[SunHELP] script help test on return value $?

Will Mc Donald wmcdonald at ntlworld.com
Wed Jul 10 08:37:00 CDT 2002


You mean in perl or in sh? Ah, I see you mean in kshell...

http://quong.best.vwh.net/shellin20/#LtohTOCentry-18

The special variable $? contains the exit status of the last command run,
however you should rarely have to access this variable. Evaluate the command
in the if/while directly. The following example shows how to process a file
$fx if it contains a java class.


  # poor, too wordy
grep -c ~class $fx > /dev/null
if [ $? = 0 ]; then
  process $fx
fi

  # much better, directly run grep
if grep -c class $fx > /dev/null; then
  process $fx
fi

Presumably != will negate the test. Have a play around.

Will.

----- Original Message -----
From: "Simon Jespersen" <shj at pine.dk>
To: "Sun solaris help list" <sunhelp at sunhelp.org>
Sent: Wednesday, July 10, 2002 2:28 PM
Subject: [SunHELP] script help test on return value $?


> I need a little script help
> I want to test on the return value from another program
> #!/bin/ksh
> #Running getstate.pl
> getstate.pl
>
> echo $? #the output is 1 but how do i test on this. I have programmet the
> perl scritp so i returns 0 if outcome is diffrent than inspected and it
> works.
>
>
> if [ $? -eq 1 ] ; then    /this doesnt seem to work
>     echo do something
> fi



More information about the SunHELP mailing list