Bash Example Script

These days I use bash more and more often for simple tasks on my Linux system, as well as for a University course I am taking.

Have a look at the script:

#!/bin/bash
# Skript: bash_example.sh
# Credit to: http://www.pro-linux.de/artikel/2/111/
#1,ein-shellskript-template.html
#
# I have added some usefull functions that I am using in many
# of my scripts
# Find this script here: http://phynformatik.de
# Janosch Maier
#
# This work is licensed under the
# Creative Commons Attribution-Share Alike License
#
# You are free
# to Share — to copy, distribute and transmit the work
# to Remix — to adapt the work
# to make commercial use of the work
#
# Under the following conditions:
# Attribution — You must attribute the work in the manner
# specified by the author or licensor (but not in any way
# that suggests that they endorse you or your use of the work).
# Share Alike — If you alter, transform, or build upon this
# work, you may distribute the resulting work only under the
# same or similar license to this one.
version="0.1"
lastChange="14.12.2011"

#### CONFIG - DO NOT MAKE CHANGES HERE ####
# PATH has to be a trusted value
# Try to make this script save in unsave environments
# "." cannot be in PATH
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin
export PATH

# Environment variable IFS should always be
# "" or undefined
# Same as with PATH
if [ -z "$IFS" ]
then
        IFS="
"
        export IFS
fi

#### YOU CAN START MAKING CHANGES FROM HERE ON ####
# Define global variables for later use
# Name of the script is its filename (without possible suffix .sh)
SCRIPTNAME=$(basename $0 .sh)
# Define Exit values
EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10
# Logfile will be put under current working directory
LOGFILE=`readlink -f "$SCRIPTNAME.log"`

# Error values
ERROR="ERROR"
WARNING="WARNING"
INFO="INFO"
NOTICE="NOTICE"

# Default values for options
VERBOSE=false
DEBUGLEVEL=1

#### FUNCTIONS FOR LATER USE ####
# Print usage message
function usage {
	echo "Usage: $SCRIPTNAME [-h] [-v] [-d debuglevel] file ...

  -h		show this help
  -v		be verbose
  -d		set debug level (1-9)
	[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}

# Log stuff to the defined logfile
# Call "log $ERROR|$WARNING|$INFO|$NOTICE message"
function log {
	case $1 in
		$ERROR)
			if [ $DEBUGLEVEL -ge 1 ]
			then
				shift
				echo "$(date '+%F %T') $ERROR: $@" 
                                >> $LOGFILE
			fi
			;;
		$WARNING)
			if [ $DEBUGLEVEL -ge 3 ]
			then
				shift
				echo "$(date '+%F %T') $WARNING: $@" 
                                >> $LOGFILE
			fi
			;;
		$INFO)
			if [ $DEBUGLEVEL -ge 5 ]
			then
				shift
				echo "$(date '+%F %T') $INFO: $@" 
                                >> $LOGFILE
			fi
			;;
		$NOTICE)
			if [ $DEBUGLEVEL -ge 7 ]
			then
				shift
				echo "$(date '+%F %T') $NOTICE: $@" 
                                >> $LOGFILE
			fi
			;;
		*)
			echo "$(date '+%F %T') BUG: $@" 
                        >> $LOGFILE
			;;
	esac
}

# Echo a string if verbose mode is set
# Log as an Info message in any case
function echo_verbose {
	if [ "$VERBOSE" == "true" ]
	then
		echo $@
	fi
	log $INFO "$@"
}

# Echo a string and put it to Error log
function echo_error {
        log $ERROR "$@"
        echo "$ERROR: $@" >&2
}

#### GETTING ALL OPTIONS #####
# Option -h gives some help
# Option -v makes the script verbose
# Option -o has one option argument therefore o:
while getopts ':o:d:vh' OPTION ; do
	case $OPTION in
		v) VERBOSE=true
		;;
 		h) usage $EXIT_SUCCESS
 		;;
		d) DEBUGLEVEL="$OPTARG"
		;;
 		?) echo "Unknown Option "-$OPTARG"." >&2
 		usage $EXIT_ERROR
 		;;
		:) echo "Option "-$OPTARG" needs an argument." >&2
		usage $EXIT_ERROR
		;;
		*) echo "There should never happen..." >&2
		usage $EXIT_BUG
		;;
	esac
done

#### DO CHECKS ON THE ARGUMENTS ####
# $OPTIND is number of options gotten by getopts + 1
# Therefore shift arguments gotte by OPTIND - 1
shift $(( OPTIND - 1 ))

# Check if the number of arguments gotten is correct
if (( $# < 1 || $# > 2)) ; then
	echo "One or two arguments needed." >&2
	log ERR "1 or 2 arguments needed. $# arguments gotten"
	usage $EXIT_ERROR
fi

#### DO SOME STUFF HERE ####
# Do something for all other arguments
for ARG ; do
	echo_verbose "Doing stuff for an argument"
	echo $ARG
done

# The script succeded doing its work
exit $EXIT_SUCCESS

 

I have tried to comment the script as good as possible. Explanation for some parts of the script can be found at the page the base for this script originates from. Feel free to ask questions, if something is not understandable.

 

Download the script: bash_example.sh.

2 thoughts on “Bash Example Script”

Comments are closed.