I agree.
I've used this page successfully as reference for portable syntax:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...
Some sections on features I use heavily:
- Parameter Expansion (specifically :-, %, %%, #, ##)
- Special Parameters (specifically "$@", $#, $?)
- set --, this lets you set the $1, $2, etc. variables. I use this with "$@" for arrays, primarily for building command strings.
Here's an small shell script demonstrating some of them:
#!/bin/sh
# err function
err() { echo "$1" >&2; exit 1; }
# init variables
unset src
unset dst
dry_run=false
# get arguments
while [ $# -gt 0 ]; do
case "$1" in
-d|--dry-run) dry_run=true ;;
--) shift; break ;;
-*) err "unknown option: $1" ;;
*)
if [ -z "$src" ]; then src="$1"
elif [ -z "$dst" ]; then dst="$1"
else err "unexpected argument: $1"
fi
;;
esac
shift
done
# sanity checks
## TODO: print usage
if [ -z "$src" ]; then err "source not specified"; fi
if [ ! -d "$src" ]; then err "source does not exist"; fi
if [ ! -r "$src" ]; then err "cannot read source directory"; fi
if [ -z "$dst" ]; then err "destination not specified"; fi
if ! rsync --version >/dev/null 2>&1; then
err "missing rsync(1)"
fi
# build rsync command
set -- rsync -aq "$src" "$dst"
# log command
echo "copying $src to $dst"
echo " $@"
if ! $dry_run; then
if "$@"; then
echo "success"
else
# rsync will have printed an error message
err "rsync exited with error code $?"
fi
else
echo "dry run; not executing"
fi
Also be sure to read the man page for test (the [ command).