The example you post actually have an issue of performance, since each loop will unzip the same file repeatedly, maybe you should first change the structure of the loop? The answer maybe very different if the structure changed.
However, for the general case of the problem, the showing percentage part
, I recommend using awk.
Option 1: Show for Loop
percentage
Calculate percentage for the for loop
's current iteration / total iterations
.
ALL=$(cat all-programs.txt)
LEN=${#ALL[@]}
for ((i = 0; i < $LEN; ++i)); do
awk -v total=$LEN -v cur=$i '
function bar(x){s="";i=0; while (i++ < x) s=s "#";return s}
BEGIN{
percent=int(cur / total * 100);
printf "%s %s%%\r", bar(percent*.8), percent
}
END { print }
'
done
Option 2: Show per file
percentage
Calculate percentage for per file current length / total size
, for the case showing percentage of ver_big_file.json
progress.
for domain in $(cat all-programs.txt); do
awk '
function bar(x){s="";i=0;while (i++ < x) s=s "#";return s}
BEGIN{
("ls -l " ARGV[1]) | getline total;
split(total,array);
total=array[5];
}
{
cur+=length($0)+1;
percent=int(cur / total * 100);
printf "LINE %s:%s %s%%\r", NR, bar(percent*.8), percent
}
END {print}' very_big_file.json | grep ".$domain" | ...
done
You can combine Option 1 and Option 2 together as you need.