bash - 使用 Bash 显示进度指示器(微调器)

标签 bash shell unix progress

<分区>

仅使用 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/

相关文章:

macos - 命令行中转换命令的错误

bash - 在 bash 脚本中使用 pinentry-tty (如 read)

mysql - 从终端或 shell 脚本制作 neo4j 命令

javascript - 如何给这个纪元添加一毫秒的时间?

linux - 将文件名添加到linux中的空文件

bash - 如何grep包含下划线的关键字?

bash - 如何使用 djpeg 和 pnmtopng 将 jpeg 转换为 png?

linux - 脚本转到文件夹的每个子目录并运行命令

c - Ctrl-D(Unix)和Ctrl-Z(Windows)的不同行为

unix - 命令行将curl连接到我的应用程序使用socket()打开的整数套接字