Batch programming

The bash (borne again shell) installed at the workstations in the ASIC-Lab is used to communicate with the operating system.

After you got your account the shell you use is /bin/bash you might also change this to another shell you like more using the command chsh newShellName.

The setup files you should use for the bash are described in the setup page.

Here you find some simple examples to batch programming with the bash shell.

Writing and executing a batch file

There are much more shells you might use to execute your commands like perl, sh, csh, ksh, wish. The first line in your file must contain the path to the interpreter you want to use starting with #!

Example:

 #!/bin/bash

There are then two ways to execute your batch script then. The first is to make the script executable using the command chmod u+x. Here the commands are executed from the current shell.

Or the second interpret it with an extra shell bash batch file

Some examples to the batch language used in bash programming


The examples explain only the very basic structures of the script language.

Functions

#!/bin/bash
#	Michael Keller
#	Example to define and call a function
#
function fun() {
	echo" How to define and call a function"
} 
# the call of the function
fun 

Lists

#!/bin/bash
#
# Example for handling lists
#
# get the first argument and assign it to ARG
ARG1=$1
# if ARG is not set use 2 (to assign $1 direct to MEM with MEM=${$1:-2} won't work
MEM=${ARG1:-2}
# create a list using the ls command
flist=(`ls *`)
# loop over the list
for file in ${flist[@]}
do
	echo $file
	echo -e"\t"`basename $file .sh`
done

echo "the "$MEM"'th member of list is <"${flist[$MEM]}">"

Arguments and positionale parameters

#!/bin/bash
# examle how to handle options
#
while test $# != 0
do
	echo $1
	shift
done
# leave the programm
exit
#---------------- Cut Here ----------------------------------
# or do it more sophisticated
#Number of arguments
echo $#
#List of Arguments
echo $@
#one Argument out of the List
echo $2
# getopts shell builtin getopts optstring name [args]
#
# : option has arguments
#
OPTS=a:b:c:h
while getopts "$OPTS" ARG
do
	case "$ARG" in
	a)
		echo "Option a was was specified with $OPTARG"
		aARG=$OPTARG
		;;
	b)
		echo "Option b was was specified with $OPTARG"
		bARG=$OPTARG
		;;
	c)
		echo "Option c was was specified with $OPTARG"
		cARG=$OPTARG
		;;
	h)
		echo "Help was requested"
		usage
		;;
	esac
done

function usage() {
	echo "How to define and call a function"
}

Looping Constructs

#!/bin/bash
#
# Michael Keller
# Example for the loop constructs in bash
#

# to create a list use VarName=()
list=(1 2 3 4 5 6 7)

cARG=5
#
num=0
while [ $num -lt 100 ];
do
	num=$((num + $cARG))
	echo $num
done
echo "---------------------------------"
#
num=0
until [ $num -gt 100 ];
do
	num=$((num + $cARG))
	echo $num
done
echo "---------------------------------"
for c in ${list[@]}
do
	echo $c
done

Testing

#!/bin/bash
#
# Michael Keller
# Example for testing
#
a=$1
b=$2
if [ $a -eq $b ]; then
	echo "a is equal to b"
elif [ $a -gt $b ]; then
	echo "a is bigger than b"
else
	echo "a is not equal to b"
fi
echo "-------------------------"
echo
echo -n " enter the name of an animal: "
read ANIMAL
echo -n "the $ANIMAL has "
case $ANIMAL in
	horse | cat | dog)
		echo -n "four (4) "
	;;
	man | goose)
		echo -n "two (2) "
	;;
	*)
		echo -n "an unknown (?) number "
esac
echo "legs"