如何创建秒表Bash脚本以不断显示经过的时间? [英] How do I create a stopwatch Bash script to constantly display elapsed time?

查看:114
本文介绍了如何创建秒表Bash脚本以不断显示经过的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将其视为一个非常简单的秒表.我正在尝试破解一个bash脚本,该脚本显示自指定日期以来的经过时间,并每秒更新一次输出.

You can think of this like a really simple stopwatch. I'm trying to hack together a bash script that displays the elapsed time since a specified date and updates the output every second.

首先,在脚本内您指定UNIX日期:Fri Apr 14 14:00:00 EDT 2011.这就是秒表开始的时间.

First, Inside the script you'd specify a UNIX date: Fri Apr 14 14:00:00 EDT 2011. This would be when the stopwatch starts.

现在,当您运行脚本时,您会看到...

Now, when you run the script, you'd see...

06d:10h:37m:01s

几秒钟后,您会看到...

and a few seconds later you'd see...

06d:10h:37m:05s

我不是想每隔一秒钟就打印一条新行.该脚本应仅输出1行,并每秒更新一次.显然,您可以随时退出脚本并重新启动它,由于开始时间是经过硬编码的,因此它仍然可以正常运行.

I'm not trying to have a new line printed for every second that elapses. The script should only have 1 line of output, and update it every second. Obviously you could quit the script and start it up again at any time, and it would still be right on since the starting time is hard coded.

有什么想法吗?

推荐答案

我在长时间运行的脚本中使用以下代码段. 计时器运行在一个函数(单独的进程)中,如果主进程因键盘中断而终止,该函数将被杀死(陷阱).输出显示从计时器开始经过的时间:

I use the following code snippet in long-running scripts. The timer runs in a function (separate process) which will be killed (trap) if the main process is terminated by a keybord interrupt. The output shows the time elapsed from the start of the timer:

... working [hh:mm:ss]  00:07:58

摘要:

#===  FUNCTION  ================================================================
#          NAME:  progressIndicatorTime
#   DESCRIPTION:  Display a progress indicator with seconds passed.
#    PARAMETERS:  [increment]   between 1 and 60 [sec], default is 2 [sec]
#===============================================================================
function progressIndicatorTime ()
{
  declare default=2                                       # default increment [sec]
  declare increment="${1:-$default}"                      # 1. parameter or default
  declare format='\b\b\b\b\b\b\b\b%02d:%02d:%02d'         # time format hh:mm:ss
  declare timepassed=0
  declare seconds minutes hours

  [[ ! "$increment" =~ ^([1-9]|[1-5][0-9]|60)$ ]] && increment=$default
  printf " ... working [hh:mm:ss]  00:00:00"
  while : ; do                                            # infinite loop 
    ((seconds=timepassed%60))
    ((minutes=timepassed/60))
    ((hours=minutes/60))
    ((minutes=minutes%60))
    printf "$format" $hours $minutes $seconds
    sleep $increment || break                             # sleep ...
    ((timepassed+=increment))
  done
}    # ----------  end of function progressIndicatorTime  ----------

progressIndicatorTime &                                   # run progress indicator
declare progressIndicatorPid=${!}                         # save process ID
trap  "kill $progressIndicatorPid" INT TERM               # trap keyboard interrupt

#
# run long command
#

kill -s SIGTERM $progressIndicatorPid                     # terminate progress indicator

这篇关于如何创建秒表Bash脚本以不断显示经过的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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