Make your own free website on Tripod.com

BigCalm : AIX UNIX : Shell Script Help
Search

Shell Script Help


"I think there is a world market for maybe five computers." -- Thomas Watson, chairman of IBM, 1943.


Introduction Variables If Statements For and While
Case statements Changing Information


Introduction

There are various bits and bobs used in shell scripts which are not normally (but can be) used on the command line...all examples are written as command line arguments but are not usually done this way. If you're trying to learn shell scripts, please type the commands to see the output - it'll be much easier! For normal command reference see This Page

Simplest example - the hello world script...(try it)

$ echo " Hello world "
Hello world


Variables

Suppose we aren't sure what we want to echo to the screen - we can use a variable:
$ THIS_IS_A_VARIABLE="Hello World"
$ echo " ${THIS_IS_A_VARIABLE} "
Hello world

All variables are type char or int, and there are ways to read the output of commands into variables by using `` back-quotes. Once you have set a variable to reference it use a '$' symbol and the variable name. If you need to then you can encase the variable name in {} to make sure that the shell will correctly interpret which variable you are trying to use. For example
$ CURRENT_TTY=`tty`
$ echo ${CURRENT_TTY}
/dev/pts/5

this will read the value returned by the command tty into the variable CURRENT_TTY

There are also special variables:

Commands for manipulating variables....

If Statements

'if' is used to check various things. It is usually used to check the value of variables, e.g. I want to know if the current user is using the dial-in modem (tty647) I can check this with the following script
$ WHICH_ONE=`tty`
$ if [ "${WHICH_ONE}" = "/dev/tty647" ]
> then
> echo "You are on a modem"
> else
> echo "You are not on a modem"
> fi

You can also check the success of the last command...
$ if [ $? -ne 0 ]
> then
> echo "Failure of last command"
> fi

Notes on if statements - they all take the form:
	if [ <condition> ]
	then
		do these statements
	else
		do these other statements
	fi
The else part is optional. The 'if' statement uses the command 'test' for its purposes - 'if' is only available in the korn + bourne shells (well, this is not strictly true, but for other shells (csh, tcsh) the syntax differs). The conditions that can be included are:
if [ ! condition ] If condition is not true
if [ -f $FILENAME ] If file exists called $FILENAME
if [ -d $DIRNAME ] If directory exists called $DIRNAME
if [ -x $FILENAME ] if file $FILENAME is executable
if [ -w $FILENAME ] if file $FILENAME is writable
if [ -r $FILENAME ] if file $FILENAME is readable
if [ -s $FILENAME ] if file $FILENAME exists and is of greater than zero size.
if [ $VAR1 -ne $VAR2 ] If VAR1 does not equal VAR2
if [ $VAR1 -gt $VAR2 ] If VAR1 is greater than VAR2
if [ $VAR1 -lt $VAR2 ] If VAR1 is less than VAR2
if [ $VAR1 -eq $VAR2 ] If VAR1 is equal to VAR2
if [ $VAR1 -ge $VAR2 ] if VAR1 is greater than or equal to VAR2
if [ $VAR1 -le $VAR2 ] if VAR1 is less than or equal to VAR2
if [ "$VAR1" = "$VAR2" ] If VAR1 is equal to VAR2 (for strings)
if [ "$VAR1" != "$VAR2" ] If VAR1 does not equal VAR2 (strings)
if [ ! "$VAR1" ] If VAR1 is null
if [ "$VAR1" ] if VAR1 is not null

This is only a summary of possible conditions - see 'man test' for more information.

for and while

Both for and while are similar to if in syntax, but allow looping....
Structure:
while [ <condition> ]     # condition functions exactly the same as if statements
do
	statements
done

for VARIABLE in <string or command>
do
	statements
done

Examples....

$ while [ "$1" ] # While argument 1 is not null
> do
> echo $1 # echo it to the screen
> shift # Let $1 = $2, let $2 = $3, let $3 = $4, etc.
> done

$ for i in 0 1 2 3 4 5 6 7 8 9
> do
> echo "Counting up from 0 to 9: $i"
> done
$

$ for i in `find . -name "*.4gl" -print`
> do
> grep -i "SCROLL CURSOR" $i
> done # Finds all files which end in '.4gl' which contain the text "SCROLL CURSOR"

hints on for: for picks up the words released by the command in `` back-quotes therefore, things like 'for i in `ls -l` ' will probably be non- sensical. (e.g. first $i will be '-rw-rw-rw', second will be '1', third will be 'root',etc..). If you are forced to use something like this, a counter (see the command 'expr') and a case statement may help you.

case statements

Most often used when checking options that have been passed. Allows you to use the case statement instead of multiple ifs. The form is...

case $VARIABLE in
	"<text>")	statements ;; # The semicolons ARE important
	"")		statements if blank;;
	*)		default statements ;;
esac

For example, if we want to check if $1 is a valid argument...
$ EXITFLAG=0
$ case $1 in
> "-A") SETOPTION="A" ;;
> "-G") SETOPTION="G" ;;
> -?) echo "Invalid argument"
> EXITFLAG=1
> ;;
> "") echo "Usage: farch [-A|-G] FILENAME"
> EXITFLAG=1
> ;;
> *) PASSEDFILENAME=$1 ;;
> esac
$ if [ ${EXITFLAG} -eq 1 ]
> then
> exit 1
> fi

Notes on case: The order that you write case statements is very important.e.g.
$ case $1 in
> *) echo "This will always be executed, even if $1 is '-G'" ;;
> "-G") echo "This will never be executed as the one above takes precedence" ;;
> esac

Changing and selecting returned information from shell commands

The nasty, hard stuff you can't do without....
There are several commands to help you extract information from other commands the main ones are echo,awk,sed,ex (not often used explicitly), expr, read

Let's start with the easy ones...read

read VARIABLE - reads a variable from stdin. e.g.
$ read a
$ echo $a
# will echo whatever has been entered

echo..... (some hints)
Throughout this document, echo has been used so you should hopefully be pretty familiar with the syntax, so this is just some hints on use:



line requests the user to press return. e.g.
$ echo "Press return to continue\c" ; line


Ok, now for expr...

expr is used generally to perform maths functions on existing variables. Immensely useful in while loops and suchlike where a for is inappropriate e.g.
$ COUNT=`expr $COUNT + 1` # will add 1 to the variable count
$ LENGTH=`expr $VARIABLE : ".*"` # will return the length of the variable. Note that this doesn't work if $VARIABLE has zero length.
there are many other far nastier ones, but I've covered them elsewhere, such as 'awk' and 'sed'


tr....

tr is a dead simple, but very useful command - it simply 'tr'anslates characters e.g.
$ echo "Translate$the$dollars$to$spaces" | tr "$" " " '
Translate the dollars to spaces



Please see the Awk help page for information on awk.


----- End of shellscripthelp file --------


Valid HTML 4.0!
This Page Was Last updated 28/09/01 Copyright Jonathan Daniel 2001