leestretton.com

You are in: > Home > Developers Guides

Shell Scripting - Language Constructs

The shell script language, like other programming languages, has constructs for conditional execution (if-then-else; while), iterative execution (for loop), a switch statement, and a goto statement:

1. if-then-else

The syntax of the if-then-else construct is

if ( expr ) simple-command

or

if ( expr ) then
    commandlist-1
[else
    commandlist-2]
endif

The expression expr will be evaluated and according to its value, the commandlist-1 or the commandlist-2 will be executed. The portion of the construct enclosed in '[' and ']' is optional.1

As an example, suppose we write a shell script which is supposed to have two parameters, and that the code will set up two variables, `name1' and `name2' from those two parameters, i.e.

set name1 = $argv[1]
set name2 = $argv[2]

(which presumably it would make use of later on). But suppose we also wish to do error-checking, emitting an error message if the user gives fewer than two, or more than two, parameters. We could use the following code

if ($#argv <> 2) then
    echo "you must give exactly two parameters"
else
    set name1 = $argv[1]
    set name2 = $argv[2]
endif

2. while

The syntax of while loop construct is

while ( expr ) 
    commandlist
end

The commandlist will be executed until the expr evaluates to false.

3. foreach

The syntax of foreach loop construct is

foreach var ( worddlist )
    commandlist
end

The commandlist is executed once for each word in the wordlist, and each time the variable var will contain the value of that word. For example, the following script can search all immediate subdirectories of the current directory for a given file (and then quit if it finds one):

#!/bin/bash
     set f = $1
     foreach d (*)
         if (-e $d/$f) then
               echo FOUND: $d/$f
               exit(0)
         endif
     end
     echo $f not found in subdirectories

For example, say I call this script FindImm, and my current directory consists of files s, t and u, with s and t being subdirectories, and with t having a file x. Typing

FindImm x

would yield the message

FOUND: t/x

Here is how it works: In the line

foreach d (*)

The `*' is a wild card, so it would expand to a list of all files in my current directory, i.e. the list (s t u). So, the for-loop will first set d = s, then d = t and finally d = u.

In the line

if (-e $d/$f) then

the -e means existence; in other words, we are asking if the file $d/$f exists. If we type `FindImm x' as in the example above, $f would be x, and $d would start out as s, so we would be asking if the file s/x exists (the answer would be no).

4. switch

The switch command provides a multiple branch similar to the switch statement in C. The general form of switch is:

    
switch ( str )
    case string1:
        commandlist1
        breaksw
    case string2:
        commandlist2
        breaksw
    default
        commandlist
    endsw

The given string str is successively matched against the case patterns. Control flow is switched to where the first match occurs. As in file name expansion, a case label may be a literal string, or contain variable substitution, or contain wild-card character such as *,?, etc.

5. Goto

The goto command provides a way to branch unconditionally to a line identified by a label.

goto lab

where lab is a label on a line (by itself) somewhere in the script in the form

lab:

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css