Reputation:
In bash
, from environment variable $PIPESTATUS
we can retrieve exit staus of piped command line like below.
$ echo "some string" | grep x | tee some.tmp ; echo ${PIPESTATUS[1]}
1
However if I am on ksh
I need to something like below to get exit status. I searched for it to get something like below.
$ getme=`((echo "some string" | grep x 3>&- 4>&- ; echo $? >&4) | tee some.tmp 1>&3 3>&- 4>&- ) 4>&1` ; echo $getme
1
$ getme=`((echo "some string " | grep me 3>&- 4>&- ; echo $? >&4) | tee some.tmp 1>&3 3>&- 4>&- ) 4>&1` ; echo $getme
0
Is there a simpler form to above one in ksh
to retrieve exit status in piped command line?. And how to interprit above line. I do know little on subshell & usage of 4 as descriptor.
Redirection part is little hart to interprit
Upvotes: 2
Views: 2763
Reputation: 10039
$?
does not suite ?
echo "some string" | grep x | tee some.tmp ; echo "Returncode: $?"
or you need specific error code for each pipe
you can also try using set
options
-e
Unless contained in a || or && command, or the commandfollowing an if while or until command or in the pipeline following !, if a command has a non-zero exit status, execute the ERR trap, if set, and exit. This mode is disabled while reading profiles.
-o pipefail
A pipeline will not complete until all components of the pipeline have completed, and the return value will be the value of the last non-zero command to fail or zero if no command has failed.
Upvotes: 2