1

我有一个 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:我也很欣赏关于可以在此代码段中以更好方式完成的内容的评论。

4

1 回答 1

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
于 2013-10-16T14:25:04.100 回答