4

你可以把它想象成一个非常简单的秒表。我正在尝试编写一个 bash 脚本,该脚本显示自指定日期以来经过的时间并每秒更新一次输出。

首先,在脚本中指定一个 UNIX 日期:Fri Apr 14 14:00:00 EDT 2011. 这将是秒表开始的时候。

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

06d:10h:37m:01s

几秒钟后你会看到......

06d:10h:37m:05s

我不是想为每一秒打印一个新行。该脚本应该只有 1 行输出,并且每秒更新一次。显然,您可以随时退出脚本并重新启动它,并且由于开始时间是硬编码的,所以它仍然会正常运行。

有任何想法吗?

4

3 回答 3

5

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

... 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
于 2012-04-20T10:09:31.667 回答
3

艺术可以使用一些工作,但试试这个:

#!/bin/bash

ref_date='Thu Apr 19 17:07:39 CDT 2012'
ref_sec=$(date -j -f '%a %b %d %T %Z %Y' "${ref_date}" +%s)
update_inc=1

tput clear
cat <<'EOF'


            [|]     [|]
         _-'''''''''''''-_
        /                 \
       |                   |
       |                   |
       |                   |
        \                 /
         '-_____________-'
EOF

while :
do
  ((sec=$(date +%s) - ${ref_sec}))
  ((day=sec/86400))
  ((sec-=day*86400))
  ((hour=sec/3600))
  ((sec-=hour*3600))
  ((min=sec/60))
  ((sec-=min*60))
  tput cup 6 14
  printf "%.2id:%.2ih:%.2im:%.2is\r" ${day} ${hour} ${min} ${sec}
  sleep ${update_inc}
done

exit 0

请注意,第一个 date 命令的语法适用于 OSX。

对于 GNU 日期,请使用date --date="${ref_date}" +%s

于 2012-04-19T23:36:52.737 回答
1

如果你有 Bash4 或 ksh93,你可以使用printf %()T语法来打印strftime格式。以下示例是以您想要的格式打印的 Bash 4。Bash 精度限制为 1 秒。ksh93 支持浮点数并且需要一些修改。

#!/usr/bin/env bash

# To supply a default date/time. To use now as the default, leave empty, or run with a null first arg.
deftime='Fri Apr 14 14:00:00 EDT 2011'

if (( ${BASH_VERSINFO[0]}${BASH_VERSINFO[1]} >= 42 )); then
    unixTime() {
        printf ${2+-v "$2"} '%(%s)T' -1
    }
else
    unixTime() {
        printf ${2+-v "$2"} "$(date '+%s')"
    }
fi

stopWatch() {
    local timestamp="$(date ${1:+'-d' "$1"} '+%s')" curtime day=$((60*60*24)) hour=$((60**2))
    # unixTime -1 timestamp

    while
        unixTime -1 curtime
        (( curtime -= timestamp ))
        printf '%02dd:%02dh:%02dm:%02ds\r' $(( curtime / day )) $(( (curtime / hour) % 24 )) $(( (curtime / 60) % 60 )) $(( curtime % 60 ))
        do sleep 1
    done
}

stopWatch "${1-deftime}"

您可能还对$SECONDS变量感兴趣。不幸的是,没有方便的方法来接受您想要的人类可读的日期。这将需要大量的解析工作。date 命令(尤其是 GNU date)可以在有限的范围内做到这一点。

于 2012-04-19T14:59:36.917 回答