如何在 Bash 脚本中捕获退出代码
我的 bash 代码中有很多退出点。我需要在退出时做一些清理工作,所以我使用 trap 为退出添加回调,如下所示:
trap "mycleanup" EXIT
问题是有不同的退出代码,我需要做相应的清理工作。我可以在 mycleanup 中获取退出代码吗?
There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this:
trap "mycleanup" EXIT
The problem is there're different exit codes, I need to do corresponding cleanup works. Can I get exit code in mycleanup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
接受的答案基本上是正确的,我只是想澄清一下。
以下示例效果很好:
但是如果在没有函数的情况下进行内联清理,则必须更加小心。例如,这不起作用:
相反,您必须转义
$rv
和$?
变量:您可能还想转义
$tmpdir
,因为它会在陷阱行执行时进行评估,并且如果 tmpdir 值稍后发生变化,可能不会给出预期的行为。编辑:使用 shellcheck 检查您的 bash 脚本并注意此类问题。
The accepted answer is basically correct, I just want to clarify things.
The following example works well:
But you have to be more careful if doing cleanup inline, without a function. For example this won't work:
Instead you have to escape the
$rv
and$?
variables:You might also want to escape
$tmpdir
, as it will get evaluated when the trap line gets executed and if thetmpdir
value changes later that might not give the expected behaviour.Edit: Use shellcheck to check your bash scripts and be aware of problems like this.
我认为您可以使用
$?
来获取退出代码。I think you can use
$?
to get the exit code.我发现最好将 EXIT 陷阱与其他信号的陷阱分开。
示例陷阱测试脚本...
临时文件已清理。
文件退出值 10 被保留!
中断导致退出值为 2
基本上只要您不在 EXIT 陷阱中使用“exit”,它将退出并保留原始退出值。
旁白:注意 EXIT 陷阱中的引用。这让我可以更改脚本生命周期内需要清理的文件。在尝试删除 $tmpfile 之前,我经常还对它是否存在进行测试,因此我什至不需要在脚本开始时设置它,只需在创建它之前即可。
I've found it is better to separate EXIT trap from the trap for other signals
Example trap test script...
The temporary file is cleaned up.
The file exit value of 10 is preserved!
Interrupts result in an exit value of 2
Basically as long as you don't use "exit" in a EXIT trap, it will exit with the original exit value preserved.
ASIDE: Note the quoting in the EXIT trap. That lets me change what file needs to be cleaned up during the scripts lifetime. I often also include a test for the existence of the $tmpfile before trying to remove it, so I don't even need to set it at the start of the script, only before creating it.
下面的代码运行良好。您可以存储退出代码并定义陷阱函数中每个退出代码所需的命令。
The following code works well. You can store the exit code and define the commands that are needed for each exit code in the trap function.