Skip to content

Instantly share code, notes, and snippets.

@mapk0y
Created June 4, 2014 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapk0y/38a569fde4f2cdab1d08 to your computer and use it in GitHub Desktop.
Save mapk0y/38a569fde4f2cdab1d08 to your computer and use it in GitHub Desktop.
Template for Bash Script.
#!/bin/bash
# vi: set ts=4 sw=4 sts=0 et:
# [使いやすいシェルスクリプトを書く | SOTA](http://deeeet.com/writing/2014/05/18/shell-template/)
# [Zsh - シェルスクリプトのオプション設計ガイドライン - Qiita](http://qiita.com/mollifier/items/95a294f95f5977b9d663)
PROG=$(basename ${0})
usage() {
cat <<EOF
${PROG} is a tool for %FIXME%
Usage:
${PROG} [command] [<options>]
Options:
--debug, -d print debug messages.
--verbose, -v Verbose output.
--help, -h print this help.
Example:
${PROG} -v
EOF
}
error_exit() {
[ -z "${1}" ] || echo "${1}"
echo "Try `${PROG} -h|--help' for more information."
exit 1
}
# messages
red=31
green=32
yellow=33
blue=34
cecho() {
color=${1}
if test -t 1;then
echo -e "\033[1;${color}m$@"
tput sgr0
else
echo "$@"
fi
}
okecho() {
cecho $green "OK: $@"
}
eecho() {
cecho $red "ERROR: $@"
}
decho() {
if [ "${DEBUG}" -eq 1 ]; then
echo "DEBUG: $@"
fi
}
DEBUG=0
VERBOSE=0
while [ $# -gt 0 ]; do
case ${1} in
--debug|-d)
DEBUG=1
;;
--verbose|-v)
VERBOSE=1
;;
--help|-h)
usage
;;
*)
error_exit "Invalid option '${1}'"
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment