Results 1 to 6 of 6
Hello,
I run a executable and then use the stream editor on the output before redirecting it into a file. The $? appears to have the completion/exit code of the ...
- 03-03-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
Multiple Commands ( |'ed) and $?
Hello,
I run a executable and then use the stream editor on the output before redirecting it into a file. The $? appears to have the completion/exit code of the stream editor and not the executable. Is there any way to get the exit code of the first command?
Example:
executable this this etc parameters | sed -e 'whatever' > file.out.somefile
$? returns 0 unless sed fails. What to use to check the return code of the executable?
Thanks.
- 03-03-2010 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
A few ways:
The man pages, Google, and Wikipedia are your friends ... cheers, drlCode:PIPESTATUS An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command). ... The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit success- fully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value. -- excerpts form man bashWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-03-2010 #3Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
Using standard re-direction methods
Don't use this...
SORRY ... I failed here was the "stdout" bypassed the second command.
Code:#!/bin/bash # # make file descriptor 3 goto wherever stdout is going... exec 3>&1 # make file descriptor 4 goto wherever stderr is going... exec 4>&1 # Run whatever but: # reroute stdout through file descriptor 3 # reroute stderr through file descriptor 4 # Then echo the status into file descriptor 5 # After the second command out the status saved in file descriptor 5 to # output. status=`((first_cmd 2>&4 1>&3 3>&- 4>&- 5>&-; echo $? >&5) | second_cmd) 5>&1` exit $status
- 03-03-2010 #4Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
SORRY ... I failed here was the "stdout" bypassed the second command.
- 03-04-2010 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
If the object is to avoid the "sed" command if the first "executable" fails, you really can't get there from here. Failure or success of the first part of the pipeline is not determined until it finishes, and there is an indefinite amount of data in the pipe, some of which will have already been processed by "sed".
- 03-04-2010 #6
While $PIPESTATUS seems to be the really general purpose solution, you might also want to consider joining the commands with the ``&&'' operator. I am not sure what you are trying to achieve, but if you are just interested in whether the ``executable'' failed, using ``&&'' would be simpler.


Reply With Quote
