Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建stopwatch Bash脚本以不断显示经过的时间?_Bash_Time_Stopwatch_Elapsedtime - Fatal编程技术网

如何创建stopwatch Bash脚本以不断显示经过的时间?

如何创建stopwatch Bash脚本以不断显示经过的时间?,bash,time,stopwatch,elapsedtime,Bash,Time,Stopwatch,Elapsedtime,你可以把它想象成一个非常简单的秒表。我试图拼凑一个bash脚本,它显示自指定日期以来经过的时间,并每秒更新输出 首先,在脚本中指定一个UNIX日期:Fri Apr 14:00:00 EDT 2011。这将是秒表启动的时间 现在,当你运行脚本时,你会看到 06d:10h:37m:01s 几秒钟后你就会看到 06d:10h:37m:05s 我不想让每一秒都打印一行新的内容。脚本应该只有一行输出,并且每秒更新一次。很明显,您可以随时退出脚本并重新启动它,但由于启动时间是硬编码的,所以它仍然可以正常运行

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

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

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

06d:10h:37m:01s

几秒钟后你就会看到

06d:10h:37m:05s

我不想让每一秒都打印一行新的内容。脚本应该只有一行输出,并且每秒更新一次。很明显,您可以随时退出脚本并重新启动它,但由于启动时间是硬编码的,所以它仍然可以正常运行


有什么想法吗?

如果您有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)可以在有限的范围内完成这项工作。

本领域可能需要一些工作,但请尝试一下:

#!/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
#/bin/bash
参考日期:2012年4月19日星期四17:07:39
ref_sec=$(日期-j-f'%a%b%d%T%Z%Y'${ref_date}+%s)
更新_inc=1
t输出清除

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

... 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

我应该指定在OS X Snow Leopard上使用GNU bash版本3.2.48(1)-release(x86_64-apple-darwin10.0)。我收到一个错误,上面写着。/timer.sh:第4行:printf:`(':无效的格式字符。你知道如何修复此错误吗?我在这里使用的功能需要Bash 4.2。你必须用日期调用替换所有的
printfs
。我认为这和更改unixTime函数一样简单……我必须测试itAlright现在应该可以在Bash 3.1及更新版本中工作。拥有4.2这里正在保存一个subshell。哇!!非常酷!!但你是说它不能有一个硬连线的开始日期吗?现在的情况是,如果我关闭外壳,它会重新开始。Jordann,我尝试通过谷歌搜索“bash秒表”、“bash运行时间”和其他类似查询来修改我在网上找到的几个脚本。