Linux 如何在shell中输出返回码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6810279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:15:00  来源:igfitidea点击:

How to output return code in shell?

linuxbashshellsh

提问by yegor256

I'm trying to call a custom shell script through sh:

我正在尝试通过以下方式调用自定义 shell 脚本sh

/bin/sh -c 'myscript.sh` >log.txt 2>&1 & echo $!

Output of this command is a PID of a created background process. I want to instruct /bin/shto save return code of myscript.shto some file. Is it possible?

此命令的输出是创建的后台进程的 PID。我想指示/bin/sh将返回代码保存myscript.sh到某个文件。是否可以?

采纳答案by Karoly Horvath

(/bin/sh -c "myscript.sh" >log.txt 2>&1 ; echo $? >somefile) & echo $!

回答by jman

echo $? >> /path/to/return_code

$? has the return code of the last statement in bash.

$? 具有 bash 中最后一条语句的返回码。

回答by TMS

(
/bin/sh -c 'myscript.sh` >log.txt 2>&1
echo $? > some_file
) &