庆典:如何截获每个命令 [英] bash: how to intercept every command

查看:86
本文介绍了庆典:如何截获每个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法拦截给庆典的每一个命令?我可以拦截特定的命令,例如, CD 定义一个函数 CD(),我可以做一个-command-在一次一对其它命令,以及。但是,我可以写一个执行每一个命令之前它被调用函数?我想要做的命令一些记账,然后执行命令。

Is there a way to intercept every command given to bash? I can intercept a particular command, e.g., cd by defining a function cd() and I can do that for one-command-at-a-time for other commands as well. But can I write a function which gets called before every command is executed? I want to do some bookkeeping of commands, and then executed the command.

米哈尔Šrajer的想法 PS4 ='$(回声$(日期)$(历史1)>> /tmp/trace.txt)TRACE:看起来很有前途的,但这里是我的输出得到:

Michał Šrajer's idea PS4='$(echo $(date) $(history 1) >> /tmp/trace.txt) TRACE: ' looks very promising but here is the output I get:

$ ping www.google.com
 TRACE: ping www.google.com
PING www.l.google.com (74.125.224.52) 56(84) bytes of data.
64 bytes from 74.125.224.52: icmp_seq=1 ttl=56 time=3.77 ms
64 bytes from 74.125.224.52: icmp_seq=2 ttl=56 time=2.33 ms
^C
--- www.l.google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 2.334/3.054/3.774/0.720 ms
  TRACE: echo -ne '\033]0;myhost.com /home/yogeshwer/github/myproject\007'
  TRACE: grep -e '\* '
  TRACE: git branch
  TRACE: sed 's/^..\(.*\)/ {\1}/'

周三8月3日12点47分27秒PDT 2011 6672平www.google.com 被写入在/tmp/trace.txt整整5倍。四个其他的来自于我的 PS1 在我运行下面的命令的定义: $(GIT分支2 - ;的/ dev / null的| grep的-e \\ *| sed的S /^..\\(.*\\)/ {\\ 1} /)。两个问题:

And Wed Aug 3 12:47:27 PDT 2011 6672 ping www.google.com get written in /tmp/trace.txt exactly 5 times. The four other ones comes from my definition of PS1 where I run the following command: $(git branch 2> /dev/null | grep -e "\* " | sed "s/^..\(.*\)/ {\1}/"). Two questions:


  • 是否有可能写入命令 /tmp/trace.txt 到底是什么?

  • 而更重要的,是有可能不杂乱命令的输出,但只写命令 /tmp/trace.txt

  • Is it possible to write the command to /tmp/trace.txt exactly?
  • And more importantly, is it possible to not clutter the output of the command, but only write the command to /tmp/trace.txt?

我是如此兴奋的是能够从所有我的bash会话在一个地方记录命令的可能性!

I am so excited about the possibility of being able to record commands from all my bash sessions in one place!

推荐答案

您可以设置PS4变量,它为每一个命令,如果是跟踪上执行只是执行前评估:

You can set the PS4 variable, which is evaluated for every command being executed just before the execution if trace is on:

PS4='$(echo $(date) $(history 1) >> /tmp/trace.txt) TRACE: '

然后,启用跟踪:

Then, enable trace:

set -x

要停止跟踪,只是:

set +x

这篇关于庆典:如何截获每个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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