Re: [SLUG] Bash script

From: Bob Stia (rnr@sanctum.com)
Date: Wed May 25 2005 - 01:32:34 EDT


On Tuesday 24 May 2005 01:19, Paul M Foster wrote:
> 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?

Ummmmm....BLUSH !!! Yes, I copied it and modified it to work ?? for me.
Made the suggested changes by you and Eben and still doesn't work. The
copied script is below.
>
> I'll go over it, and try to explain the prior comments.
>
...........<snip some stuff>.............
OK, Thanks

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

OK, understood and removed.
>
> > 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.

OK, understood and learning, (hopefully)
>
> 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.

OK, changed that.
>
> 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;
> > -------------------------------------------------------------------
.....................<snip more>...............
>
> 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

Sean, running SuSE 9.2 Pro. Yes, my convert is at /usr/bin/convert also.

Kwan, would like to try your suggested changes but if they are not
necessary I'd like to get this to work first.

Eben, You said, "Is this /home/bob/commands/ImageMagickConvert ?"
Yes, that is where the script is and how I run it.

Following is the script after Paul's and your suggestions:
------------------------------------------------------------------------------------
#!/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

cd $FROM_DIR
for i in *.$FROM; do

  echo $i
  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;
---------------------------------------------------------------------------------------
Bob S.

-----------------------------------------------------------------------
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:10:42 EDT