0

I want to add progress bar to a script currently i am running this command from the terminal

for line in $(ls -1 | grep .mkv); do merge $line; done

it lists all the mkv files in the working directory and merges them with another audio file. The merge is handled by another mkvmerge script. Now i wan to make a script out of this such that it takes a directory and runs that merge script on the mkv files, this i can do but i also want to add a progress bar which shows how many videos have been processed and display any errors..maybe turn progress bar red if any errors, keep it gray while working and turn it green if it completed without any errors.

i did a google search for progress bars in bash but i am not able to adapt them to this code.

https://unix.stackexchange.com/questions/415421/linux-how-to-create-simple-progress-bar-in-bash https://www.linuxjournal.com/content/how-add-simple-progress-bar-shell-script How to add a progress bar to a shell script?

most of them are assuming the number of times the task will be done is fixed but in this case it isn't. The folder can have any number of mkv files. i can get the number of files before hand using wc -l but how do i get the number of the file that is being worked on? here's the how it currently looks if that helps

pop-os:~/HDD/newdownloads/Doggy_Day_School/Season_1$ for line in $(ls -1 | grep .mkv); do merge $line; done
Error: The file 'S1E18-Episode_18_-_Rosie_s_Gift.1080p.en.Zee5.Web-dl-A99mus.m4a' could not be opened for reading: open file error.
Error: The file 'S1E20-Episode_20_-_Something_s_Fishy.1080p.en.Zee5.Web-dl-A99mus.m4a' could not be opened for reading: open file error.
Error: The file 'S1E24-Episode_24_-_Let_s_get_ready.1080p.en.Zee5.Web-dl-A99mus.m4a' could not be opened for reading: open file error.
Error: The file 'S1E8-Episode_8_-_Pedro_s_Secret.1080p.en.Zee5.Web-dl-A99mus.m4a' could not be opened for reading: open file error.

1 Answers1

1

Maybe rather than a progress bar, just a status.

lst=( *.mks )
ndx=0
echo "Processing ${#lst[@]} *.mks files -"
while (( ndx < ${#lst[@]} ))
do printf "\ron %05d of %05d" $ndx ${#lst[@]}
    merge ${file[ndx++]}
done
printf "\n\nDone\n"

This will throw a lot of output to the screen, but \r should cause each line to overwrite on the screen, making it look like it's updating in place.

Also, don't parse ls.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36