#!/bin/bash
HasToBeRoot=0
RunWithoutArguments=0
DEBUG=0
# Allowed Arguments
declare -a allowed_arguments
# Syntax: Option|Has_Value|Description|[Option_Short]
# Argumente werden als Variable ARG_<name> mit 1|0 gespeichert
# Argumente mit Value werden zusaetzlich in ARG_<name>_VALUE gespeichert
# Ausnahme bildet --help|-h (kann nicht ueberschrieben werden)
#allowed_arguments[0]="--verbose|0|Gives Verbose-Output|-v"
#allowed_arguments[1]="--debug|0|Enables DEBUG-Mode|"
#allowed_arguments[2]="--user|1|Followed by the Username|-u"
#allowed_arguments[3]="-p|1|just a test|"
#allowed_arguments[4]="-x|0|just onemore test"
#============================================================================
# Standard-Variablen fuellen
cd `dirname $0`
scriptpath=`pwd`
scriptname=`basename $0`
# DEBUG
if [ $DEBUG -eq 1 ]; then
echo -e "DEBUG: Scriptstart: "`date`"\n"
echo -e "DEBUG: Env-UserID: "`id -u`
echo -e "DEBUG: HasToBeRoot: "$HasToBeRoot
echo -e "DEBUG: RunWithoutArguments: "$RunWithoutArguments"\n"
echo -e "DEBUG: Scriptcall:\t"$0
echo -e "DEBUG: ScriptName:\t"$scriptname
echo -e "DEBUG: ScriptPath:\t"$scriptpath"\n"
for i in $(seq 0 $((${#allowed_arguments[@]} - 1))); do
echo -e "DEBUG: allowed_argument:\t"${allowed_arguments[$i]}
done
echo -e "\nDEBUG: Arguments:\t"$@
fi
# Print Syntax
function print_usage {
echo -n "USAGE"
if [ $HasToBeRoot -eq 1 ]; then echo -n " (as root)"; fi
echo -n ": "$scriptname
if [ ${#allowed_arguments[@]} -gt 0 ]; then
echo -n " "
if [ $RunWithoutArguments -eq 1 ]; then echo -n "["; fi
echo -n "options"
if [ $RunWithoutArguments -eq 1 ]; then echo -n "]"; fi
echo -e "\n\nOptions:"
for i in $(seq 0 $((${#allowed_arguments[@]} - 1))); do
option=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $1 }'`
short=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $4 }'`
value=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $2 }'`
desc=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $3 }'`
echo -en "\t"$option
if [ -n "$short" ]; then
echo -n ", "$short
else
echo -en "\t"
fi
echo -e "\t"$desc
if [ $value -eq 1 ]; then
echo -e "\t\t\tThis options needs an additional argument: "$option" <value>"
fi
done
fi
echo -en "\n"
}
# Print Info
function print_info {
echo "INFO: Try --help|-h to get help for the syntax."
}
# Spezialfall: --help|-h
if [ `echo " $@ " | grep " --help " &> /dev/null; echo $?` -eq 0 ] || \
[ `echo " $@ " | grep " -h " &> /dev/null; echo $?` -eq 0 ]; then
# DEBUG
if [ $DEBUG -eq 1 ]; then echo -e "\nDEBUG: Asked for Help."; fi
print_usage
exit 0
fi
# HasToBeRoot
if [ $HasToBeRoot -eq 1 ] && [ `id -u` -ne 0 ]; then
#DEBUG
if [ $DEBUG -eq 1 ]; then echo -en "\n"; fi
echo "ERROR: Only root can run $scriptname"
print_info
exit 1
fi
# RunWithoutArguments
if [ $RunWithoutArguments -eq 0 ] && \
[ ${#allowed_arguments[@]} -gt 0 ] && \
[ $# -eq 0 ]; then
# DEBUG
if [ $DEBUG -eq 1 ]; then echo -e "\nDEBUG: No run without arguments!"; fi
echo "ERROR: Missing Arguments(s)."
print_usage
exit 1
fi
# Argumenten-Auswertung
if [ ${#allowed_arguments[@]} -gt 0 ]; then
declare -a value_args
m=0
value_arg=0
tmp_arguments="$@"
arr_arguments=""
# Validierung der Short-Options
for tmp_arg in $tmp_arguments; do
if [ `echo "$tmp_arg" | egrep "^-{1}[a-zA-Z0-9]{2,}$" &> /dev/null; echo $?` -eq 1 ]; then
arr_arguments="$arr_arguments $tmp_arg"
else
tmp_arg=`echo "$tmp_arg" | sed s/"-"//g`
arr_arguments="$arr_arguments"`echo "$tmp_arg" | sed 's/[a-z]/ -&/g'`
fi
done
arr_arguments=`echo "$arr_arguments" | sed 's/^ *//g'`
# DEBUG
if [ $DEBUG -eq 1 ]; then
echo -e "DEBUG: Arguments:\t$arr_arguments\n"
fi
# Fuer jedes Argument...
for argument in $arr_arguments; do
found_arg=0
if [ $value_arg -eq 1 ] && \
[ `echo "$argument" | grep -e "^-" &> /dev/null; echo $?` -eq 1 ]; then
eval "ARG_${value_arg_name}_VALUE=\"$argument\""
value_args[$m]="$argument"
m=$(( $m + 1 ))
# DEBUG
if [ $DEBUG -eq 1 ]; then
echo -e "DEBUG: set variable: ARG_${value_arg_name}_VALUE => "$argument
fi
value_arg=0
unset $value_arg_name
continue
else
value_arg=0
unset $value_arg_name
fi
for i in $(seq 0 $((${#allowed_arguments[@]} - 1))); do
option=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $1 }'`
short=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $4 }'`
value=`echo "${allowed_arguments[$i]}" | awk -F\| '{ print $2 }'`
if [ "x$argument" = "x$option" ] || [ "x$argument" = "x$short" ]; then
found_arg=1
value_arg=$value
option_arg=$option
if [ $value_arg -eq 1 ]; then
value_args[$m]=$argument
m=$(( $m + 1 ))
fi
break
fi
done
if [ $found_arg -eq 0 ]; then
echo "ERROR: illegal option: "$argument
print_info
exit 1
else
argument=`echo $option_arg | sed s/-//g`
eval ARG_$argument=1
# DEBUG
if [ $DEBUG -eq 1 ]; then
echo -e "DEBUG: set variable: ARG_$argument => 1"
fi
if [ $value_arg -eq 1 ]; then
value_arg_name=$argument
fi
fi
done
fi
# Plausibilitaets-Pruefung
for i in $(seq 0 $((${#value_args[@]} - 1))); do
value="${value_args[$i]}"
if [ $i -eq 0 ] || \
[ `echo "$value" | grep -e "^-" &> /dev/null; echo $?` -eq 0 ]; then
next_value="${value_args[$(( $i + 1 ))]}"
if [ -z "$next_value" ] || \
[ `echo "$next_value" | grep -e "^-" &> /dev/null; echo $?` -eq 0 ]; then
echo "ERROR: Missing value for option $value."
print_info
exit 1
fi
fi
done
#============================================================================
# Hier weiter...
#echo "#"$ARG_user"#"
#echo "#"$ARG_user_VALUE"#"
exit 0