Shell Scripts

Luke Davis ldavis at shellworld.net
Sat Nov 8 15:49:49 EST 2003


Oh, something else you have to know about scripts, is that they have to
either be in a bin directory (such as /usr/local/sbin, or /root/bin (if
that is in your path)), or you must specify the path, even if it is in the
current directory (for example: "./myscript").

Now, here's a more advanced version of my previous script.  It deletes all
files given to it on the command line, and then edits new files with those
names, in turn:

Warning that this is highly untested, and I may have forgotten something.

#!/bin/bash

# See if the first commandline parameter is empty
if [ "$1" == "" ]; then
echo "You didn't specify any files!"
exit 1
fi

# conditionally delete the files
rm -i $@ && \
# If the rm was successful, print this
echo "All files deleted..." || { \
# Otherwise, enter a command sequence, and start by outputting this
echo "There was a problem deleting the files; exiting..."
# Exit the program
exit 2
# End the command sequence
}

# Output the following, with no carrage return at the end
echo -n "Press enter to start editing the files."
# Read what the user types into the variable "$dummie"
read dummy

# Start a loop conditional
while ;
# Start the loop of commands; and assign the first parameter to $curname
do curname=$1
# Edit the file listed in $curname
nano $curname
# Put the first parameter into $lastname
lastname=$1
# Move all of the command line parameters one parameter to the left, so
# that $2 is now $1, $3 is now $2, and so on
shift
# If the new $1 is empty, exit
if [ "$1" == "" ]; then break;
# Or, if the previous name, and the current first parameter are the same,
# leave the loop
elif [ "$lastname" == "$1" ]; then break;
# Otherwise, start over with the new parameters
else continue;
fi			# Leave the if statement
done;			# Leave the while do loop

exit 0;			# Leave the program with success

Luke





On Sat, 8 Nov 2003, Rejean Proulx wrote:

> I have a bunch of MYSQL tables and files to back up.  I want to write some
> scripts that call functions so that I can reuse them.  I need to have a
> script that gets called like this
>
> backsql tablename weekly
>
> Table name and weekly are 2 parameters.  In dos we use to code that with %1
> %2 or maybe it was just numbers.  How do I pass parameters to a script?  I
> need a sample to work from so I can get started.  Then I can read manuals.
> What file names do I use in Linux.  Would it be backmysql.sh or something
> like that?
>
>  Rejean Proulx
> Visit my family at http://interfree.ca
> MSN is: rejp at rogers.com
> Ham License VA3REJ
>
>
> _______________________________________________
> Speakup mailing list
> Speakup at braille.uwo.ca
> http://speech.braille.uwo.ca/mailman/listinfo/speakup
>




More information about the Speakup mailing list