考虑源代码:
1. 父.sh
#!/usr/bin/ksh
# No tee
ksh Child.sh;
exit_status=$?;
echo "Exit status: ${exit_status}"
# Using tee
ksh Child.sh | tee -a log.txt;
exit_status=$?;
echo "Exit status: ${exit_status}"
Run Code Online (Sandbox Code Playgroud)
2. 子.sh
#!/usr/bin/ksh
...
exit 1;
Run Code Online (Sandbox Code Playgroud)
输出:
Exit status: 1
Exit status: 0
Run Code Online (Sandbox Code Playgroud)
$exit_status
正在捕获 Child.sh 的退出状态,1
.$exit_status
正在捕获 tee 的退出状态,即0
.那么如何捕获退出状态并使用 tee 呢?
This question is similar to the following link, but focused on using the command line (bash shell).
Using a simple example, when doing the following command:
$ cat <(date); echo $?
Fri Jul 7 21:04:38 UTC 2017
0
Run Code Online (Sandbox Code Playgroud)
The exit value is 0 as expected.
In the following command there is an error introduced on purpose, but the return value is still 0:
$ cat <(datE); echo $?
bash: datE: command not found...
Similar command is: 'date'
0
Run Code Online (Sandbox Code Playgroud)
Is …