Re: [Linuxcommand-discuss] how do you create a shell script with parameters like cp or mv?
Brought to you by:
bshotts
|
From: William S. <wes...@co...> - 2002-08-04 18:32:06
|
On Sunday 04 August 2002 11:59 am, you wrote:
> On Sun, 2002-08-04 at 16:12, William Shotts wrote:
> > No, cp and mv are not special. While new_script encourages the use of
> > named options, there is nothing preventing the use of an argument list
> > instead.
>
> let me see if I understand this correctly: if you don't specify the
> options like the script does, you can still "extract" them from the
> command as the $1, $2 and so on?
> So every WORD that is passed on with the command that is no named option
> will get stored in some sort of an array?
Yes, this is correct. Actually, this is about the only way to get anything
from the command line. The scripts I write for using new_script use the
getopts command to process options, but the lesson on positional parameters
will demonstrate how to do it with a while, a case, and a bunch of shifts.
It is actually a more flexible way to do it, and I will probably change
new_script to do it that way.
>
> I've been reading the Bash man pages but the part about the $ was very
> unclear to me...
That's why it needs its own lesson ;-)
>
> > To eliminate the need for the "hard-coded" values in SOURCE_DIR and
> > TARGET_DIR, you would use positional parameters like so:
> >
> > #!/bin/bash
> >
> > SOURCE_DIR=$1
> > TARGET_DIR=$2
> > PROGNAME=$(basename $0) # Figure out name of program
> >
> > DATE=$(date +'%Y%m%d')
> >
> > # Check for arguments
> >
> > if [ $# -ne 2 ]; then
> > echo "Usage: $PROGNAME source_dir destination_dir" 1>&2
> > exit 1
> > fi
>
> So $# would be some loop going through the array of options? What does
> the "2" in the expression stand for?
No, $# holds the number of words on the command line in addition to $0
which the name of the command. The "2" is the number of arguments the
program requires (source and target directories). The if statement is
testing to see if the number of arguments is not equal (-ne) to 2. If it is
not equal, then output a usage message and exit with an error.
>
> > # Check that source directory exists
> >
> > if [ ! -d $SOURCE_DIR ]; then
> > echo "Source directory $SOURCE_DIR does not exist!" 1>&2
> > exit 1
> > if
> >
> > # Perform the copy
>
> In the mean time I think I'm going to have it tar -czf to save some
> space :), but the principle is the same...
>
> > if [ -d ${TARGET_DIR}/${DATE} ]; then
> > echo "you already created a backup today!"
> > else
> > mkdir -p $TARGET_DIR/$DATE
> > cp -r $SOURCE_DIR/* $TARGET_DIR/$DATE
> > fi
>
> The part about quoting is something I had to read again, it's ore
> important than I first thought, I've some experience with JavaScript and
> it's a lot less important there...
>
> > The difference between $VAR and ${VAR} is subtle.
> > So, you use {} whenever you need to eliminate ambiguity.
>
> Indeed, that part was quite clear in the bash man pages, that means I
> understood it the first time I read it! :))
>
> > You're welcome. Glad it was of use.
> >
> > By the way, the next lesson I plan to write will cover positional
> > parameters, so stay tuned.
>
> Definitely, I'm looking forward to the next lesson! I like it that the
> lessons are focused on the "non-root" tasks, it takes away much of the
> fear of "learning by example", which is something I really like.
>
> Actually related to this, I didn't perform the installation exactly the
> way you suggest in the README, to avoid exactly the kind of problems
> root permissions can cause. I cp'd the file to my ~/bin directory and
> changed its permissions to 700. Also I removed the -f from the rm
> commands in the scripts, I know the only file that get deleted are in
> the /tmp folder but still. (It actually was the first time I exercised
> my rights to modify free software, that may also have been part of the
> reason! :)) )
>
> Why is the -f there anyway? I don't get any questions since I have full
> rights to those files...
-f does two things:
1. If the file you are trying to delete does not exist, -f will force rm to
not complain about it.
2. -f will override -i. This is useful if, for example, you are on a Red Hat
system and rm has been aliased to "rm -i" like it is on the root account.
This way you can delete multiple files without being prompted before each one
is deleted.
>
> Thanks for the reply, I'll keep you posted about my progress (though it
> may be slow)
>
> Bram
--
||||| William Shotts, Jr. (bshotts AT panix DOT com)
||||| Be a Linux Commander! Follow me to http://linuxcommand.org
|