Re: [SLUG] Bash script

From: Paul M Foster (paulf@quillandmouse.com)
Date: Tue May 24 2005 - 01:19:25 EDT


On Thu, May 19, 2005 at 11:00:02PM -0400, Bob Stia wrote:

> Hello Sluggers,
>
> What is wrong with this script??

Bob, by putting out a script like this without any explanation, we
assumed you wrote it, or knew how it worked. It looks, instead, like you
copied it from somewhere else. Right?

I'll go over it, and try to explain the prior comments.

> --------------------------------------------------
> #!/bin/sh
>
> #########################################################################
> # A simple script to create smaller  and lower quality images from
> # one directory to another
> #########################################################################
>
> # The file format you wish to change
> FROM=jpg
>
> # The end result file format
> TO=jpg
>
> # Options for "convert"
> CONVERT_OPTIONS='-quality 70 -geometry 800x600'
>
> # Pull the directory name for the title
> FROM_DIR=/home/bob/WebPages/PorschePilots/fog/temp
> TO_DIR=/home/bob/Webpages/PorschePilots/fog/carlouelimages
>
> #########################################################################
> # The actual conversion stuff
> # You can edit stuff below here to change how this works, or just stick
> # with the variables above (which should be good for most people
> #########################################################################
>
> echo Running $0
> echo
>
> COUNT=0

Someone asked if this is an unused variable. You've said here you want
to create a variable called COUNT, and you want to assign it a 0 value.
That's fine except that it's not referenced anywhere else in the script.
So it appears you're just assigning a variable a number for no reason,
thus an "unused variable". You can eliminate this line if you like.

>
> cd $FROM_DIR

Someone else mentioned $VARNAME versus ${VARNAME}. No, you won't find
this exact thing in your script, but the above is an example of what the
poster meant. When he said "VARNAME", he meant "variable name". Anything
preceded by a $ in a bash script is a variable. In this case, the
variable name is $FROM_DIR. In bash, you can also say ${FROM_DIR}, and
it means mostly the same thing. In most cases, it's a matter of
preference which form you use. In some cases you need to use ${FROM_DIR}
instead. But I don't think you have any of that kind of thing in your
script.

You'll note that earlier, you created the COUNT variable and assigned it
a 0 value. Notice you didn't have a $ in front of it. When you first
create a variable and assign a value to it, you don't put the $ there.
But later if you wanted to _use_ the COUNT variable, like:

echo $COUNT

you'd have to put the $ in front of it, or bash would choke. Um, I
should also say that "echo" tells bash to print out everything else
that's on that line. So the line above would tell bash to print out the
value of the COUNT variable.

> for i in *.$FROM; do
>
>   echo $1

The above appears to by line 35 by my count. Terms like $0, $1, $2
indicate the parameters with which you call the program. $0 is always
the name of the script. So for example, if you said:

bobs_script joe sam

then those variables would be as follows:

$0 = bobs_script
$1 = joe
$2 = sam

You can see from this how it would progress; the next thing on the
command line corresponds to the next number.

In this case, I don't think you're actually passing the script any
parameters, so the $1 is meaningless. I suspect this should be

echo $i

instead of echo $1. That way the script would tell you what file it was
processing for each loop.

If my count is right, I don't know why your script is telling you this
line is an unknown command. I suspect there is some alteration between
the running script and what you posted. Otherwise, it doesn't make
sense.

>   if [ ! -e "$TO_DIR/$i"  ]       # Process only files not already done 
>       then
>          echo Converting $i... with $CONVERT_OPTIONS
>           convert $CONVERT_OPTIONS $FROM_DIR/$i $TO_DIR/${i%%$FROM}$TO
>        echo
>   fi
>
> done;
>
> exit;
> ----------------------------------------------------------------------
> This is what I get:
>
> /home/bob/commands/ImageMagickConvert: line 35:  : command not found
> /home/bob/commands/ImageMagickConvert: line 36:  : command not found
> /home/bob/commands/ImageMagickConvert: line 37:  : command not found
> /home/bob/commands/ImageMagickConvert: line 38:  : command not found
> /home/bob/commands/ImageMagickConvert: line 39:  : command not found
> /home/bob/commands/ImageMagickConvert: line 40:  : command not found
> /home/bob/commands/ImageMagickConvert: line 41:  : command not found
> bob@EasyStreet:/>

Make the modification(s) I suggested above, and try the script again. If
still no joy and the error is the same, try to cut and paste the script
into an email back to the list (so there are no alterations). We can
look at it again.

Paul
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:07:04 EDT