在获得去年的Bash运行程序的名称 [英] Get name of last run program in Bash

查看:100
本文介绍了在获得去年的Bash运行程序的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,我使用的陷阱命令捕获错误,我希望能够打印的最后一个命令的名称(失败的)

I have a bash script where I trap errors using the trap command, and I would like to be able to print the name of the last command (the one that failed)

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

我不知道要放什么东西在错误功能。我试图回声$ _ 但如果命令没有变,只有工作。我也试图与 !! 但是这给了我!!:命令未找到。在一个交互式提示符(也bash)的我得到:

I'm not sure what to put in the error function. I tried echo $_ but that only works if the command has no arguments. I also tried with !! but that gives me "!!: command not found". At an interactive prompt (also bash) I get:

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

这似乎是pretty我想太多的东西。为什么会有差别?
是什么让一个脚本里面的previous命令的名称最简单的方法?

which seems to be pretty much what I want. Why the difference? What is the easiest way to get the name of the previous command inside a script?

推荐答案

尝试回声$ BASH_COMMAND 在你的陷阱功能。

男人庆典

BASH_COMMAND结果
                该命令正在执行或者即将执行,
                除非壳正在执行的命令为陷阱的结果,
                在这种情况下,它是在该时间执行命令
                陷阱。

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.

这篇关于在获得去年的Bash运行程序的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆