Skip to content

Instantly share code, notes, and snippets.

@mickours
Last active December 15, 2015 05:09
Show Gist options
  • Save mickours/5207399 to your computer and use it in GitHub Desktop.
Save mickours/5207399 to your computer and use it in GitHub Desktop.
Template for Bash script. Including main structure with argument and colored OK / FAIL message for shell script
#!/bin/bash
#############################################################################
# my script NAME #
#############################################################################
#
# *Description*
# this script must be used ...
#
#
#
# It must also ...
#
#
# *Author*: Michael Mercier <mickours@gmail.com>
#
#############################################################################
#show the steps for debug
#set -x
### SETTINGS ###
CONTACT="Michael Mercier <mickours@gmail.com>"
#display prefix
BEGIN_RED='\e[01;31m'
BEGIN_GREEN='\e[0;32m'
END_COLOR='\e[0m'
SCRIPT_START="[${BEGIN_GREEN}START${END_COLOR}]"
SCRIPT_OK="[ ${BEGIN_GREEN}OK${END_COLOR} ]"
SCRIPT_FAIL="[${BEGIN_RED}FAIL${END_COLOR} ]"
### USAGE ###
usage(){
echo "USAGE:
$0 [-hicr]
-h
display this Help
-i
Install
-c
Configure
-r
start Runtime
default (without argument) run the 3 steps: Install, Configure and Runtime.
Problems or questions?
contact $CONTACT
";
exit 1
}
### UTILS ###
clean-env(){
#clean the environnement...
}
catch-errors(){
trap 'echo -e "$SCRIPT_FAIL Failed run $FUNCNAME, aborting."
clean-env
exit 1;' ERR
}
### TEMPLATE ###
template(){
catch-errors
echo -e "$SCRIPT_START $FUNCNAME..."
#do something...
echo -e "$SCRIPT_OK $FUNCNAME finished"
return 0
}
### FUNCTIONS ###
install(){
#do something
echo "Hello world!"
}
configure(){
#do something
echo "Hello world!"
}
runtime(){
#do something
echo "Hello world!"
}
### MAIN ###
echo -e "$SCRIPT_START setup..."
if [ $# -eq 0 ]; then
install
configure
runtime
fi;
while getopts ":icrh" opt; do
case $opt in
i)
install
;;
c)
configure
;;
r)
runtime
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
echo -e "$SCRIPT_OK setup finished."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment