#!/bin/bash # Script: template.sh # Task: Template for creating own scripts. Already contains # basic elements (usage-funkcion, parsing of commandline # options with getopts and predefined variables) # global variables SCRIPTNAME=$(basename ${0} .sh) EXIT_SUCCESS=0 EXIT_FAILURE=1 EXIT_ERROR=2 EXIT_BUG=10 # variables for option switches with default values VERBOSE="n" OPTFILE="" # functions function usage { echo "Usage: ${SCRIPTNAME} [-h] [-v] [-o arg] file ..." >&2 [[ ${#} -eq 1 ]] && exit ${1} || exit ${EXIT_FAILURE} } # the options -h for help should always be present. Options -v and # -o are examples. -o needs a parameter, indicated by the colon ":" # following in the getopts call while getopts ':o:vh' OPTION ; do case ${OPTION} in v) VERBOSE=y ;; o) OPTFILE="${OPTARG}" ;; h) usage ${EXIT_SUCCESS} ;; \?) echo "unknown option \"-${OPTARG}\"." >&2 usage ${EXIT_ERROR} ;; :) echo "option \"-${OPTARG}\" requires an argument." >&2 usage ${EXIT_ERROR} ;; *) echo "Impossible error. parameter: ${OPTION}" >&2 usage ${EXIT_BUG} ;; esac done # skip parsed options shift $(( OPTIND - 1 )) # if you want to check for a minimum or maximum number of arguments, # do it here if (( $# < 1 )) ; then echo "I need at least one argument!" >&2 usage ${EXIT_ERROR} fi # loop through all arguments for ARG ; do if [[ ${VERBOSE} = y ]] ; then echo -n "argument: " fi echo ${ARG} done exit ${EXIT_SUCCESS}