> On May 31, 2018, at 14:40, Stephen via talk <[email protected]> wrote:
>
> I want to build a script to convert any *.png files to *.jpg files using
> convert.
>
> Doing manually works fine.
>
> Building a script has presented problem. I am well versed in programming but
> a novice with bash.
>
> My loop through the files works fine.
>
> #!/bin/bash
> cd /big1/memes/;
>
> shopt -s nullglob
> for f in "*.png"
> do
> echo $f
> done
>
> I get a list of filenames with an extension of .png
>
In this case, the value of $f is ‘*.png’. Your loop iterates once, and the
list of PNG files is generated as a shell expansion before evaluating the
“echo”.
> But when I try to get the basename with:
>
> #!/bin/bash
> cd /big1/memes/;
>
> shopt -s nullglob
> for f in "*.png"
> do
> echo $f
> b=${f%.png}
> echo $b
> done
>
> I first get a list of the .png files, then a list of all files, both the
> existing .jog and the .png. This gets a WTF reaction from me.
Once more, $f is ‘*.png’ and is being expanded for the execution of the echo.
Your brace expansion looks to be converting ‘*.png’ to the string ‘*’, and
assigning that to b. The when evaluating the line “echo $b”, the value of $b,
‘*’, is expanded prior to execution.
> Can someone explain and help me get closer to getting this to work?
You were a touch sloppy with your quoting. You should have left “*.png”
unquoted in the for loop so that the list of file names to process would expand
there, and stuck $f and $b in double quotes so their values would not expand.
Seneca
---
Talk Mailing List
[email protected]
https://gtalug.org/mailman/listinfo/talk