Count number of occurrences in a for in loop {Bash}

Asked

Viewed 325 times

0

I have the following script

    #!/bin/bash
for file in *.jpg;
do
        convert $file -resize 1920x1080! -blur 0x8 alterado$file;
        echo "A Processar o ficheiro $file"
done

I want to do an echo with

Processor the foo file ( 1 of 100)

How do I get the index and total number of occurrences? I could do another script to list the directory files and count the result, but I would like to know how to do this with loop for ... in

1 answer

1


You can do it:

#!/bin/bash

total=$(ls -l *.jpg | wc -l);
contador=1;

for file in *.jpg;
do
        echo "A Processar o ficheiro $file ($contador de $total)";
        convert $file -resize 1920x1080! -blur 0x8 alterado$file;            
        contador=$((contador+1));
done

Explanation:

The total variable is obtained by executing a command that counts how many *.jpg type files are in the current directory.

The counter variable is used to show the progress. It is incremented at each end of the loop.

  • @lazyFox, I didn’t quite understand what you want. Could clarify better?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.