In bash how do I exit a script from a function that is piped by tee?

╄→гoц情女王★ 提交于 2020-01-03 15:33:07

问题


I'm trying to understand why whenever I'm using function 2>&1 | tee -a $LOG tee creates a subshell in function that can't be exited by simple exit 1 (and if I'm not using tee it works fine). Below the example:

#!/bin/bash
LOG=/root/log.log

function first()
{
echo "Function 1 - I WANT to see this."
exit 1
}

function second()
{
echo "Function 2 - I DON'T WANT to see this."
exit 1
}
first 2>&1 | tee -a $LOG
second 2>&1 | tee -a $LOG

Output:

[root@linuxbox ~]# ./1.sh  
Function 1 - I WANT to see this.
Function 2 - I DON'T WANT to see this.

So. if I remove | tee -a $LOG part, it's gonna work as expected (script will be exited in the first function).

Can you, please, explain how to overcome this and exit properly in the function while being able to tee output?


回答1:


If you create a pipeline, the function is run in a subshell, and if you exit from a subshell, only the subshell will be affected, not the parent shell.

printPid(){ echo $BASHPID; }

printPid #some value
printPid #same value
printPid | tee #an implicit subshell -- different value
( printPid ) #an explicit subshell -- also a different value

If, instead of aFunction | tee you do:

aFunction > >(tee)

it'll be essential the same, except aFunction won't run in a subshell, and thus will be able to affect the current environment (set variables, call exit, etc.).




回答2:


Use PIPESTATUS to retrieve the exit status of the first command in the pipeline.

first 2>&1 | tee -a $LOG; test ${PIPESTATUS[0]} -eq 0 || exit ${PIPESTATUS[0]}
second 2>&1 | tee -a $LOG; test ${PIPESTATUS[0]} -eq 0 || exit ${PIPESTATUS[0]}



回答3:


You can tell bash to fail if anything in the pipeline fails with set -e -o pipefail:

$ cat test.sh
#!/bin/bash
LOG=~/log.log

set -e -o pipefail

function first()
{
echo "Function 1 - I WANT to see this."
exit 1
}

function second()
{
echo "Function 2 - I DON'T WANT to see this."
exit 1
}
first 2>&1 | tee -a $LOG
second 2>&1 | tee -a $LOG
$ ./test.sh
Function 1 - I WANT to see this.


来源:https://stackoverflow.com/questions/34385862/in-bash-how-do-i-exit-a-script-from-a-function-that-is-piped-by-tee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!