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:
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







