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 14:14:00
|
On Saturday 03 August 2002 06:48 am, you wrote: > 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) :) Actually, I think you are doing pretty well for just one day. > > 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? 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. > > 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 This is good so far. 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 # 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 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. Good observation. > > 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? The difference between $VAR and ${VAR} is subtle. Imagine this: You have a file called my_file1 and you want to copy it to my_file2, and you start to write a general script for this task like so: BASEFILE=my_file cp $BASEFILE1 $BASEFILE2 Thinking about this, you see that this won't work because the shell will think that BASEFILE1 and BASEFILE2 are seperate variable names. To fix this, you would do it this way: BASEFILE=my_file cp ${BASEFILE}1 ${BASEFILE}2 So, you use {} whenever you need to eliminate ambiguity. $(VAR) should really be $(command) since this performs command substitution, that is, the results of the command are substituted. > > Thanks a lot in advance, I loved the tutorial! 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. -- ||||| William Shotts, Jr. (bshotts AT panix DOT com) ||||| Be a Linux Commander! Follow me to http://linuxcommand.org |