what's wrong with my script?
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. 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 ] 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 -- Daniel Bauer photographer Basel Málaga Twitter: @Marsfotografo (often explicit nudes) https://www.patreon.com/danielbauer https://www.daniel-bauer.com (nudes)
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
On Wed, 27 Dec 2023 23:42:55 +0300, Andrei Borzenkov <arvidjaar@gmail.com> wrote:
On 27.12.2023 23:26, Daniel Bauer wrote:
[...] 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).
Along with Andrei's "nullglob" suggestion, adding a file check wouldn't hurt: cd "$png_directory" || exit shopt -s nullglob for bild in *.png do if [ ! -f "$bild" ] ;then printf 'strange_script: not a file: %s\n' "$bild" >&2 exit 2 fi [...] done -- Robert Webb
On 12/28/23 01:57, Robert Webb via openSUSE Users wrote:
if [ ! -f "$bild" ] ;then
I've gotten to where I like -s even better. Adds a non-empty check to the file which is helpful in many cases where you don't want to operate on an empty file. -- David C. Rankin, J.D.,P.E.
On 2023-12-27 21:26, Daniel Bauer wrote:
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)?
When there is no png file in the directory it can't glob so the script evaluates the statement as string ie *.png. You can check by running it with: $ bash -x strange_script.txt I suggest you check for png first or change the this row: for bild in *.png to for bild in `ls *.png` Or you could check for png with: if compgen -G "*png"; then <your script> fi -- /bengan
participants (5)
-
Andrei Borzenkov
-
Bengt Gördén
-
Daniel Bauer
-
David C. Rankin
-
Robert Webb