shell programming control structures

 

Control Constructs

The flow of control within SH scripts is done via four main constructs; if...then...elif..else, do...while, for and case.

If..Then..Elif..Else

 

This construct takes the following generic form, The parts enclosed within ([) and (]) are optional:

 

if list then list [elif list

then list] ... [else list] fi

 

 

When a Unix command exits it exits with what is known as an exit status, this indicates to anyone who wants to know the degree of success the command had in performing whatever task it was supposed to do, usually when a command executes without error it terminates with an exit status of zero. An exit status of some other value would indicate that some error had occurred, the details of which would be specific to the command. The commands' manual pages detail the exit status messages that they produce.

 

A list is defined in the SH as "a sequence of zero or more commands separated by newlines, semicolons, or ampersands, and optionally terminated by one of these three characters.", hence in the generic definition of the if above the list will determine which of the execution paths the script takes. For example, there is a command called test on Unix which evaluates an expression and if it evaluates to true will return zero and will return one otherwise, this is how we can test conditions in the list part(s) of the if construct because test is a command.

We do not actually have to type the test command directly into the list to use it, it can be implied by encasing the test case within ([) and (]) characters.

 

Numerical Comparision

 

Symbol

Description

Usage

-eq

Equals

$A -eq $B

-ne

Not equal

$A -ne $B

-lt

Less than

$A –le $B

-le

Less than or equal to

$A -le $B

-gt

Greater than

$A -ge $B

-ge

Greater than or equal to

$A –ge $B

 

String Comparision

 

Symbol

Description

Usage

=

Equals

$A = $B

!=

Not Equal

$A != $B

-n

Not a null string

-n $str

-z

Null String

-z $str

 

Testing For File

 

Symbol

Description

Usage

-r

Read Permission

-r $filename

-w

Write Permission

-w $filename

-x

Execution Permission

-x $filename

-f

File Exists

-f $filename

-d

Directory Exits

-d $filename

-c

Special Character File

-c $filename

-b

Block Special File

-b $filename

-s

Size of File is not zero

-s $filename

 

 

Do...While

 The Do...While takes the following generic form:

while list do list done

In the words of the SH manual "The two lists are executed repeatedly while the exit status of the first list is zero." there is a variation on this that uses until in place of while which executes until the exit status of the first list is zero

 

For

 The syntax of the for command is:

 for variable in word ... do list

done

 

The SH manual states “The words are expanded, and then the list is executed repeatedly with the variable set to each word in turn.”. A word is essentially some other variable that contains a list of values of some sort, the for construct assigns each of the values in the word to variable and then variable can be used within the body of the construct, upon completion of the body variable will be assigned the next value in word until there are no more values in word.

 

Case

 The case construct has the following syntax:

case word inpattern) list ;;

...

 esac

An example of this should make things clearer:

!#/bin/sh case $1 in

1)  echo 'First Choice';;

2)  echo 'Second Choice';;

*) echo 'Other Choice';; esac

 

 "1", "2" and "*" are patterns, word is compared to each pattern and if a match is found the body of the corresponding pattern is executed, we have used "*" to represent everything, since this is checked last we will still catch "1" and "2" becausethey are checked first. In our example word is "$1", the first parameter, hence if the script is ran with the argument "1" it will output "First Choice", "2" "Second Choice" and anything else "Other Choice". In this example we compared against numbers (essentially still a string comparison however) but the pattern can be more complex,

 

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction