如何为命令很少的 shell 脚本添加进度条?
How to add Progress bar for shell scripts having few commands?
我正在用一些命令编写 shell 脚本。我想给它添加一个进度条。
例如-
Command 1 #Progress bar at 25%
Command 2 #Progress bar at 50%
Command 3 #Progress bar at 75%
Command 4 #Progress bar at 100%
非常感谢任何帮助。
编辑:我正在使用 linux 的 Bash 终端,并希望在终端中显示进度条。
试试这个:
#!/bin/bash
echo -n 'Progress: [--------------------]'
sleep 1 # <- Command 1
echo -ne '\rProgress: [#####---------------]'
sleep 1 # <- Command 2
echo -ne '\rProgress: [##########----------]'
sleep 1 # <- Command 3
echo -ne '\rProgress: [###############-----]'
sleep 1 # <- Command 4
echo -e '\rProgress: [####################]'
许多解决方案最简单的方法是使用外部命令,如 pv
du -h /var | tail -2 | pv
更好的方法是在脚本中使用函数来显示条形图:
echo -ne '##### (33%)\r'
sleep 1
echo -ne '############# (66%)\r'
sleep 1
echo -ne '####################### (100%)\r'
echo -ne '\n'
或者使用滚动旋转更好:
sp='/-\|'
printf ' '
while true; do
printf '\b%.1s' "$sp"
sp=${sp#?}${sp%???}
done
您将任务作为背景启动,条形图 and/or 作为前景旋转
当然你在信号上设置条件或陷阱以在 task/command.
结束时停止它
我正在用一些命令编写 shell 脚本。我想给它添加一个进度条。
例如-
Command 1 #Progress bar at 25%
Command 2 #Progress bar at 50%
Command 3 #Progress bar at 75%
Command 4 #Progress bar at 100%
非常感谢任何帮助。
编辑:我正在使用 linux 的 Bash 终端,并希望在终端中显示进度条。
试试这个:
#!/bin/bash
echo -n 'Progress: [--------------------]'
sleep 1 # <- Command 1
echo -ne '\rProgress: [#####---------------]'
sleep 1 # <- Command 2
echo -ne '\rProgress: [##########----------]'
sleep 1 # <- Command 3
echo -ne '\rProgress: [###############-----]'
sleep 1 # <- Command 4
echo -e '\rProgress: [####################]'
许多解决方案最简单的方法是使用外部命令,如 pv
du -h /var | tail -2 | pv
更好的方法是在脚本中使用函数来显示条形图:
echo -ne '##### (33%)\r'
sleep 1
echo -ne '############# (66%)\r'
sleep 1
echo -ne '####################### (100%)\r'
echo -ne '\n'
或者使用滚动旋转更好:
sp='/-\|'
printf ' '
while true; do
printf '\b%.1s' "$sp"
sp=${sp#?}${sp%???}
done
您将任务作为背景启动,条形图 and/or 作为前景旋转 当然你在信号上设置条件或陷阱以在 task/command.
结束时停止它