Tag Archives: Linux

Bash Example Script #2

I have added the following function to my bash example script:

# Run external command and only procced, when exit code is 0
function try {
	$@
	returnValue=$?
	echo "$returnValue"
	if [ $returnValue -ne 0 ]
	then
		log $ERROR "$@ exited with return value \
		$returnValue Script will stop here."
		echo "$@ exited with return value $returnValue. \
		Script will stop here."
		exit $EXIT_FAILURE
	fi
}

The function takes a program as an argument and runs in. If the program returns with a status other then 0, so it fails, then the script will print an error message, log it and and exits. I needed this behaviour in some recent scripts and hope it to be usefull for everybody else.

bash_example

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.

Banshee 1.8

Right now, my package manager pacman (I am using Arch Linux) updated the music player Banshee. The last stable version 1.6.1 was updated to version 1.8.0.

The first things I recognized are:

  • Some new menu entries on the left, as Amazon MP3-Shop and Miro Guide
  • In the “tools” menu there is a new entry “correct meta-data”, a tool that easily removes typos in artists, albums or genres automatically
  • The symbol in the gnome panel has its transparent background back
  • When you listen to an audiobook and stop it, the place where you are is bookmarked automatically. Next time you can easily start at the same position.

Some nice improvements. Well done!