Re: [Linuxcommand-discuss] how do you create a shell script with parameters like cp or mv?
Brought to you by:
bshotts
|
From: Mertens B. <bra...@li...> - 2002-08-06 21:11:53
|
On Sun, 2002-08-04 at 16:12, William Shotts wrote:
> 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')
>
[snip]
>
> 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
I was just playing around with this a bit and I changed it to:
else
echo "mkdir -p ${TARGET_DIR}"
echo "tar -czf ${TARGET_DIR}/${DATE}.tar.gz ${SOURCE_DIR}"
fi
But I believe this introduces a problem: (the script is called bu)
if the user executes the command $ bu oldvcards/ backup/vcards
the result is:
mkdir -p backup/vcards
tar -czf backup/vcards/20020806.tar.gz oldvcards/
which what I want but if the user executes $ bu oldvcards/
backup/vcards/ the result is:
mkdir -p backup/vcards/
tar -czf backup/vcards//20020806.tar.gz oldvcards/
Won't the double slash cause a problem?
If it does the script should perform a regular expression on the
variables, the SOURCE_DIR won't make a difference but the TARGET_DIR
should be checked, would this work:
if [ ${TARGET_DIR} = .*/$ ]
echo "please omit the trailing '/' from ${TARGET_DIR}"
fi
Is it possible to strip that character automatically? I can only find
the 'offset' substring but that seems to work only to omit the beginning
characters... In JavaScript it is possible to determine the length of a
string, if that is possible in bash I could use the offset command...
TIA
--
| Mertens Bram "M8ram" <bra...@li...>
| Registered [Red Hat] Linux User # 249103 since Octobre 2000
| http://linux.be | http://www.redhat.com | http://counter.li.org
\____________________________
|