加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

获取Bash中最后运行程序的名称

发布时间:2020-12-15 21:28:09 所属栏目:安全 来源:网络整理
导读:我有一个bash脚本,我使用trap命令捕获错误,我希望能够打印最后一个命令的名称(失败的那个) #!/bin/bashfunction error{ # echo program name}trap error ERR # Some commands .../bin/false foo # For testing 我不确定在错误函数中放入什么.我试过echo $_但
我有一个bash脚本,我使用trap命令捕获错误,我希望能够打印最后一个命令的名称(失败的那个)

#!/bin/bash
function error
{
   # echo program name
}
trap error ERR
 # Some commands ...
/bin/false foo # For testing

我不确定在错误函数中放入什么.我试过echo $_但是只有在命令没有参数时才有效.我也试过!!但这给了我“!!:命令未找到”.在交互式提示(也是bash)我得到:

$/bin/false foo
$!!
/bin/false foo

这似乎是我想要的.为什么不同?
在脚本中获取上一个命令名称的最简单方法是什么?

解决方法

在陷阱功能中尝试echo $BASH_COMMAND.

来自man bash:

BASH_COMMAND The command currently being executed or about to be executed,unless the shell is executing a command as the result of a trap,in which case it is the command executing at the time of the trap.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读