Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 405  / 1 Year ago, thu, december 29, 2022, 9:15:37

I run the following command in a bash script:



cd SomeDir && (sh -c "$CMD_STR") 2>&1 | some_filtering | sed -u -e ....


I.e. I run CMD_STR in SomeDir (actually CMD_STR is 'make' or 'make target'), do some filtering on CMD_STR's output and then replace some symbols by 'sed'.



I need to retrieve CMD_STR's status code in case CMD_STR fails to return it from my script. How can I do it?


More From » scripts

 Answers
6

The solution is to set 'pipefail' option before running the command.



http://www.gnu.org/software/bash/manual/bashref.html




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 successfully.




So now my script looks like the following:



set -o pipefail
cd SomeDir && (sh -c "$CMD_STR") 2>&1 | some_filtering | sed -u -e ....
exit $?

[#29111] Friday, December 30, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
icielight

Total Points: 158
Total Questions: 92
Total Answers: 93

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
icielight questions
;