显示进度条或微调
问题描述:
我有结束如下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
正在运行)。 这可能吗?我是否需要自己编写这样的函数,或者是否有可用于此目的的脚本?
答
这不是很漂亮,但它的工作原理:
~$ 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