在bash中的每个命令之后执行命令 [英] Execute command after every command in bash

查看:151
本文介绍了在bash中的每个命令之后执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我运行的每个bash命令之后打印日期.

I want to print the date after every bash command I run.

这可以帮助我了解远离键盘时要执行多少命令.

This could help me understand how much a command took to execute when I am away from keyboard.

我知道我能做到

`DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

要获取日期,但我不知道在bash上执行的每个命令之后如何甚至什至可以运行此命令.

to get the date but I don't know how or even if it could be possible to run this command after every command I execute on bash.

我还想对每个命令运行 之前的命令,以便知道一个命令花费了多长时间.

I would also be interested in running the same command before every command so I could know how long a command took.

有可能吗?

我应该编辑什么文件?

例如:

$ wget google.com
15/07/2017 23:40:05

如果我也可以介绍以下功能,我将很高兴:

I would be happy, if I could also introduce this following feauture:

$ wget google.com
15/07/2017 23:40:05 15/07/2017 23:40:11 
Program run for 00:00:06

第一个日期是我运行程序的日期,第二个是程序终止时的日期,第三个是不言自明的.

where the first date is when I ran the program, second is when program terminated the third is self-explanatonary.

如您所知,我不想每次都输入

As you understood, I don't want to type every time

$ wget google.com && `DATE=`date +%d/%m/%Y\ %H:%M:%S` && echo $DATE`

推荐答案

要在输入每个命令之前执行cmd,请在DEBUG上设置一个陷阱.例如

To execute a cmd before every command entered, set a trap on DEBUG. Eg.

trap date DEBUG

要在发出提示之前执行该命令,请设置PROMPT_COMMAND:

To execute that command before emitting a prompt, set PROMPT_COMMAND:

PROMPT_COMMAND=date

这篇关于在bash中的每个命令之后执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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