bash 在另一个脚本中获取最后一个 shell 命令的退出代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12999830/
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-09-09 22:51:28  来源:igfitidea点击:

Getting exit code of last shell command in another script

bashshellzshexit-code

提问by sebastiangeiger

I am trying to beef up my notify script. The way the script works is that I put it behind a long running shell command and then all sorts of notifications get invoked after the long running script finished.

我正在尝试加强我的通知脚本。脚本的工作方式是我将它放在一个长时间运行的 shell 命令后面,然后在长时间运行的脚本完成后调用各种通知。

For example:

例如:

sleep 100; my_notify

It would be nice to get the exit code of the long running script, the problem is that calling my_notifycreates a new process that does not have access to the $?variable.

获得长时间运行脚本的退出代码会很好,问题是调用my_notify会创建一个无法访问该$?变量的新进程。

Compare:

相比:

~ $: ls nonexisting_file; echo "exit code: $?"; echo "PPID: $PPID"
ls: nonexisting_file: No such file or directory
exit code: 1
PPID: 6203

vs.

对比

~ $: ls nonexisting_file; my_notify      
ls: nonexisting_file: No such file or directory
exit code: 0
PPID: 6205

The my_notifyscript has the following in it:

my_notify脚本包含以下内容:

#!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"

I am looking for a way to get the exit code of the previous command without changing the structure of the command too much. I am aware of the fact that if I change it to work more like time, e.g. my_notify longrunning_command...my problem would be solved, but I actually like that I can tack it at the end of a command and I fear complications of this second solution.

我正在寻找一种方法来获取上一个命令的退出代码,而不会过多地改变命令的结构。我知道如果我将其更改为更像time,例如,my_notify longrunning_command...我的问题将得到解决,但我实际上喜欢这样,我可以在命令的末尾添加它,并且我担心第二个解决方案会出现并发症。

Can this be done or is it fundamentally incompatible with the way that shells work?

这可以完成还是从根本上与 shell 的工作方式不兼容?

My shell is zshbut I would like it to work with bashas well.

我的外壳是,zsh但我希望它也能使用bash

回答by qqx

You'd really need to use a shell function in order to accomplish that. For a simple script like that it should be pretty easy to have it working in both zsh and bash. Just place the following in a file:

您确实需要使用 shell 函数来实现这一点。对于像这样的简单脚本,让它在 zsh 和 bash 中工作应该很容易。只需将以下内容放入文件中:

my_notify() {
  echo "exit code: $?"
  echo "PPID: $PPID"
}

Then source that file from your shell startup files. Although since that would be run from within your interactive shell, you may want to use $$ rather than $PPID.

然后从您的 shell 启动文件中获取该文件。虽然因为它会在你的交互式 shell 中运行,你可能想要使用 $$ 而不是 $PPID。

回答by Ignacio Vazquez-Abrams

It is incompatible. $?onlyexists within the current shell; if you want it available in subprocesses then you must copy it to an environment variable.

这是不兼容的。$?存在于当前shell中;如果您希望它在子进程中可用,则必须将其复制到环境变量中。

The alternative is to write a shell function that uses it in some way instead.

另一种方法是编写一个 shell 函数,以某种方式使用它。

回答by BIBS

One method to implement this could be to use EOF tag and a master script which will create your my_notify script.

实现这一点的一种方法可能是使用 EOF 标记和一个主脚本,它将创建您的 my_notify 脚本。



#!/bin/bash

if [ -f my_notify ] ; then
rm -rf my_notify
fi

if [ -f my_temp ] ; then
rm -rf my_temp
fi

retval=`ls non_existent_file &> /dev/null  ; echo $?`
ppid=$PPID
echo "retval=$retval" 
echo "ppid=$ppid" 
cat >> my_notify << 'EOF'
#!/bin/bash

echo "exit code: $retval"
echo " PPID =$ppid"
EOF

sh my_notify 


You can refine this script for your purpose.

您可以根据自己的目的优化此脚本。