Have to kill a bash script twice for it to run

If I run this script for several files, it works fine...

files=(./pics/all/*.jpg) # create array from directory
total=${#files[@]} # array length
i=0
for file in ${files[@]}; do
    filename="${file##*/}" # turns ./pics/all/045315.jpg into 045315.jpg
    convert $file -sampling-factor 4:2:0 -strip -quality 1 -interlace JPEG -colorspace RGB ./pics/all_resampled/$filename;
    i=$(( i + 1 ))
    progress=$(echo "scale=2;$i/$total*100" | bc)
    echo $i/$total ${progress%.*}% $file RESAMPLED;
done

However, for the 30,000+ images I have, if I run the script, it does nothing.

When I kill it with CTRL + c, it prints the whole array.

Killing it again it prints the array again, and only after that does it start looping and resampling...

What is going on?

1 answer

  • answered 2021-11-23 05:31 Ivan

    Turns out, I had two images named *.jpg and **.jpg which caused all the weirdness.

    This caused the first two resamples to be done as if the whole directory was a single file.

    Revised code:

    fileCount=$(ls ./pics/all | wc -l)
    i=0
    for file in ./pics/all/*.jpg; do
        filename="${file##*/}" # turns ./pics/all/045315.jpg into 045315.jpg
        convert $file -sampling-factor 4:2:0 -strip -quality 1 -interlace JPEG -colorspace RGB ./pics/all_resampled/$filename;
        i=$(( i + 1 ))
        progress=$((100 * i / fileCount))
        echo $i/$fileCount $progress% $file RESAMPLED;
    done
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum