On 27.12.2023 23:26, Daniel Bauer wrote:
Hello,
I have a script that does some imageMagick stuff in a for-loop and save the results in sub-directories. Normally it works perfect, but in a certain condition it produces a strange result I cannot explain.
I start the script in my /home/daniel/digikam folder with ./scriptname.sh
It cd's to the directory containing the files that should be transformed and the for-loop searches for all *.png files, like:
cd to png-directory for bild in *.png do resize $bild, add text, save ${bild%.png}.jpg as JPEG-image in sub-directory ./4000 add color profile to ./4000/"${bild%.png}.jpg" done
(The original script resizes to 2 sizes into 2 sub-directories, adds a color-profile and then converts to new color-profile to both versions, it is here: https://www.daniel-bauer.com/div/strange_script.txt)
Now when I have NO png-files (but jpg-files) in the "png-directory", in my opinion the script should do just nothing, because there is nothing to process in the for-loop.
for-loop is not aware about files. It works with strings. That some strings are the result of the pathname expansion is irrelevant for the for-loop. In this case it gets string *.png. If you want for-loop to get empty string list (and really do nothing) set shell option nullglob. Or explicitly test inside your for-loop for $bild being equal "*.png" (which will result in the false positive if there is real file with the name *.png).
But what happens is:
All files that already have been in the sub-directories before are copied to a file with the name of the last file that already was in that sub-directory, plus a number.
[to explain:
there are already 3 files A.jpg, B.jpg, C.jpg in the sub-directory before I start the script. After the script executed I have there addditional: C-0.jpg, C-1.jpg, C-3.jpg ... ... C-32.jpg ... ... where those C-n-files are copies of A, B and C.jpg
]
You call convert "*.jpg" bla bla bla which gets expanded by ImageMagick tools. I have no idea how the expansion results are interpreted, I suppose this can be found in ImageMagick documentation.
Where is my error? Why does the script do anything, when the condition for the for-loop isn't met (there are no *.png-files to process)?
Thanks for your help!
Daniel