我希望使用tee
将any command (first command)
的输出写入一个文件,但我希望从first command
而不是tee
命令获得返回代码.
Example 1: The following code will return 1 and it is expected:个
cp -unknown-args "hello"
echo "return code is $?"
---output---
cp: invalid option -- 'k'
Try 'cp --help' for more information.
return code is 1
Example 2: The following code will return 0 and it is unexpected:个
cp -unknown-args "hello" | tee test.txt
echo "return code is $?"
---output---
cp: invalid option -- 'k'
Try 'cp --help' for more information.
return code is 0
如果first command
有任何错误,我想把"Example 2
"变成return 1
.
这是可能的吗?如果是的话,该怎么做?