在ZSH脚本中显示倒计时?

时间:2013-10-15 13:35:35

标签: shell timeout zsh

我有一个zsh脚本,我从用户那里读取输入,一段时间后我默认为'是'。

我目前正在使用read -q -tt 5之类的东西。这是实际的代码:

echo "${Red}Errors${RegF} were detected in the output of $i."
read -q -tt $CONFIRMATION_TIMEOUT "REPLY?Proceed? [Yn] "
echo
case $REPLY in
    'N') ;&
    'n') echo "Aborting..."; exit 0;;
    *) ;;
esac

我得到了:

Errors were detected in the output of groups.xml.
Proceed? [Yn] 

但我想在屏幕上显示一个倒计时,可能就像

Errors were detected in the output of groups.xml.
Proceed? [Yn] (5)

这个数字每秒都在减少。

我有什么方法可以做到这一点吗?

谢谢!

PS:我也非常感谢有关可以在此代码段中以更好的方式完成的内容的评论。

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

echo "${Red}Errors${RegF} were detected in the output of $i."
{ i=$CONFIRMATION_TIMEOUT; while test $((i--)) -ge 0; do 
  printf "\rREPLY?Proceed? [Yn] ($i)"; sleep 1; done; } &

read -q -tt $CONFIRMATION_TIMEOUT
kill $!
echo
case $REPLY in
    'N') ;&
    'n') echo "Aborting..."; exit 0;;
    *) ;;
esac