Python (the language with the most community traction to replace bash for scripts) is a royal pain to use for this without libraries that are present in no default system, and even with those it often ends up being more verbose than it should.
Python (the language with the most community traction to replace bash for scripts) is a royal pain to use for this without libraries that are present in no default system, and even with those it often ends up being more verbose than it should.
maybe I was not clear - nothing against shell-scripting but doing some weird dancing like in the article is imho useless, also depending on bash is a stupid idea imho.
posix sh + shellcheck is all you need. if you can't solve your problem in posix sh rethink your code / approach and simplify until it will work.
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).I agree that python is a pain. It's too low level for any task.