Mik*_*ike 7 unix shell io-redirection exitstatus
假设我在unix shell中执行此操作
$ some-script.sh | grep mytext
$ echo $?
Run Code Online (Sandbox Code Playgroud)
这将给我退出代码 grep
但是如何获得退出代码呢? some-script.sh
编辑
假设管道操作是不可变的.也就是说,我无法分开它并单独运行这两个命令
有多种解决方案,这取决于您想要做什么.
最简单易懂的方法是将输出发送到文件,然后在保存退出代码后grep for it:
tmpfile=$(mktemp)
./some-script.sh > $tmpfile
retval=$?
grep mytext $tmpfile
rm tmpfile
Run Code Online (Sandbox Code Playgroud)