Created
July 26, 2012 17:25
-
-
Save jelder/3183322 to your computer and use it in GitHub Desktop.
Template for robust shell scripting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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