在每个执行的命令之前和之后打印 Bash 中的 epoch 时间。

huangapple go评论74阅读模式
英文:

Print epoch time before and after every issued command in bash

问题

I am trying to modify my ~/.bash_profile so that the Unix epoch time automatically gets printed before and after the execution of every issued command in bash, like so:

Started execution at: <epoch time>
...
<stuff printed out by some-maybe-multiline-command, if any>
...
Finished execution at: <epoch time>
[prompt]$```

How can I achieve this? Ideally, I want to have minimum side effects while doing so (e.g. I do not want this change to affect too many logs if possible); it should ideally only affect what I see on the terminal screen.

Thanks a lot in advance!

I found [this answer](https://unix.stackexchange.com/questions/562018/run-every-bash-command-through-a-function-or-wrapper) Unix StackExchange, and tried to adopt it to my situation; but I haven't been successful so far..

Adding `trap <command> DEBUG` into the `~/.bash_profile` is the closest thing to a solution that I found so far. However, I am not sure how to make the `<command>` be aware of whether it is being executed before or after the execution of the issued command.

<details>
<summary>英文:</summary>

I am trying to modify my `~/.bash_profile` so that the Unix epoch time automatically gets printed before and after the execution of every issued command in bash, like so:

[prompt]$ some-maybe-multiline-command
Started execution at: <epoch time>
...
<stuff printed out by some-maybe-multiline-command, if any>
...
Finished execution at: <epoch time>
[prompt]$

How can I achieve this? Ideally, I want to have minimum side effects while doing so (e.g. I do not want this change to affect too many logs if possible); it should ideally only affect what I see on the terminal screen.

Thanks a lot in advance!

I found [this answer](https://unix.stackexchange.com/questions/562018/run-every-bash-command-through-a-function-or-wrapper) Unix StackExchange, and tried to adopt it to my situation; but I haven&#39;t been successful so far..

Adding `trap &lt;command&gt; DEBUG` into the `~/.bash_profile` is the closest thing to a solution that I found so far. However, I am not sure how to make the `&lt;command&gt;` be aware of whether it is being executed before or after the execution of the issued command

</details>


# 答案1
**得分**: 0

`trap command DEBUG` 在执行前执行该命令。因此,你需要另外的东西来在执行后执行。

一个解决方案可以是 PS1 和 trap 的组合。

```bash
function showBefore(){
    printf "started at %s\n" $(date +"%s")
}
trap showBefore DEBUG
export PS1="Ended at \D{%s}> "

示例输出

Ended at 1686798230> sleep 10
started at 1686798232
Ended at 1686798242> 

你也可以使用 PROMPT_COMMAND='printf "Ended at %s\n" $(date +"%s")' 代替 PS1。这两者都在提示符显示后执行,即在上一个命令执行完之后执行。但是,如你所见的链接中所示,showBefore 函数变得更复杂,因为执行 PROMPT_COMMAND 也会触发 TRAP。这并不美观(尽管你可以忽略所有的 "started at" 行,除了第一行)。

英文:

trap command DEBUG execute the command before the execution. So you need something else for after.

One solution could be a combination of PS1 and trap

function showBefore(){
    printf &quot;started at %s\n&quot; $(date +&quot;%s&quot;)
}
trap showBefore DEBUG
export PS1=&quot;Ended at \D{%s}&gt; &quot;

Example output

Ended at 1686798230&gt; sleep 10
started at 1686798232
Ended at 1686798242&gt; 

You could also use PROMPT_COMMAND=&#39;printf &quot;Ended at %s\n&quot; $(date +&quot;%s&quot;)&#39; instead of PS1. Both occur when the prompt is displayed, that is just after the previous command is finished. But then (as shown in the link you have seen) the showBefore function becomes more complicated, because the execution of the PROMPT_COMMAND will also trigger the TRAP. Which is not aesthetic (tho you can ignore all the "started at lines" but the first)

答案2

得分: 0

你可以使用Bash的PS0(需要Bash 4.4+)和PROMPT_COMMAND

根据man bash

  • PROMPT_COMMAND

如果设置了此变量并且是一个数组,则在发出每个主提示符之前,将执行每个设置元素的值作为命令。如果设置了但不是数组变量,则其值将用作要执行的命令。

  • PS0

此参数的值在读取命令后且在执行命令之前由交互式Shell展开和显示。

示例:

PS0='>>> $( date +%s )\n'
PROMPT_COMMAND='echo "<<< $( date +%s )"'
$ echo hello world; sleep 2
>>> 1686801897
hello world
<<< 1686801899
英文:

You can use Bash's PS0 <sup>(requires bash 4.4+)</sup> and PROMPT_COMMAND.

According to man bash:

> * PROMPT_COMMAND
>
> If this variable is set, and is an array, the value of each set element is executed as a command prior to issuing each primary prompt. If this is set but not an array variable, its value is used as a command to execute instead.
>
> * PS0
>
> The value of this parameter is expanded and displayed by interactive shells after reading a command and before the command is executed.
>

Example:

PS0=&#39;&gt;&gt;&gt; $( date +%s )\n&#39;
PROMPT_COMMAND=&#39;echo &quot;&lt;&lt;&lt; $( date +%s )&quot;&#39;
$ echo hello world; sleep 2
&gt;&gt;&gt; 1686801897
hello world
&lt;&lt;&lt; 1686801899

huangapple
  • 本文由 发表于 2023年6月15日 10:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定