Skip to content

Instantly share code, notes, and snippets.

@wyrmiyu
Last active August 29, 2015 13:56
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 wyrmiyu/9234362 to your computer and use it in GitHub Desktop.
Save wyrmiyu/9234362 to your computer and use it in GitHub Desktop.
Example bash script with option parsing, error handling, usage and cleanups
#!/bin/bash
# Define your global variables
#
# Add names of all global variables to GLOBALS
GLOBALS=" GLOBALS \
PATH \
SCRIPT_NAME \
ARG \
OPTION \
OPT_BOOL \
"
PATH=/bin:/usr/bin # Set lookup PATH for progs
SCRIPT_NAME=$(basename $0) # Get script name
ARG= # declared global var with initial null value
OPTION="Some default value" # same but with default value
OPT_BOOL=0 # Boolean global var, with default value 0
# Function declarations
exit_script()
{
# Method to clean environment and exit.
# If first argument is a number between 0 and 255 it's treated as an
# exit code to be used for exiting.
# If the exit_code is not 0, all other arguments passed to exit_script()
# are printed to stderr with "ERROR:" prefix. If the exit code is 0, then
# the arguments are printed to stdout with "INFO: " prefix.
# Default exit code is 1
local exit_code=1
local re var
re='^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
if echo "$1" | egrep -q "$re"; then
exit_code=$1
shift
fi
re='[[:alnum:]]'
if echo "$@" | egrep -iq "$re"; then
if [ $exit_code -eq 0 ]; then
echo "INFO: $@"
else
echo "ERROR: $@" 1>&2
fi
fi
# Clean-up global variables
for var in $GLOBALS; do
unset $var
done
# Print 'aborting' string if exit code is not 0
[ $exit_code -ne 0 ] && echo "Aborting script..."
exit $exit_code
}
usage()
{
# Prints out usage and runs exit_script().
# All args passed to usage() are forwarded to exit_script()
sed -e "s/^ //" -e "s|SCRIPT_NAME|$SCRIPT_NAME|" <<" EOF"
USAGE
Description of the script
SYNTAX
SCRIPT_NAME [OPTIONS] ARGUMENTS
ARGUMENTS
name Short description of the argument
OPTIONS
-o, --option <value> Short description of the option and the value
-b, --boolean Short description of the option
-h, --help Prints this usage.
EOF
exit_script $@
}
test_arg()
{
# Used to validate user input
local arg="$1"
local argv="$2"
if [ -z "$argv" ]; then
if echo "$arg" | egrep -q '^-'; then
usage "Null argument supplied for option $arg"
fi
fi
if echo "$argv" | egrep -q '^-'; then
usage "Argument for option $arg cannot start with '-'"
fi
}
# Your own functions go under here...
# MAIN() function to run the actual logic
main()
{
# Set the default exit value to be 0
local exit_value=0
# Parse arguments and options
[ $# -gt 0 ] || usage
while [ $# -gt 0 ]; do
case "$1" in
-o|--option)
test_arg "$1" "$2"
shift
OPTION="$1"
shift
;;
-b|--boolean)
OPT_BOOL=1
shift
;;
-h|--help)
usage 0
;;
*)
test_arg "$1"
ARG="$1"
shift
;;
esac
done
# Your code goes here...
# Exit script
exit_script $exit_value
}
main $@
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment