Count number of occurrences in a for loop {Bash}

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 echo with

  

The processor the xpto file (1 of 100)

How can 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 I can do this with the for loop in ...

    
asked by anonymous 11.11.2015 / 20:03

1 answer

1

You can do this:

#!/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 running a command that tells how many * .jpg files are in the current directory.

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

    
12.11.2015 / 13:05