如何在shell脚本中添加进度条?
在 bash 或 *NIX 中的任何其他 shell 中编写脚本时,如果运行的命令需要花费几秒钟的时间,则需要进度条。
例如,复制大文件、打开大 tar 文件。
您建议使用哪些方法向 shell 脚本添加进度条?
When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.
For example, copying a big file, opening a big tar file.
What ways do you recommend to add progress bars to shell scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
还没有看到任何类似的东西,而且这里的所有自定义函数似乎都只专注于渲染,所以...下面是我非常简单的 POSIX 兼容解决方案,并附有逐步说明,因为这个问题并不简单。
TL;DR
渲染进度条非常简单。 估计应该渲染多少内容是另一回事。 这是渲染(动画)进度条的方法 - 您可以将此示例复制并粘贴到文件中并运行它:
{1..20}
- 值从 1 到 20echo< /code> - 打印到终端(即到
stdout
)echo -n
- 打印时末尾不换行echo -e
- 解释特殊打印时的字符"\r"
- 回车符,返回行首的特殊字符您可以使其以任何速度渲染任何内容,因此此方法非常通用,例如经常用于愚蠢电影中“黑客攻击”的可视化,不是开玩笑。
完整答案(从零到工作示例)
问题的核心是如何确定
$i
值,即显示多少进度条。 在上面的示例中,我只是让它在for
循环中递增以说明原理,但现实生活中的应用程序将使用无限循环并在每次迭代时计算$i
变量。 为了进行上述计算,需要以下成分:在 cp 的情况下,它需要源文件的大小和源文件的大小目标文件:
foo=$(bar)
- 在子进程中运行bar
并将其stdout
保存到$foo
stat
- 将文件统计信息打印到stdout
stat -c
- 打印格式化值%s
- 总大小的格式在文件解包等操作的情况下,计算源大小稍微困难一些,但仍然与获取未压缩文件的大小一样简单:
gzip -l
- 打印有关 zip 存档的信息tail - n1
- 从底部开始使用 1 行tr -s ' '
- 将多个空格转换为一个(“挤压”它们)cut -d' ' -f3
> - 剪切第三个空格分隔的字段(列)这是我之前提到的问题的核心。 这个解决方案越来越不通用。 实际进度的所有计算都与您想要可视化的域紧密相关,是单个文件操作、计时器倒计时、目录中文件数量的增加、对多个文件的操作等,因此,它不能重复使用。 唯一可重用的部分是进度条渲染。 要重用它,您需要将其抽象并保存在文件中(例如
/usr/lib/progress_bar.sh
),然后定义计算特定于您的域的输入值的函数。 这就是通用代码的样子(我还使$BAR
动态化,因为人们要求它,其余的现在应该很清楚了):printf
- a用于以给定格式打印内容的内置函数printf %50s
- 不打印任何内容,只用 50 个空格填充tr ' ' '#'
- 将每个空格转换为井号这是如何使用它:
显然,您可以将其包装在一个函数中,重写以使用管道流,使用
$!
获取分叉进程 ID 并将其传递给progress_bar.sh
所以它可以猜测如何计算要做的工作和完成的工作,无论你的毒药是什么。旁注
我最常被问及这两件事:
${}
:在上面的示例中,我使用${foo:A:B}
。 此语法的技术术语是参数扩展,这是一种内置的 shell 功能,允许操作变量(参数),例如使用:
修剪字符串,但也可以做其他事情 - 它不会生成子 shell。 我能想到的关于参数扩展的最突出的描述(不完全兼容 POSIX,但可以让读者很好地理解这个概念)位于man bash
页面中。$()
:在上面的示例中我使用foo=$(bar)
。 它在子进程中生成一个单独的 shell(也称为 Subshell),在其中运行bar
命令并将其标准输出分配给$foo
多变的。 它与进程替换不同,并且与管道 (|
) 完全不同。 最重要的是,它有效。 有人说应该避免这种情况,因为它很慢。 我认为这在这里“没问题”,因为这段代码试图可视化的任何内容都会持续足够长的时间,需要一个进度条。 换句话说,子shell 不是瓶颈。 调用子 shell 还可以让我省去解释为什么return
与大多数人想象的不同、什么是退出状态以及为什么从 shell 中的函数传递值是这样的工作。一般来说,这不是 shell 函数所擅长的。 要了解更多信息,我再次强烈推荐man bash
页面。故障排除
如果您的 shell 实际上运行的是 sh 而不是 bash,或者是非常旧的 bash,例如默认的 osx,则它可能会因
echo -ne "\r${BAR:0:$i}"
而阻塞。 确切的错误是错误替换
。 如果您遇到这种情况,根据评论部分,您可以使用 echo -ne "\r$(expr "x$name" : "x.\{0,$num_skip\}\(.\{0 ,$num_keep\}\)")" 进行更便携的 posix 兼容/可读性较差的子字符串匹配。一个完整的、有效的 /bin/sh 示例:
Haven't seen anything similar and all custom functions here seem to focus on rendering alone so... my very simple POSIX compliant solution below with step by step explanations because this question isn't trivial.
TL;DR
Rendering the progress bar is very easy. Estimating how much of it should render is a different matter. This is how to render (animate) the progress bar - you can copy&paste this example to a file and run it:
{1..20}
- values from 1 to 20echo
- print to terminal (i.e. tostdout
)echo -n
- print without new line at the endecho -e
- interpret special characters while printing"\r"
- carriage return, a special char to return to the beginning of the lineYou can make it render any content at any speed so this method is very universal, e.g. often used for visualization of "hacking" in silly movies, no kidding.
Full answer (from zero to working example)
The meat of the problem is how to determine the
$i
value, i.e. how much of the progress bar to display. In the above example I just let it increment infor
loop to illustrate the principle but a real life application would use an infinite loop and calculate the$i
variable on each iteration. To make said calculation it needs the following ingredients:In case of
cp
it needs the size of a source file and the size of the target file:foo=$(bar)
- runbar
in a subprocess and save itsstdout
to$foo
stat
- print file stats tostdout
stat -c
- print a formatted value%s
- format for total sizeIn case of operations like file unpacking, calculating the source size is slightly more difficult but still as easy as getting the size of an uncompressed file:
gzip -l
- print info about zip archivetail -n1
- work with 1 line from the bottomtr -s ' '
- translate multiple spaces into one ("squeeze" them)cut -d' ' -f3
- cut 3rd space-delimited field (column)Here's the meat of the problem I mentioned before. This solution is less and less general. All calculations of the actual progress are tightly bound to the domain you're trying to visualize, is it a single file operation, a timer countdown, a rising number of files in a directory, operation on multiple files, etc., therefore, it can't be reused. The only reusable part is progress bar rendering. To reuse it you need to abstract it and save in a file (e.g.
/usr/lib/progress_bar.sh
), then define functions that calculate input values specific to your domain. This is how a generalized code could look like (I also made the$BAR
dynamic because people were asking for it, the rest should be clear by now):printf
- a builtin for printing stuff in a given formatprintf %50s
- print nothing but pad it with 50 spacestr ' ' '#'
- translate every space to hash signAnd this is how you'd use it:
Obviously you can wrap this in a function, rewrite to work with piped streams, grab forked process ID with
$!
and pass it toprogress_bar.sh
so it could guess how to calculate work to do and work done, whatever's your poison.Side notes
I get asked about these two things most often:
${}
: in above examples I use${foo:A:B}
. The technical term for this syntax is Parameter Expansion, a built-in shell functionality that allows to manipulate a variable (parameter), e.g. to trim a string with:
but also to do other things - it does not spawn a subshell. The most prominent description of parameter expansion I can think of (that isn't fully POSIX compatible but lets the reader understand the concept well) is in theman bash
page.$()
: in above examples I usefoo=$(bar)
. It spawns a separate shell in a subprocess (a.k.a. a Subshell), runs thebar
command in it and assigns its standard output to a$foo
variable. It's not the same as Process Substitution and it's something entirely different than pipe (|
). Most importantly, it works. Some say this should be avoided because it's slow. I argue this is "a okay" here because whatever this code is trying to visualise lasts long enough to require a progress bar. In other words, subshells are not the bottleneck. Calling a subshell also saves me the effort of explaining whyreturn
isn't what most people think it is, what is an Exit Status and why passing values from functions in shells is not what shell functions are good at in general. To find out more about all of it I, again, highly recommend theman bash
page.Troubleshooting
If your shell is actually running sh instead of bash, or really old bash, like default osx, it may choke on
echo -ne "\r${BAR:0:$i}"
. The exact error isBad substitution
. If this happens to you, per the comment section, you can instead useecho -ne "\r$(expr "x$name" : "x.\{0,$num_skip\}\(.\{0,$num_keep\}\)")"
to do a more portable posix-compatible / less readable substring match.A complete, working /bin/sh example:
雇用(浮点)进度条
序言
抱歉,这个不太短的答案。 在这个答案中,我将使用
整数
来渲染浮点,UTF-8
< /strong> 用于更精细地渲染进度条的字体,以及并行化
另一个任务(sha1sum
)为了跟随他的进展,所有这些都使用纯 bash 和 < em>没有分叉。
这里的所有演示都使用
read -t; && 中断
而不是睡眠
。 因此,通过按 Return 键可以很好地停止所有循环。简介
又一个 Bash 进度条...
由于这里已经有很多答案,我想添加一些关于 性能 的提示> 和精度。
1.避免分叉!
因为进度条的目的是在其他进程运行时运行,所以这一定是一个好的进程...
所以避免使用分叉 不需要时。 示例:而不是
使用
解释: 当您运行
var=$(command)
时,您启动一个新进程来执行command
并发送他的 <一旦终止,em>输出到变量$var
。 这是非常资源昂贵的。 请比较:在我的主机上,使用
$mysmiley
进行相同的分配工作(只需 2500 次),使用 fork 似乎比使用内置 fork 慢了 135 倍/贵了 135 倍。代码>printf -v。那么
Hires (floating point) progress bar
Preamble
Sorry for this not so short answer. In this answer I will use
integer
to render floating point,UTF-8
fonts for rendering progress bar more finely, andparallelise
another task (sha1sum
) in order to follow his progression, all of this with minimal resource footprint using pure bash and no forks.All demos here use
read -t <float seconds> && break
instead ofsleep
. So all loop could be nicely stopped by hitting Return key.Introduction
Yet Another Bash Progress Bar...
As there is already a lot of answer here, I want to add some hints about performances and precision.
1. Avoid forks!
Because a progress bar are intented to run while other process are working, this must be a nice process...
So avoid using forks when not needed. Sample: instead of
Use
Explanation: When you run
var=$(command)
, you initiate a new process to executecommand
and send his output to variable$var
once terminated. This is very resource expensive. Please compare:On my host, same work of assigning
$mysmiley
(just 2500 time), seem ~135x slower / more expensive by using fork than by using built-inprintf -v
.Then
So your
function
have to not print (or output) anything. Your function have to attribute his answer to a variable.2. Use integer as pseudo floating point
Here is a very small and quick function to compute percents from integers, with integer and answer a pseudo floating point number:
Usage:
3. Hires console graphic using UTF-8:
▏ ▎ ▍ ▌ ▋ ▊ ▉ █
To render this characters using bash, you could:
or
Then we have to use 8x
string width
asgraphic width
.Now do it!
This function is named
percentBar
because it render a bar from argument submited in percents (floating):Usage:
To show little differences:
With colors
As rendered variable is a fixed widht string, using color is easy:
Little animation:
Last animated demo
Another demo showing different sizes and colored output:
Could produce something like this:
Practical GNU/Linux sample 1: kind of
sleep
with progress barRewrite feb 2023: Turn into more usefull
displaySleep
function suitable to use as displayed timeout read:This sleep show a progress bar with 50 refresh by seconds (tunnable)
This will keep current cursor position to fill only the rest of line (full line if current cursor position is 1). This could be useful for displaying some kind of prompt:
Practical GNU/Linux sample 2:
sha1sum
with progress barUnder linux, you could find a lot of usefull infos under
/proc
pseudo filesystem, so using previoulsy defined functionspercentBar
andpercent
, here issha1progress
:Of course,
25 ms
timeout mean approx 40 refresh per second. This could look overkill, but work fine on my host, and anyway, this can be tunned.Explanation:
exec {sha1in}<
create a new file descriptor for the output of<( ... )
forked task run in backgroundsha1sum -b - <"$1"
ensuring input came from STDIN (fd/0
)while ! read -ru $sha1in -t .025 sha1res _
While no input read from subtask, in25 ms
.../proc/$sha1pid/fdinfo/0
kernel variable showing information about file descriptor 0 (STDIN) of task$sha1pid
APT 样式进度条(不会破坏正常输出)
编辑:有关更新版本,请检查我的 github页
我对这个问题的回答不满意。 我个人想要的是 APT 所看到的一个精美的进度条。
我查看了 APT 的 C 源代码,并决定为 bash 编写自己的等效代码。
此进度条将很好地保持在终端底部,并且不会干扰发送到终端的任何输出。
请注意,该进度条当前固定为 100 个字符宽。 如果你想将其缩放到终端的大小,这也很容易完成(我的 github 页面上的更新版本可以很好地处理这个问题)。
我将在这里发布我的脚本。
用法示例:
脚本(我强烈推荐我的 github 上的版本):
APT style progress bar (Does not break normal output)
EDIT: For an updated version check my github page
I was not satisfied with the responses on this question. What I was personally looking for was a fancy progress bar as is seen by APT.
I had a look at the C source code for APT and decided to write my own equivalent for bash.
This progress bar will stay nicely at the bottom of the terminal and will not interfere with any output sent to the terminal.
Please do note that the bar is currently fixed at 100 characters wide. If you want scale it to the size of the terminal, this is fairly easy to accomplish as well (The updated version on my github page handles this well).
I will post my script here.
Usage example:
The script (I strongly recommend the version on my github instead):
GNU tar 有一个有用的选项,它提供了以下功能:一个简单的进度条。
(...)另一个可用的检查点操作是“点”(或“.”)。 它指示 tar 在标准列表流上打印一个点,例如:
通过以下方式可以获得相同的效果:
GNU tar has a useful option which gives a functionality of a simple progress bar.
(...) Another available checkpoint action is ‘dot’ (or ‘.’). It instructs tar to print a single dot on the standard listing stream, e.g.:
The same effect may be obtained by:
一种更简单的方法,可以使用 pipelineview ( pv ) 实用程序在我的系统上运行。
A simpler method that works on my system using the pipeview ( pv ) utility.
下面是它的外观
上传文件
等待作业完成
实现它的简单函数
您只需将其复制粘贴到脚本中即可。 它不需要任何其他东西就可以工作。
使用示例
在这里,我们上传一个文件并在每次迭代时重绘进度条。 只要我们能得到两个值:最大值和当前值,实际执行什么工作并不重要。
在下面的示例中,最大值为
file_size
,当前值由某个函数提供,称为uploaded_bytes
。Here is how it might look
Uploading a file
Waiting for a job to complete
Simple function that implements it
You can just copy-paste it to your script. It does not require anything else to work.
Usage example
Here, we upload a file and redraw the progress bar at each iteration. It does not matter what job is actually performed as long as we can get 2 values: max value and current value.
In the example below the max value is
file_size
and the current value is supplied by some function and is calleduploaded_bytes
.这可以让您直观地看到命令仍在执行:
这将创建一个在后台执行并回显“.”的无限 while 循环。 每一秒。 这将在 shell 中显示
.
。 运行tar
命令或任何您想要的命令。 当该命令完成执行后,杀死后台运行的最后一个作业 - 这是无限 while 循环。This lets you visualize that a command is still executing:
This will create an infinite while loop that executes in the background and echoes a "." every second. This will display
.
in the shell. Run thetar
command or any a command you want. When that command finishes executing then kill the last job running in the background - which is the infinite while loop.我需要一个进度条来迭代 csv 文件中的行。 能够将 cprn 的代码改编成对我有用的东西:
看起来像这样:
I needed a progress bar for iterating over the lines in a csv file. Was able to adapt cprn's code into something useful for me:
Looks like this:
大多数 UNIX 命令不会为您提供可以执行此操作的直接反馈。
有些会在 stdout 或 stderr 上提供可供您使用的输出。
对于像 tar 这样的东西,您可以使用 -v 开关并将输出通过管道传输到一个程序,该程序会为其读取的每一行更新一个小动画。 当 tar 写出一个已解开的文件列表时,程序可以更新动画。 要计算完成百分比,您必须知道文件数量并计算行数。
据我所知, cp 没有给出这种输出。 要监视 cp 的进度,您必须监视源文件和目标文件并观察目标的大小。 您可以使用 stat (2) 系统调用编写一个小型 c 程序来获取文件尺寸。 这将读取源的大小,然后轮询目标文件并根据迄今为止写入的文件的大小更新完成百分比栏。
Most unix commands will not give you the sort of direct feedback from which you can do this.
Some will give you output on stdout or stderr that you can use.
For something like tar you could use the -v switch and pipe the output to a program that updates a small animation for each line it reads. As tar writes out a list of files it's unravelled the program can update the animation. To do a percent complete you would have to know the number of files and count the lines.
cp doesn't give this sort of output as far as I know. To monitor the progress of cp you would have to monitor the source and destination files and watch the size of the destination. You could write a small c program using the stat (2) system call to get the file size. This would read the size of the source then poll the destination file and update a % complete bar based on the size of the file written to date.
根据 Edouard Lopez 的工作,我创建了一个适合屏幕大小的进度条,无论屏幕大小如何。 一探究竟。
它也发布在 Git Hub 上。
享受
Based on the work of Edouard Lopez, I created a progress bar that fits the size of the screen, whatever it is. Check it out.
It's also posted on Git Hub.
Enjoy
我需要一个适合弹出气泡消息 (
notify-send
) 的进度条来表示电视音量级别。 最近我一直在用python写一个音乐播放器,电视画面大部分时间都是关闭的。终端的示例输出
Bash 脚本
测试 bash 脚本
使用此脚本测试终端中的进度条。
TL;DR
本节详细介绍了如何使用
notify-send
将垃圾弹出气泡消息快速发送到桌面。 这是必需的,因为音量级别每秒可能会改变多次,并且默认的气泡消息行为是消息在桌面上停留很多秒。弹出气泡消息示例
弹出气泡消息 bash 代码
从上面的脚本中,
main
函数被复制到名为VolumeBar
的现有 bash 脚本中的新函数电视供电
。 复制的main
函数中的exit 0
命令已被删除。以下是如何调用它并让 Ubuntu 的
notify-send
命令知道我们将向弹出气泡消息发送垃圾邮件:这是新行,它告诉
notify-send
立即替换最后一个弹出窗口bubble:volume
将弹出的气泡消息分组在一起,该组中的新消息立即替换之前的消息。 您可以使用anything
代替volume
。I needed a progress bar that would fit in popup bubble message (
notify-send
) to represent TV volume level. Recently I've been writing a music player in python and the TV picture is turned off most of the time.Sample output from terminal
Bash script
Test bash script
Use this script to test the progress bar in the terminal.
TL;DR
This section details how
notify-send
is used to quickly spam popup bubble messages to the desktop. This is required because volume level can change many times a second and the default bubble message behavior is for a message to stay on the desktop for many seconds.Sample popup bubble message
Popup bubble message bash code
From the script above the
main
function was copied to a new functioned calledVolumeBar
in an existing bash script calledtvpowered
. Theexit 0
command in the copiedmain
function was removed.Here's how to call it and let Ubuntu's
notify-send
command know we will be spamming popup bubble message:This is the new line which tells
notify-send
to immediately replace last popup bubble:volume
groups the popup bubble messages together and new messages in this group immediately replaces the previous. You can useanything
instead ofvolume
.我的解决方案显示了 tarball 的百分比
目前正在解压缩和写入。 我用这个
写出 2GB 根文件系统映像时。 你真的
这些事情需要一个进度条。 我所做的是使用
gzip --list
获取未压缩的总大小压缩包。 由此我计算出所需的阻塞因子
将文件分为 100 个部分。 最后,我打印一个
每个块的检查点消息。 对于 2GB 文件来说
每个块大约有 10MB。 如果太大了那么你可以
将 BLOCKING_FACTOR 除以 10 或 100,然后就可以了
更难以百分比形式打印漂亮的输出。
假设您使用的是 Bash,那么您可以使用
以下 shell 函数
My solution displays the percentage of the tarball that
is currently being uncompressed and written. I use this
when writing out 2GB root filesystem images. You really
need a progress bar for these things. What I do is use
gzip --list
to get the total uncompressed size of thetarball. From that I calculate the blocking-factor needed
to divide the file into 100 parts. Finally, I print a
checkpoint message for each block. For a 2GB file this
gives about 10MB a block. If that is too big then you can
divide the BLOCKING_FACTOR by 10 or 100, but then it's
harder to print pretty output in terms of a percentage.
Assuming you are using Bash then you can use the
following shell function
首先bar并不是唯一的一个管道进度表。 另一个(可能更广为人知)是 pv(管道查看器)。
其次, bar 和 pv 可以像这样使用:
甚至:
如果您想在处理参数中给出的文件的命令中使用 bar 和 pv ,例如复制 file1 file2 ,一个有用的技巧是使用 进程替换:
进程替换是一个 bash 魔法,它会创建临时 fifo 管道文件 / dev/fd/ 并通过此管道从运行的进程(括号内)连接 stdout,并且复制看到它就像普通文件一样(有一个例外,它只能向前读取)。
更新:
bar 命令本身也允许复制。 在 man bar 之后:
但在我看来,进程替换是更通用的方法。 它使用 cp 程序本身。
First of all bar is not the only one pipe progress meter. The other (maybe even more known) is pv (pipe viewer).
Secondly bar and pv can be used for example like this:
or even:
one useful trick if you want to make use of bar and pv in commands that are working with files given in arguments, like e.g. copy file1 file2, is to use process substitution:
Process substitution is a bash magic thing that creates temporary fifo pipe files /dev/fd/ and connect stdout from runned process (inside parenthesis) through this pipe and copy sees it just like an ordinary file (with one exception, it can only read it forwards).
Update:
bar command itself allows also for copying. After man bar:
But process substitution is in my opinion more generic way to do it. An it uses cp program itself.
许多答案描述了编写自己的命令来打印
'\r' + $some_sort_of_progress_msg
。 有时问题是每秒打印数百个更新会减慢该过程。但是,如果您的任何进程产生输出(例如
7z a -r newZipFile myFolder
将在压缩时输出每个文件名),则存在更简单、快速、轻松且可定制的解决方案。安装 python 模块 tqdm。
帮助:
tqdm -h
。 使用更多选项的示例:作为奖励,您还可以使用 tqdm 将可迭代对象包装在 python 代码中。
https://github.com/tqdm/tqdm/blob/master/README .rst#module
Many answers describe writing your own commands for printing out
'\r' + $some_sort_of_progress_msg
. The problem sometimes is that printing out hundreds of these updates per second will slow down the process.However, if any of your processes produce output (eg
7z a -r newZipFile myFolder
will output each filename as it compresses it) then a simpler, fast, painless and customisable solution exists.Install the python module
tqdm
.Help:
tqdm -h
. An example using more options:As a bonus you can also use
tqdm
to wrap iterables in python code.https://github.com/tqdm/tqdm/blob/master/README.rst#module
我更喜欢将 dialog 与 --gauge 参数一起使用。 在许多发行版的 .deb 软件包安装和其他基本配置内容中经常使用。 所以你不需要重新发明轮子...
只需输入一个从 1 到 100 @stdin 的 int 值。 一个基本且愚蠢的例子:
我有这个 /bin/Wait 文件(带有 chmod u+x perms)用于烹饪目的:P
所以我可以输入:
等待“34 分钟”“预热烤箱”
或
等待“12 月 31 日”“新年快乐”
I prefer to use dialog with the --gauge param. Is used very often in .deb package installations and other basic configuration stuff of many distros. So you don't need to reinvent the wheel... again
Just put an int value from 1 to 100 @stdin. One basic and silly example:
I have this /bin/Wait file (with chmod u+x perms) for cooking purposes :P
So I can put:
Wait "34 min" "warm up the oven"
or
Wait "dec 31" "happy new year"
这仅适用于使用 gnome zenity。 Zenity 为 bash 脚本提供了一个很棒的本机接口:
https://help.gnome.org/users/zenity/stable/
来自 Zenity 进度条示例:
This is only applicable using gnome zenity. Zenity provides a great native interface to bash scripts:
https://help.gnome.org/users/zenity/stable/
From Zenity Progress Bar Example:
要指示活动的进度,请尝试以下命令:
OR
OR
OR
人们可以在while 循环检查并显示进度值/程度。
To indicate progress of activity, try the following commands:
OR
OR
OR
One can use flags/variables inside the while loop to check and display the value/extent of progress.
https://github.com/extensionsapp/progre.sh
创建 40% 的进度:
progreSh 40
https://github.com/extensionsapp/progre.sh
Create 40 percent progress:
progreSh 40
它可以通过一种非常简单的方式实现:
for
循环从 0 迭代到 100,$bar
变量附加到$bar
变量上>= 符号使进度条更宽\r
清理行并返回到行的开头;-ne
使 < code>echo 不会在末尾添加换行符并解析\r
特殊字符)彩色进度条
要使进度条彩色,您可以使用格式转义序列 - 这里进度条是黄色的:
\e[43m
,然后我们用\e[0m
重置自定义设置,否则即使进度条也会影响进一步的输入酒吧完成了。It may be achieved in a pretty simple way:
for
loop$bar
variable another=
sign to make the progress bar wider\r
cleans line and returns to the beginning of the line;-ne
makesecho
doesn't add newline at the end and parses\r
special character)COLORED PROGRESS BAR
To make a progress bar colorful, you can use formatting escape sequence - here the progress bar is yellow:
\e[43m
, then we reset custom settings with\e[0m
, otherwise it would affect further input even when the progress bar is done.对我来说,迄今为止最容易使用且最好看的是命令
pv
或bar
就像有人已经写过的那样,例如:需要使用
dd 备份整个驱动器
通常你使用
dd if="$input_drive_path" of="$output_file_path"
和
pv
你可以这样做:dd if=" $input_drive_path"| 光伏 | dd of="$output_file_path"
并且进度直接转到
STDOUT
,如下所示:完成后会出现摘要
for me easiest to use and best looking so far is command
pv
orbar
like some guy already wrotefor example: need to make a backup of entire drive with
dd
normally you use
dd if="$input_drive_path" of="$output_file_path"
with
pv
you can make it like this :dd if="$input_drive_path" | pv | dd of="$output_file_path"
and the progress goes directly to
STDOUT
as this:after it is done summary comes up
我使用了 在 shell 脚本中创建重复字符的字符串 对于字符重复。 我有两个相对较小的 bash 版本,用于需要显示进度条的脚本(例如,遍历许多文件的循环,但对于大 tar 文件或复制操作没有用)。 更快的一个由两个函数组成,一个为条形显示准备字符串:
另一个为显示进度条:
它可以用作:
这意味着为条形准备带有 50 个“#”字符的字符串,然后:
将显示对应于 35/80 比例的“#”字符数:
请注意,该函数会在同一行上一遍又一遍地显示条形,直到您(或其他程序)打印换行符。 如果您将 -1 作为第一个参数,则进度条将被删除:
较慢的版本全部在一个函数中:
并且它可以用作(与上面相同的示例):
如果您需要 stderr 上的进度条,只需添加
>&2
在每个 printf 命令的末尾。I used an answer from Creating string of repeated characters in shell script for char repeating. I have two relatively small bash versions for scripts that need to display progress bar (for example, a loop that goes through many files, but not useful for big tar files or copy operations). The faster one consists of two functions, one to prepare the strings for bar display:
and one to display a progress bar:
It could be used as:
which means prepare strings for bar with 50 "#" characters, and after that:
will display the number of "#" characters that corresponds to 35/80 ratio:
Be aware that function displays the bar on the same line over and over until you (or some other program) prints a newline. If you put -1 as first parameter, the bar would be erased:
The slower version is all in one function:
and it can be used as (the same example as above):
If you need progressbar on stderr, just add
>&2
at the end of each printf command.根据上面列出的建议,我决定实现我自己的进度条。
Using suggestions listed above, I decided to implement my own progress bar.
灵活的版本,具有随机颜色、可操作的字符串和日期。
Flexible version with randomized colors, a string to manipulate and date.
Usage:
output example:
我利用以下优势为嵌入式系统制作了一个纯 shell 版本:
/usr/bin/dd 的 SIGUSR1 信号处理功能。
基本上,如果您发送“kill SIGUSR1 $(pid_of_running_dd_process)”,它会输出
吞吐量速度和传输量的摘要。
后台 dd,然后定期查询它的更新,并生成
哈希标记就像老式 ftp 客户端所使用的那样。
使用 /dev/stdout 作为非标准输出友好程序(如 scp)的目标
最终结果允许您进行任何文件传输操作并获取看起来像老式 FTP“哈希”输出的进度更新,您只需为每个 X 字节获取一个哈希标记。
这很难说是生产质量的代码,但你明白了。 我觉得很可爱。
无论如何,实际的字节数可能无法正确反映在散列数中 - 根据舍入问题,您可能会有更多或更少的散列数。 不要将其用作测试脚本的一部分,它只是美观而已。 而且,是的,我知道这是非常低效的 - 这是一个 shell 脚本,我不会为此道歉。
最后提供了 wget、scp 和 tftp 的示例。 它应该适用于任何发出数据的东西。 确保对标准输出不友好的程序使用 /dev/stdout。
例子:
I did a pure shell version for an embedded system taking advantage of:
/usr/bin/dd's SIGUSR1 signal handling feature.
Basically, if you send a 'kill SIGUSR1 $(pid_of_running_dd_process)', it'll output
a summary of throughput speed and amount transferred.
backgrounding dd and then querying it regularly for updates, and generating
hash ticks like old-school ftp clients used to.
Using /dev/stdout as the destination for non-stdout friendly programs like scp
The end result allows you to take any file transfer operation and get progress update that looks like old-school FTP 'hash' output where you'd just get a hash mark for every X bytes.
This is hardly production quality code, but you get the idea. I think it's cute.
For what it's worth, the actual byte-count might not be reflected correctly in the number of hashes - you may have one more or less depending on rounding issues. Don't use this as part of a test script, it's just eye-candy. And, yes, I'm aware this is terribly inefficient - it's a shell script and I make no apologies for it.
Examples with wget, scp and tftp provided at the end. It should work with anything that has emits data. Make sure to use /dev/stdout for programs that aren't stdout friendly.
Examples:
您可以通过覆盖一行来实现这一点。 使用
\r
返回到行的开头,而不将\n
写入终端。完成该行后,请写入
\n
。使用
echo -ne
可以:\n
并\r
等转义序列。这是一个演示:
在下面的评论中,puk 提到如果您从长行开始然后想写短行,则会“失败”:在这种情况下,您需要覆盖长行的长度(例如,带空格)。
You can implement this by overwriting a line. Use
\r
to go back to the beginning of the line without writing\n
to the terminal.Write
\n
when you're done to advance the line.Use
echo -ne
to:\n
and\r
.Here's a demo:
In a comment below, puk mentions this "fails" if you start with a long line and then want to write a short line: In this case, you'll need to overwrite the length of the long line (e.g., with spaces).
您可能还对如何制作旋转器感兴趣:
我可以在 Bash 中制作旋转器吗?
You may also be interested in how to do a spinner:
Can I do a spinner in Bash?
得到了我前几天写的一个简单的进度条功能:
或者从中获取它,
https://github.com/fearside/ProgressBar/
Got an easy progress bar function that i wrote the other day:
Or snag it from,
https://github.com/fearside/ProgressBar/
使用 Linux 命令
pv
。它不知道它是否位于管道中间的大小,但它给出了速度和总数,从那里您可以计算出应该花费多长时间并获得反馈,以便您知道它没有挂起。
Use the Linux command
pv
.It doesn't know the size if it's in the middle of the pipeline, but it gives a speed and total, and from there you can figure out how long it should take and get feedback so you know it hasn't hung.
我正在寻找比所选答案更性感的东西,我自己的剧本也是如此。
预览
来源
我把它放在 github
progress -bar.sh
用法
I was looking for something more sexy than the selected answer, so did my own script.
Preview
Source
I put it on github
progress-bar.sh
Usage
一些帖子展示了如何显示命令的进度。 为了计算它,您需要查看您已经进步了多少。 在 BSD 系统上,某些命令(例如 dd(1))接受
SIGINFO
信号,并报告其进度。 在 Linux 系统上,某些命令的响应类似于SIGUSR1
。 如果此功能可用,您可以通过 _dd 管道输入以监视已处理的字节数。或者,您可以使用 lsof 来获取文件的偏移量读取指针,从而计算进度。
以下是使用 lsof(1) 查看 wc(1) 读取名为
blob
的大文件的进度的示例。我编写了一个名为 pmonitor 的命令,该命令显示处理指定的进程或文件。 使用它您可以执行以下操作。
Linux 和 FreeBSD shell 脚本的早期版本出现在我的博客(“Monitor Process Progress on Unix ”)。
Some posts have showed how to display the command's progress. In order to calculate it, you'll need to see how much you've progressed. On BSD systems some commands, such as dd(1), accept a
SIGINFO
signal, and will report their progress. On Linux systems some commands will respond similarly toSIGUSR1
. If this facility is available, you can pipe your input through _dd to monitor the number of bytes processed.Alternatively, you can use lsof to obtain the offset of the file's read pointer, and thereby calculate the progress.
Here is an example of using lsof(1) to see the progress of wc(1) reading a large file named
blob
.I've written a command, named pmonitor, that displays the progress of processing a specified process or file. With it you can do things, such as the following.
An earlier version of Linux and FreeBSD shell scripts appears on my blog ("Monitor Process Progress on Unix").