相关疑难解决方法(0)

如何在 korn shell 中捕获返回状态并同时使用 tee?

考虑源代码:

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 呢?

ksh shell-script

10
推荐指数
1
解决办法
2万
查看次数

How to detect an error using process substitution

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 …

bash pipe process-substitution exit-status

8
推荐指数
2
解决办法
2249
查看次数