Bash passes flags to shell wrong

Hi, first post, so hello to all.
I have a Bash scripting problem that is driving me a bit nutty.

It involves a program called 'convert' which is part of the ImageMagick collection.
Normal usage from the commandline is:

$ convert -resize 120x120 inputfile.jpg outputfile.jpg

This is working just fine (from the command line)

However while inside a shellscript everything seems to go weird, this I beleive should work...

echo converting thumbnails
for searchfile in ./*.jpg
do
echo processing
echo $searchfile
convert resize 120x120 "$searchfile" "$tndir/$searchfile"
done

but I get instead

convert: Unable to open file (resize) [No such file or directory].
convert: Unable to open file (120x120) [No such file or directory].

I have tried strong quotes to bind the arguments tighter

convert 'resize 120x120' "$searchfile" "$tndir/$searchfile"

I have tried opening it in a subshell

(convert resize 120x120 "$searchfile" "$tndir/$searchfile")

But nothing will convince it that -resize 120x120 is an argument not a file!!!

The file arguments themselves are fine $searchdir and $tndir are interpreted right. Indeed I get unaltered files dumped in the destination directory which kind of confirms that its just the -resize flag that isn't getting heard.
I have read the man for this program over many times and I'm sure i'm using it correctly.

Any ideas on how to crack this? Many thanks to any takers.

I had an idea to debug this , im breaking it right down to basics

When I execute this from a script in the same directory as the given .jpg file it WORKS

echo I am doing this from a script
convert resize 120x120 apache.jpg apachetn.jpg

But when I put the filenames into variables

searchfile="apache.jpg"
tndir="apachetn.jpg"
echo I am a script that uses variables
echo $searchfile
echo $tndir
convert resize 120x120 $searchfile $tndir

IT FAILS

convert: Unable to open file (resize) [No such file or directory].
convert: Unable to open file (120x120) [No such file or directory].

btw it makes no difference if the variables are quoted or not

So my logic tells me that the last two vars are getting lost completely and hence convert is taking -resize and 120x120 to be the filenames

???

very confused

"convert" is already the name of a different unix command.. where is this convert program stored?

If it's in the same directory as your script, try:
./convert -resize 120x120 $searchfile $tndir

If it's another directory, try:
path/to/convert -resize 120x120 $searchfile $tndir

Also try including variables such as $searchfile and $tndir in quotes in the above lines.

Thanks Oombera

Still not taking it :frowning:

# which convert
/usr/X11R6/bin/convert

and

# whereis convert
convert: /usr/X11R6/bin/convert /usr/bin/X11/convert

which seem to be aliases of the same proggy

substituting the full path

/usr/X11R6/bin/convert resize 120x120 "$searchfile" "$tndir"

and unquoted

/usr/X11R6/bin/convert resize 120x120 $searchfile $tndir

yeilds the same problem.

What your saying reminds me of all those times I was banging my head trying to get a script called 'test' to work..... but alas it doesn't seem to be my problem on this occasion. The quest continues....

My Bash powers are weak but my next guess is to use 'here document' or command substitution... am I thinking right? How would I recast the syntax to use an alternative method of calling convert?

If your first post you say that convert works fine from the command line...then you give an example showing -resize.

But when you complain that it is failing, you have switched from -resize to ?resize. Why the change? Is that the problem?

I can't see that myself Perderabo, but if I did it was merely a typo which is not in my actual code...prolly getting tired :slight_smile:

atm I am experimenting with eval
This promises to force variable expansion before execution , but so far I'm getting the same results :frowning:

What method might I use to'see' what gets passed in the call to convert, something involving tees and pipes I suspect?

Another thought..

Perhaps when you run "convert -resize 120x120 apache.jpg apachetn.jpg", the values 'apache.org' and 'apachetn.jpg' are being passed to your convert program. But when you pass '$searchfile' and '$tndir', your convert program does not know what your script assigned to those variables...

Although this:

convert -resize 120x120 $searchfile $tndir

convert: Unable to open file (]resize) [No such file or directory].
convert: Unable to open file (120x120) [No such file or directory].

really looks like your script is inadvertently executing the "convert" command instead of your "convert" program.

Try this: rename your convert program to myConvert and just use that instead. Is that do-able?

Thanks guys I've cracked it!

Solution: I made a link to /usr/X11R6/bin/convert

Now im calling that link and everything works fine, must be a paths/environment issue, not sure I'll ever truly know but I've sure learned a lot of Bash the last 24 hours.

So thanks for all your thoughts that kept me going. Im kinda glad this problem had me beat because I discovered this place now. Hope you 'have a day' too and that it its filled with an adequate amount of prosperity and happiness :slight_smile:

cheers
Andy