本文介绍了Linux的Bash的遍历文件夹的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本来处理的文件夹。
运行时间相当长,所以我想补充一个进度条。

I am writing a little script to process folders.The runtime time is quite long so I would like to add a progress bar.

下面是迭代:

for file in */
do 
    #processing here, dummy code
    sleep 1
done

有一个计数器,并知道文件夹的数目将是一个解决方案。
但是,我要寻找一个更通用和较短的解决方案...

Having a counter and knowing the number of folders would be a solution.But I am looking for a more generic and a shorter solution...

我希望有人能有一个想法。
感谢您的关注,

I hope someone would have an idea.Thank you for your interest,

朱利安

编辑:

我得到这样的解决方案,它做我想做的,而且是真正的图形:

I get this solution which do what I want, and is really graphical :

#!/bin/bash
n_item=$(find /* -maxdepth 0 | wc -l)
i=0
for file in /*
do
    sleep 1 #process file
    i=$((i+1))
    echo $((100 * i / n_item)) | dialog --gauge "Processing $n_item folders, the current is $file..." 10 70 0
done

不过,我会继续fedorqui的解决方案,它并没有把所有的屏幕。

However, I will keep fedorqui 's solution which doesn't take all the screen.

非常感谢您的宝贵时间。

Thank you very much for your time

推荐答案

根据我们张贴在<一个结果href=\"http://stackoverflow.com/questions/18017256/how-to-print-out-to-the-same-line-overriding-$p$pvious-line\">How打印出的同一行,覆盖previous线我对这个结果来了?

Based on the results we posted in How to print out to the same line, overriding previous line? I came with this result:

#!/bin/bash

res=$(find /* -maxdepth 0 | wc -l)
echo "found $res results"
i=1

for file in /*
do
    echo -n "["
    for ((j=0; j<i; j++)) ; do echo -n ' '; done
    echo -n '=>'
    for ((j=i; j<$res; j++)) ; do echo -n ' '; done
    echo -n "] $i / $res $file" $'\r'
    ((i++))
    sleep 1
done

示例

$ ./a
found 26 results
[  =>                        ] 2 / 26 /boot 
[                =>          ] 16 / 26 /root

这篇关于Linux的Bash的遍历文件夹的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:02