e633 wrote:
- directories to backup are hard-coded cause i couldn't figure out a good way to choose them "at runtime";
Please pardon my newbiness, but have you considered using positional parameters to specify the directories to back-up?
If you only want to back-up one directory, the code would be (something like):
Code:
BACKUP_DIR=$1
BACKUP_DIR=${BACKUP_DIR:?"No Directory Given: Usage: backup [directory1] ([directory2]) ([...])"}
echo "Directory to backup (BACKUP_DIR): $BACKUP_DIR"
If you want to back-up multiple directories, you could get all positional parameters (command line values) with a simple loop and a check for valid directories:
Code:
#backup
echo "backup: $@"
if [ $# = 0 ]; then echo "No Directory Given: Usage: backup [directory1] ([directory2]) ([...])"; exit 0; fi
unset BACK_DIR[*]
let -i count=0
for DIR in $@; do
let count=count+1
if [ ! -d $DIR ]; then
echo "$DIR is not a directory."
let count=count-1
else
BACKUP_DIR[$count]=$DIR
echo "BACKUP_DIR[$count]: ${BACKUP_DIR[$count]}"
fi
done
let -i DIRECTORIES=$count
echo "Number of good directories: $DIRECTORIES"
echo "Directories to back-up:"
for (( count = 1; count <= $DIRECTORIES; count++ )); do
echo ${BACKUP_DIR[$count]}
done
"Positional Parameters", from
Learning the bash Shell:
Quote:
The most important special, built-in variables are called positional parameters. These hold the command-line arguments to the scripts when invoked. Positional parameters have the names 1, 2, 3, etc. There is a positional parameter 0, whose value is the name of the script (i.e., the command typed in to invoke it).
Two special variables contain all of the positional parameters (except positional parameter 0): * and @. The difference between them is subtle and important, and it's apparent only when they are within double quotes.
"$*" is a single string that consists of all the positional parameters, separated by the first character in the value of the environment variable IFS (Internal Field Separator), which is a space, TAB, and NEWLINE by default. On the other hand, "$@" is equal to "$1" "$2" "$3"... "$N", where N is the number of positional parameters. That is, it's equal to N separate double-quoted strings, which are separated by spaces. If their are no positional parameters, "$@" expands to nothing. We'll explore this difference in a little while.
The varible # holds the number of positional parameters (as a character string). All of these variables are "read-only," meaning that you can't assign new values to them within scripts.
(
Learning the bash Shell [Third Edition], p.86 (Newham and Rosenblatt; O'Reilly; 2005)
http://oreilly.com/catalog/9780596009656/(Good book!)
