Return value of redirected bash command -
this question has answer here:
- how check exit if used tee? 3 answers
the following script tries create directory , fails because temp1 doesn't exist. want redirect output "tee." problem "$?" catches return value of "tee" succeeds , not value of "mkdir" fails. how can use "tee" check return value of "mkdir"
file name: ./test.sh
#!/bin/bash mkdir temp1/temp2 | tee test_output.txt if [ "$?" != 0 ]; echo "command failed" else echo "command successed" fi ./test.sh
mkdir: cannot create directory `temp1/temp2': no such file or directory
command successed
since stated bash, can use pipestatus. man page:
pipestatus
array variable (see arrays below) containing list of exit status values processes in most-recently-executed foreground pipeline (which may contain single command).
for example:
#!/bin/bash mkdir temp1/temp2 | tee test_output.txt if [ "${pipestatus[0]}" != 0 ]; echo "command failed" else echo "command succeeded" fi
Comments
Post a Comment