#!/bin/bash
#
# Author: Wiebe Cazemier (wiebe@halfgaar.net)
#
# Template bash script, for when you need something overengineerd :)
# Hack prevention
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
# Error codes
wrong_params=5
interrupted=99
default_error=1
# Function to echo in color. Don't supply color for normal color.
echo_color()
{
message="$1"
color="$2"
red_begin="\033[01;31m"
green_begin="\033[01;32m"
yellow_begin="\033[01;33m"
color_end="\033[00m"
# Set color to normal when there is no color
[ ! "$color" ] && color_begin="$color_end"
[ "$color" == "red" ];
color_begin="$red_begin"
[ "$color" == "green" ];
color_begin="$green_begin"
[ "$color" == "yellow" ];
color_begin="$yellow_begin"
-e "${color_begin}${message}${color_end}"
}
end()
{
message="$1"
exit_status="$2"
[ -z "$exit_status" ];
exit_status="0"
[ ! "$exit_status" -eq "0" ];
echo_color "$message" "red"
echo_color "$message" "green"
[ "$exit_status" -eq "$wrong_params" ];
dohelp
$exit_status
}
# Define function to call when SIGTERM is received
"end 'Interrupted' $interrupted" 1 2 3 15
dohelp()
{
""
"Example script"
""
"help = todo"
# Exit because you don't want the script to do anything after displaying help
}
[ -n "$*" ];
flag=$1
value=$2
"$flag"
"--option1")
option1=$value
;;
"--help")
dohelp
;;
"--")
;;
*)
end "unknown option $flag. Type --help" "$wrong_params"
;;
[ -z "$option1" ];
end "option1 not given" $wrong_params
1 Comment ( Add comment / trackback )
test