Skip to content

Instantly share code, notes, and snippets.

@jelder
Created July 26, 2012 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelder/3183322 to your computer and use it in GitHub Desktop.
Save jelder/3183322 to your computer and use it in GitHub Desktop.
Template for robust shell scripting
#!/bin/bash
set -o nounset
set -o errtrace
set -o errexit
set -o pipefail
progname=$(basename $0)
default_name="Marley"
usage() {
extra=${1:-''}
cat <<END
Usage: $progname OPTIONS
-h Display this help text
-n Name (default is ${default_name})
$extra
END
exit 1
}
while getopts "hn:" OPTION ; do
case $OPTION in
h)
usage
;;
n)
name=$OPTARG
;;
*)
usage
;;
esac
done
name=${name:-$default_name}
tempdir=$(mktemp -d /tmp/$progname.XXXXXX)
function cleanup() {
rm -rf $tempdir
}
trap cleanup INT EXIT
echo "Hi ${name}" > $tempdir/output.txt
cat $tempdir/output.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment