我如何为这个多行 bash 倒计时器输出分配一个变量:
seconds=10
start="$(($(date +%s) + $seconds))"
while [ "$start" -ge `date +%s` ]; do
time="$(( $start - `date +%s` ))"
printf '%s\r' "$(date -u -d "@$time" +%S)"
done
并显示结果:
echo -e $(timer_output)
语法都在一行中?
例如:
echo -e Script will exit in $(timer_output) seconds.
我现在拥有的:
echo -e "\e[5m\e[1;31mScript will exit in:\e[0m\e[25m"
seconds=10
start="$(($(date +%s) + $seconds))"
while [ "$start" -ge `date +%s` ]; do
time="$(( $start - `date +%s` ))"
printf '%s\r' "$(date -u -d "@$time" +%S)"
done
感谢 Terrance 提供的更优雅的解决方案。下面是使用一些颜色后的效果,并且执行起来非常干净:
seconds=10
for ((i=${seconds};i>=0;i--)); do printf "\r\e[5m\e[1;31mScript will exit in $i seconds...\e[0m\e[25m"; sleep 1; done