You need to be careful. First operation on $? usually clears it, so your if will not work.
You would be better off using:
rc=$? echo $rc if [ $rc -ne 0 ] :
Other than that, it works great for me:
$ grep 1 /dev/null $ if [ $? -ne 0 ] > then > echo xx > fi xx $ grep 1 /dev/null $ echo $?; 1 $ if [ $? -ne 0 ] > then > echo yy > fi $ _
Note the lack of output in the latter. This is because echo pulled in the return value and overwritten it (because the echo was successful).
As an aside, you should tell us which UNIX and which ksh you are using. My working version is ksh93 under Ubuntu. Your mileage may vary if you use a smaller version.
It looks like from your update, your only problem now is the function call. This is most likely because you define it after using it. script:
grep 1 /dev/null rc=$? if [ $rc -ne 0 ] then failed $rc fi failed() { echo Return code was $1 }
gives:
qq.ksh[6]: failed: not found
a
failed() { echo Return code was $1 } grep 1 /dev/null rc=$? if [ $rc -ne 0 ] then failed $rc fi
produces
Return code was 1
source share