Linux bash 计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21424985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:23:44  来源:igfitidea点击:

Linux bash Timer

linuxbashif-statementwhile-loop

提问by DopeGhoti

Ok, stupid newbie question here. I thought I was making a countdown timer. This is supposed to count down from 5 and once it is at 0 then execute the echo "time is up clown" then end. What am I doing wrong here?

好吧,愚蠢的新手问题在这里。我以为我在制作倒数计时器。这应该从 5 开始倒计时,一旦为 0,则执行回声“时间到了小丑”然后结束。我在这里做错了什么?

seconds=5
date1=$((`date +%s` + $seconds)); 
while [ "$date1" -ne `date +%s` ]; do 
  if (!$date1 -lt ((`date +%s` + $seconds)+1)); then
     echo "time is up clown";
  break;
  fi;
  echo -ne "$(date -u --date @$(($date1 - `date +%s` )) +%H:%M:%S)\r";
done

回答by DopeGhoti

#!/bin/bash
SECS=5
while [[ 0 -ne $SECS ]]; do
    echo "$SECS.."
    sleep 1
    SECS=$[$SECS-1]
done
echo "Time is up, clown."