BASH:呼应的最后一个命令运行 [英] BASH: echoing the last command run

查看:132
本文介绍了BASH:呼应的最后一个命令运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想呼应bash脚本内运行的最后一个命令。我找到了一种与某些历史,尾巴,头,sed的时,命令重新present特定的行我脚本解析器的角度来看,工作正常做到这一点。但是在某些情况下,当命令被插入盒内我没有得到预期的输出,例如语句:

I am trying to echo the last command run inside a bash script. I found a way to do it with some history,tail,head,sed which works fine when commands represent a specific line in my script from a parser standpoint. However under some circumstances I don't get the expected output, for instance when the command is inserted inside a case statement:

脚本:

#!/bin/bash
set -o history
date
last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
echo "last command is [$last]"

case "1" in
  "1")
  date
  last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
  echo "last command is [$last]"
  ;;
esac

输出:

Tue May 24 12:36:04 CEST 2011
last command is [date]
Tue May 24 12:36:04 CEST 2011
last command is [echo "last command is [$last]"]

[Q]谁能帮我找到一个方法来呼应上次运行命令不管如何/在哪里这个命令被称为bash脚本内?

我的答案

尽管我的同事SO'ers pciated贡献多少AP $ P $,我选择了写运行功能 - 它运行的所有参数作为单个命令和显示命令,当它失败的错误code - 具有以下优点:结果
-I只需要prePEND我想请与运行这让他们在同一行,不会影响我的脚本结果的简洁的命令
-Whenever脚本这些命令中的一个失败,我的脚本的最后输出线清楚显示一条消息,该命令其退出code,这使得调试失败沿着容易

Despite the much appreciated contributions from my fellow SO'ers, I opted for writing a run function - which runs all its parameters as a single command and display the command and its error code when it fails - with the following benefits:
-I only need to prepend the commands I want to check with run which keeps them on one line and doesn't affect the conciseness of my script
-Whenever the script fails on one of these commands, the last output line of my script is a message that clearly displays which command fails along with its exit code, which makes debugging easier

示例脚本:

#!/bin/bash
die() { echo >&2 -e "\nERROR: $@\n"; exit 1; }
run() { "$@"; code=$?; [ $code -ne 0 ] && die "command [$*] failed with error code $code"; }

case "1" in
  "1")
  run ls /opt
  run ls /wrong-dir
  ;;
esac

输出:

$ ./test.sh
apacheds  google  iptables
ls: cannot access /wrong-dir: No such file or directory

ERROR: command [ls /wrong-dir] failed with error code 2

我测试了多个参数,bash的变量各种命令作为参数,报价参数...和运行功能并没有打破他们。我迄今发现的唯一问题是运行打破回音,但我不打算反正检查我的回声。

I tested various commands with multiple arguments, bash variables as arguments, quoted arguments... and the run function didn't break them. The only issue I found so far is to run an echo which breaks but I do not plan to check my echos anyway.

推荐答案

这个命令历史记录是一个互动的功能。只有完整的命令在历史进入。例如,情况构造输入作为一个整体,当外壳已经完成解析它。无论是仰视历史与历史内置(也无法通过shell扩展(打印出来!:P ))没有你仿佛想,这是打印简单的命令调用。

The command history is an interactive feature. Only complete commands are entered in the history. For example, the case construct is entered as a whole, when the shell has finished parsing it. Neither looking up the history with the history built-in (nor printing it through shell expansion (!:p)) does what you seem to want, which is to print invocations of simple commands.

DEBUG 陷阱让你任何简单的命令执行前右执行命令。要执行的命令(用空格隔开的单词)的字符串版本是在<一个可用href=\"http://www.gnu.org/software/bash/manual/bashref.html#index-BASH_005fCOMMAND-169\"><$c$c>BASH_COMMAND变量。

trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
…
echo "last command is $previous_command"

注意 previous_command 将改变你每次运行一个命令的时候,所以它保存到一个变量,才能使用它。如果你想知道previous命令的返回状态为好,既保存在一个单一的命令。

Note that previous_command will change every time you run a command, so save it to a variable in order to use it. If you want to know the previous command's return status as well, save both in a single command.

cmd=$previous_command ret=$?
if [ $ret -ne 0 ]; then echo "$cmd failed with error code $ret"; fi

此外,如果你只是想中止一个失败的命令,使用 设置-e 使你的脚本退出的第一个失败的命令。您可以从 退出陷阱

set -e
trap 'echo "exit $? due to $previous_command"' EXIT

请注意,如果你想跟踪你的脚本,看看它在做什么,忘了这一切,并使用的 设置-x

Note that if you're trying to trace your script to see what it's doing, forget all this and use set -x.

这篇关于BASH:呼应的最后一个命令运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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