Linux Bash iteration over a progress bar folder

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

Here is the iteration:

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

Having a counter and knowing the number of folders is the solution. But I'm looking for a more general and shorter solution ...

Hope someone would have an idea. Thank you for your interest.

Julien

Edit:

I get this solution that does what I want, and really graphically:

 #!/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 

However, I will continue with fedorqui's solution, which will not take up the entire screen.

Thanks so much for your time.

+6
source share
3 answers

According to the results published in How to print on the same line, overriding the previous line? I came up 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 

Example

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

Based on the amazing Fedorqui solution, I created a function that works great with any loop.

 function redraw_progress_bar { # int barsize, int base, int i, int top local barsize=$1 local base=$2 local current=$3 local top=$4 local j=0 local progress=$(( ($barsize * ( $current - $base )) / ($top - $base ) )) echo -n "[" for ((j=0; j < $progress; j++)) ; do echo -n '='; done echo -n '=>' for ((j=$progress; j < $barsize ; j++)) ; do echo -n ' '; done echo -n "] $(( $current )) / $top " $'\r' } 

This way you can easily make for loops such as

 for (( i=4; i<=20 ; i+=2 )) do redraw_progress_bar 50 4 $i 20 $something $i done echo $'\n' 
+4
source

If you want a graphical progress bar (GTK +), take a look at zenity:

 #!/bin/bash ( for i in {0..5} do echo $((i*25)) echo "#Processing $((i+1))/5" sleep 1 done ) | zenity --progress --width=400 --title="Please wait" --auto-close 
+3
source

Source: https://habr.com/ru/post/954760/


All Articles