[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Re: Bash true/false builtings undocumented? "false" not working?



> Is there a "cleaner" way to test the true/ error exit status other
> than using "$?", with bonus points for working in posix sh as well as
> Bash, ?

test $? -eq 0

"help test" will tell you more.


Another point from your original mail,

> $ false
> $ test $? && echo ok || echo error $?

The second $? in that line is different from the first; it's the exit
status of "test". That can be demonstrated, with a corrected test
command, by

$ grep stuff /no/such/file
grep: /no/such/file: No such file or directory
$ echo $?
2

$ grep stuff /no/such/file
grep: /no/such/file: No such file or directory
$ test $? -eq 0 && echo ok || echo error $?
error 1

As was mentioned in an earlier reply, you can get round that by saving
the exit status in a variable, e.g.

$ grep stuff /no/such/file
grep: /no/such/file: No such file or directory
$ RET=$?
$ test $RET -eq 0 && echo ok || echo error $RET
error 2


-- 
Cheers,
Clive


Reply to: