Bash 脚本:暂停倒计时并按 ENTER(或任意键)恢复倒计时

问题描述 投票:0回答:1

我有一个运行 bash 脚本的任务调度程序。该任务首先打开一个 GIT Bash 终端,显示一条打开消息(“脚本将在 60 秒后启动。”)并在倒计时结束时运行脚本。

现在,我想改善用户体验,允许他/她停止/恢复倒计时或(无需任何干预)让脚本自动运行。所以,这是程序流程:

  1. GIT Bash 终端打开后,允许用户在显示的时间范围内按 ENTER 或任何其他键暂停脚本;
  2. 如果用户未采取任何操作,倒计时将继续,脚本将在时间范围结束时运行;
  3. 如果用户按下 ENTER(或任何其他键),则通过再次按 ENTER(或任何其他键),他/她将恢复倒计时,并且将立即运行。

我尝试使用

read -p
,但它对我来说不好:我不希望用户操作触发某些内容,而是停止/暂停倒计时(然后恢复倒计时)。

bash shell scheduled-tasks git-bash
1个回答
4
投票

更新历史:

  • 答案的第一部分实现了可暂停倒计时(打印很多行)
  • 第二部分实现了更简洁的可暂停超时(在按键时打印一条静态行+附加消息)
  • 一个更复杂的不断更新同一行的可暂停倒计时位于第三个代码片段中。

可暂停倒计时

结合类似问题的一些提示这里和一些关于如何读取单个字符的外部资源(例如这里,否则互联网上的任何地方),并添加一个额外的循环来恢复,这就是我想出的:

#!/bin/bash

# Starts a pausable/resumable countdown.
# 
# Starts the countdown that runs for the
# specified number of seconds. The 
# countdown can be paused and resumed by pressing the
# spacebar. 
#
# The countdown can be sped up by holding down any button
# that is no the space bar.
#
# Expects the number of seconds as single
# argument.
#
# @param $1 number of seconds for the countdown
function resumableCountdown() {
  local totalSeconds=$1
  while (( $totalSeconds > 0 ))
  do
    IFS= read -n1 -t 1 -p "Countdown $totalSeconds seconds (press <Space> to pause)" userKey
    echo ""
    if [ "$userKey" == " " ]
    then
      userKey=not_space
      while [ "$userKey" != " " ]
      do
        IFS= read -n1 -p "Paused, $totalSeconds seconds left (press <Space> to resume)" userKey
    echo ""
      done
    elif  [ -n "$userKey" ]
    then
      echo "You pressed '$userKey', press <Space> to pause!"
    fi
    totalSeconds=$((totalSeconds - 1))
  done
}

# little test
resumableCountdown 60

这可以作为独立脚本保存和运行。该函数可以在其他地方重用。它以

SPACE
暂停/恢复,因为这对我来说似乎更直观,因为这就是它的工作原理,例如在浏览器中嵌入的视频播放器中。

也可以通过按空格键以外的键来加快倒计时(这是一个功能)。


发出警告消息并等待可暂停超时

以下变体实现了可暂停超时,它只打印初始警告消息,除非用户通过按空格键暂停或恢复(内部)倒计时:

# Prints a warning and then waits for a
# timeout. The timeout is pausable.
#
# If the user presses the spacebar, the 
# internal countdown for the timeout is 
# paused. It can be resumed by pressing
# spacebar once again.
#
# @param $1 timeout in seconds
# @param $2 warning message
warningWithPausableTimeout() {
  local remainingSeconds="$1"
  local warningMessage="$2"
  echo -n "$warningMessage $remainingSeconds seconds (Press <SPACE> to pause)"
  while (( "$remainingSeconds" > 0 ))
  do
    readStartSeconds="$SECONDS"
    pressedKey=""
    IFS= read -n1 -t "$remainingSeconds" pressedKey
    nowSeconds="$SECONDS"
    readSeconds=$(( nowSeconds - readStartSeconds ))
    remainingSeconds=$(( remainingSeconds - readSeconds ))
    if [ "$pressedKey" == " " ]
    then
      echo ""
      echo -n "Paused ($remainingSeconds seconds remaining, press <SPACE> to resume)"
      pressedKey=""
      while [ "$pressedKey" != " " ]
      do
        IFS= read -n1 pressedKey
      done
      echo ""
      echo "Resumed"
    fi
  done
  echo ""
}

warningWithPausableTimeout 10 "Program will end in"
echo "end."

更新同一行的可暂停倒计时

这是与第一个类似的倒计时,但只需要一行。依靠

echo -e
来擦除和覆盖以前打印的消息。

# A pausable countdown that repeatedly updates the same line.
#
# Repeatedly prints the message, the remaining time, and the state of
# the countdown, overriding the previously printed messages.
#
# @param $1 number of seconds for the countdown
# @param $2 message
singleLinePausableCountdown() {
  local remainingSeconds="$1"
  local message="$2"
  local state="run"
  local stateMessage=""
  local pressedKey=""
  while (( $remainingSeconds > 0 ))
  do
    if [ "$state" == "run" ]
    then
      stateMessage="[$remainingSeconds sec] Running, press <SPACE> to pause"
    else
      stateMessage="[$remainingSeconds sec] Paused, press <SPACE> to continue"
    fi
    echo -n "$message $stateMessage"
    pressedKey=""
    if [ "$state" == "run" ]
    then 
      IFS= read -n1 -t 1 pressedKey
      if [ "$pressedKey" == " " ]
      then
        state="pause"
      else 
        remainingSeconds=$(( remainingSeconds - 1 ))
      fi
    else
      IFS= read -n1 pressedKey
      if [ "$pressedKey" == " " ]
      then
        state="run"
      fi
    fi
    echo -ne "\033[1K\r"
  done
  echo "$message [Done]"
}

如果线条比控制台宽度长(它不会完全擦除线条),则此行为可能会很奇怪。


为任何尝试做某事的人提供未分类的提示集合。类似:

  • IFS= read -n1
    读取单个字符
  • read -t <seconds>
    设置
    read
    的超时。一旦超时到期,
    read
    以非零值退出,并将变量设置为空。
  • Magic bash 内置变量
    $SECONDS
    测量从脚本开始的时间(以秒为单位)。
  • 如果已使用
    echo -n
    打印了一行,则可以使用
    echo -ne "\033[1K\r"
    将其删除并重置。
© www.soinside.com 2019 - 2024. All rights reserved.