从.bashrc执行定时命令

发布于 2025-02-06 17:55:15 字数 613 浏览 3 评论 0原文

我正在尝试创建自己的最小bash shell提示,以使每个命令的执行时间执行。到目前为止,我有一个功能上的提示,看起来很吸引我的欲望。我只是错过了时间输出,例如 starship 。我尝试使用time而无需成功的时候,我尝试从$@,$*,$*,/dev/stdin读取用户输入。我还尝试在脚本开头使用date,并在将差异减去相似的影响时结束。无论我尝试过什么,执行时机总是关闭或根本不起作用。我已经检查了文档和几个网站以获取建议,但没有一个

我得到的最接近的结果是在下面使用此片段

function timer_start {
  timer=${timer:-$SECONDS}
}

function timer_stop {
  timer_show=$(($SECONDS - $timer))
  unset timer
}

trap 'timer_start' DEBUG
PROMPT_COMMAND=timer_stop

PS1="$ ${timer_show}s "

I'm trying to create my own minimal BASH shell prompt that times execution of each command entered. So far I have a functional prompt that looks appealing for my desires; I'm just missing the time output, like starship does. I've tried reading user input from $@, $*, /dev/stdin while using time without success. I also tried using date at the start of the script and end while subtracting the difference to a similar affect. No matter what I've tried the execution timing is always off or doesn't work at all. I've checked documentation and several sites for suggestions but none worked

The closest result I got was using this snippet below

function timer_start {
  timer=${timer:-$SECONDS}
}

function timer_stop {
  timer_show=$(($SECONDS - $timer))
  unset timer
}

trap 'timer_start' DEBUG
PROMPT_COMMAND=timer_stop

PS1="$ ${timer_show}s "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2025-02-13 17:55:15

提出了 @ kamilcuk - 我能够从.bashrc工作的功能计时器!当前,它会闲置/打字 +命令执行时间(我认为我还是喜欢)

prmpt() {
    timeStart=$(date +%s)

    sec=$(((timeStart-timeEnd)%60))
    min=$(((timeStart-timeEnd)%3600/60))
    hr=$(((timeStart-timeEnd)/3600))
    
    timeEnd=$timeStart

    printf '%sh:%sm:%ss' "$hr" "$min" "$sec"
}
PROMPT_COMMAND=prmpt
timeEnd=$(date +%s)

With suggestions from @KamilCuk - I was able to get a functioning timer working from .bashrc! Currently it gets idle/typing + command execution time (which I think I prefer anyway)

prmpt() {
    timeStart=$(date +%s)

    sec=$(((timeStart-timeEnd)%60))
    min=$(((timeStart-timeEnd)%3600/60))
    hr=$(((timeStart-timeEnd)/3600))
    
    timeEnd=$timeStart

    printf '%sh:%sm:%ss' "$hr" "$min" "$sec"
}
PROMPT_COMMAND=prmpt
timeEnd=$(date +%s)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文