[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-03 10:48:45
|
Hi,
I would like to write a shell script that creates a backup of a source
directory to a destination directory.
However simply cp SRC DEST won't do, here's a brief description:
backup without parameters or options must copy the default SRC to the
default DEST. but I want to be able to use the script like backup SRC
DEST as well to create backups of other directories.
Here's why cp won't do:
I want the script to create a directory in DEST in the format (date
+'%Y%m%d') e.g. 20020703 and then copy all files in SRC recursively into
that directory.
But I want to be able to create more than one backup a day if that's
necessary, and I want the script to detect it has already created a
backup and add a number to the name of the dir so it would be like
20020703-2. So it would have to check recursively until the directory
doesn't exist and then create and copy.
I only started learning about scripts yesterday, so this might be a
little over my head, but I want to give it a try nonetheless! :) (I can
see you laughing at me already) :)
I downloaded and installed the lc_new_script script but if I understand
it right I can only do what I want like "backup -s SRC -d DEST" i.e.
with options, is it possible to do it without those or is that something
exclusive for cp and mv?
Here's what I've got so far, it isn't near what I want but it's fun
already!
SOURCE_DIR=~/scripts/
TARGET_DIR=~/scripts-backup
DATE=$(date +'%Y%m%d')
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
Here are a couple of things I noticed already:
if you define the above variables like SOURCE_DIR="~/scripts/" the ~
doesn't get substituted.
Also, but I might have to reread the "writing shell scripts" tutorial
again, why do you sometimes need $VAR and sometimes ${VAR} and on other
occasions $(VAR). the last one seems to be for expressions, am I right?
Thanks a lot in advance, I loved the tutorial!
--
| 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
\____________________________
|