yae*_*ael 4 linux bash shell-script graphics
我想在我的 bash 脚本中添加一个显示“.”的进度条。字符作为进度,进程在 MAX 180 秒后结束
在 bash 脚本中,我使用 curl 命令,因此 curl 在一段时间后给出结果但不超过 180 秒
类似的东西
|. after 2 sec
|........... after 60 sec
|................... after 100 sec
|.......................... after 150 sec
|................................| after 180 sec
Run Code Online (Sandbox Code Playgroud)
最后一个例子
|................................| after 180 sec
Run Code Online (Sandbox Code Playgroud)
或者
|....| after 30 sec
Run Code Online (Sandbox Code Playgroud)
ilk*_*chu 10
这在普通的 Bash 中相当简单:
#!/bin/bash
# progress bar function
prog() {
local w=80 p=$1; shift
# create a string of spaces, then change them to dots
printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /.};
# print those dots on a fixed-width space plus the percentage etc.
printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*";
}
# test loop
for x in {1..100} ; do
prog "$x" still working...
sleep .1 # do some work here
done ; echo
Run Code Online (Sandbox Code Playgroud)
第一个参数prog
是百分比,任何其他参数都打印在进度条之后。w
函数中的变量控制条的宽度。完成后打印换行符,该函数不打印换行符。
另一种可能性是使用该pv
工具。它用于测量管道的吞吐量,但我们可以为它创建一个:
for x in {1..100} ; do
sleep .1 # do some work here
printf .
done | pv -pt -i0.2 -s100 -w 80 > /dev/null
Run Code Online (Sandbox Code Playgroud)
在这里,-pt
启用进度条和计时器,-s 100
设置总输出大小,我们在函数内打印的任何内容都计入该大小。
归档时间: |
|
查看次数: |
32643 次 |
最近记录: |