在 5 秒内更改 bash 脚本倒计时延迟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26073520/
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 11:25:44  来源:igfitidea点击:

Changing bash script countdown delay in 5 sec steps

linuxbashcommand-line-interface

提问by tubos

I have following command i use in a script as a countdown timer and it works great

我在脚本中使用了以下命令作为倒数计时器,效果很好

#  30 sec Countdown Timer
for i in {30..1};do echo -n "$i." && sleep 1; done

Output:30.29.28.27.26 etc....

But I would like to be able to output in 5sec intervals like

但我希望能够以 5 秒的间隔输出

: 30.25.20.15 etc..

how can i change the script to do this ?

我怎样才能改变脚本来做到这一点?

回答by Cyrus

for i in {30..1..5};do echo -n "$i." && sleep 5; done

回答by Pankaj Singhal

for i in {30..1}
do 
  if [ $((i%5)) == 0 ]
  then 
    echo -n "$i."
  fi
  sleep 1
done

回答by Onlyjob

Here is straightforward script compatible with Dash and Bash:

这是与 Dash 和 Bash 兼容的简单脚本:

i=30 && while [ $i -gt 0 ]; do sleep 5; i=$(($i-5)); printf "$i."; done