<分区>
仅使用 bash 脚本,如何提供 bash 进度指示器?
例如,当我从 bash 运行命令时 - 当该命令正在执行时 - 让用户知道某些事情仍在发生。
<分区>
仅使用 bash 脚本,如何提供 bash 进度指示器?
例如,当我从 bash 运行命令时 - 当该命令正在执行时 - 让用户知道某些事情仍在发生。
最佳答案
在这个使用 SCP 的示例中,我演示了如何获取进程 ID (pid),然后在该进程运行时执行某些操作。
这会显示一个简单的旋转图标。
/usr/bin/scp me@website.com:file somewhere 2>/dev/null &
pid=$! # Process Id of the previous running command
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
echo -n "[copying] ${spin[0]}"
while [ kill -0 $pid ]
do
for i in "${spin[@]}"
do
echo -ne "\b$i"
sleep 0.1
done
done
William Pursell 的解决方案
/usr/bin/scp me@website.com:file somewhere 2>/dev/null &
pid=$! # Process Id of the previous running command
spin='-\|/'
i=0
while kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r${spin:$i:1}"
sleep .1
done
关于bash - 使用 Bash 显示进度指示器(微调器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498304/