【问题标题】:Display a progress bar or spinner while a command is running in bash script [duplicate]在 bash 脚本中运行命令时显示进度条或微调器 [重复]
【发布时间】:2015-08-21 20:33:40
【问题描述】:

我有一个 bash 脚本,其结尾如下:

trap "exit" INT
for ((i=0; i < $srccount; i++)); do
    echo -e "\"${src[$i]}\" will be synchronized to \"${dest[$i]}\""
    echo -e $'Press any key to continue or Ctrl+C to exit...\n' 
    read -rs -n1
    #show_progress_bar()
    rsync ${opt1} ${opt2} ${opt3} ${src[$i]} ${dest[$i]}
done

我需要一个命令或函数,例如show_progress_bar(),它在rsync 命令运行时每隔一秒将.(一个点)放入标准输出(或旋转为/ 的旋转/ - \ | rsync 运行时的序列)。 是否可以?我是否需要自己编写这样的函数,或者有可用的脚本用于此目的?

【问题讨论】:

    标签: bash progress-bar


    【解决方案1】:

    它不漂亮,但它有效:

    ~$ while true; do echo -n .; sleep 1; done & sleep 3; kill %-; wait; echo;
    [1] 26255
    ...[1]+  Terminated              while true; do
        echo -n .; sleep 1;
    done
    

    (将“睡眠3”换成你的实际工作)

    它是这样工作的:

    • while 循环作为后台作业运行。
    • 同时,您的工作(在我的示例中为“睡眠 3”)在前台运行。
    • 工作完成后,“kill %-”会终止回显循环。
    • 然后我们等待作业终止,并回显一个换行符,以防万一。

    就像我说的,它并不漂亮。并且可能有更好的方法来做到这一点。 :)

    编辑:例如,喜欢这里的答案:Using BASH to display a progress (working) indicator

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多