Skip to content
Advertisement

bash: [: 1+1: integer expression expected

I have a main directory with 40 subdirectories with names [Set 1], [Set 2][Set 40], each containing 20 wallpapers with name 1.jpg, 2.jpg, … 20.jpg. I want to move all these wallpapers to the main directory and rename them as 1.jpg, 2.jpg800.jpg. I wrote the following Bash script but getting error.

i=1; j=1; k=1;
while [ $i -ne 41 ]; do
  j=1;
  while [ $j -ne 21 ]; do
    mv [Set $i]/$j.jpg $k.jpg;
    j=$j+1;
    k=$k+1;
  done;
  i=$i+1;
done
bash: [: 1+1: integer expression expected
bash: [: 1+1: integer expression expected

Where am I making a mistake?

Advertisement

Answer

In bash you enclose mathmatical/arithmetical operations in $(()), so

i=1; j=1; k=1; 
while [ $i -ne 41 ]; do 
    j=1
    while [ $j -ne 21 ]; do 
        mv "[Set ${i}]/${j}.jpg" $k.jpg
        j=$(($j+1)) k=$(($k+1))
    done
    i=$(($i+1))
done

Should do what you want..

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement