使用bash显示进度(工作)指示灯 [英] Using BASH to display a progress (working) indicator

查看:108
本文介绍了使用bash显示进度(工作)指示灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一个bash脚本只,你怎么能提供一个bash进度指示器?

Using a bash only script, how can you provide a bash progress indicator?

所以我可以运行一个命令形式的bash,虽然该命令执行时让用户知道的东西还是发生了。

So I can run a command form bash, and while that command is executing let the user know that something is still happening.

推荐答案

在这个例子中使用SCP,我演示了如何抓住进程ID(PID),然后做一些事情而进程正在运行。

In this example using SCP, I'm demonstrating how to grab the process id (pid) and then do something while that process is running.

这显示一个简单的spinnng图标。

This displays a simple spinnng icon.

/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


威廉Pursell的解决方案


William Pursell's solution

/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显示进度(工作)指示灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆