Bash passes flags to shell wrong


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash passes flags to shell wrong
# 1  
Old 07-06-2003
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.
# 2  
Old 07-06-2003
The plot thickens

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

Last edited by andyj; 07-06-2003 at 11:04 PM..
# 3  
Old 07-07-2003
"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.
# 4  
Old 07-07-2003
good idea, but I dont think its the one

Thanks Oombera

Still not taking it Smilie

# 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?

Last edited by andyj; 07-07-2003 at 12:58 PM..
# 5  
Old 07-07-2003
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?
# 6  
Old 07-07-2003
Must be a typo

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 Smilie

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

What method might I use to'see' what gets passed in the call to convert, something involving tees and pipes I suspect?
# 7  
Old 07-07-2003
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What's wrong with my bash code?

I want to let sleep 3 in the background and echo $i pkglists="a b c d e f g" f() { local i set -- $pkglists && ((i +=2)) && sleep 3 &;echo $i } f (3 Replies)
Discussion started by: yanglei_fage
3 Replies

2. Shell Programming and Scripting

Bash conditional | getting logic wrong?

I have a file cat <<EOF > /tmp/test Line one Line two Line three EOF Now I am trying to get the exit stat ($?) of 1 if any text is found and 0 if any text is not found using grep, i.e. just reversing the exit status of grep # (snippet 1) this one is not working!!! retval $?... (4 Replies)
Discussion started by: the_gripmaster
4 Replies

3. AIX

find {start} -ls - Two Passes?

I have a &quot;find {start} -ls&quot; command listing all files and dirs in on the whole filesystem. As part of this it lists locations that contain temporary files and, sometimes when executing, it identifies a file but produces an ERROR when trying to list it. ERROR thrown: find: bad status--... (2 Replies)
Discussion started by: apiggott
2 Replies

4. Shell Programming and Scripting

Please help to fingure out what wrong with my tomcat restarting bash script

Hi, I am a nbee to Unix, I have used following script to check my tomcat is running or not and restart if it down. but actually it restart my tomcat each time running even my tomcat still running fine: Script that can run a check and perform an action if the check fails ... (1 Reply)
Discussion started by: quyennd
1 Replies

5. Shell Programming and Scripting

Check if parameter passes in contains certain string

Hi Guys, I am writing a Unix script which accepts a directory path as parameter $1 so something like /user5.data/WA/01 will be passed in. I want to determine if the directory path passed in contains "WA" as above (because then I need to do something specific if it does) What is the... (9 Replies)
Discussion started by: bcunney
9 Replies

6. Shell Programming and Scripting

what's wrong with my bash script?

hi, please point out what's wrong with my script. im feeding it a list containing fqdn, sit should ssh into each and verify that atleast one of its virtual backup ip resolves into one of its virtual hostnames .. anyway the objective shows in the script... however, im having problems in the ... (4 Replies)
Discussion started by: ikk
4 Replies
Login or Register to Ask a Question