Académique Documents
Professionnel Documents
Culture Documents
7005INT
Lecture 12
Announcements
Next week’s lecture topics (week 13):
– Revision
– Discussion of the final exam
– Course evaluation
– Attendance essential!
Shell programming/scripts
What is a shell?
A shell is a program that allows system users to
communicate with the operating system
The shell accepts commands from the system-
users and passes them to the system for execution
In UNIX, three shells are available:
– Bourne shell
– Korn Shell
– C Shell
UNIX Shells
The Bourne Shell was the first popular
UNIX Shell
It is called the ‘Bourne Shell’ because it was
written by Stephen Bourne
The Bourne Shell was written in the C
programming language and the executable
file (sh) is stored in the directory bin ( /bin/ )
UNIX Shells (cont.)
The C Shell was the second most popular UNIX
Shell after the Bourne Shell
It is called the ‘C Shell’ because control structures
for the C Shell are very similar to the C language
The Korn Shell is a mixture of the Bourne and C
Shells
It is called the ‘Korn Shell’ because it was written
by David Korn
Shell Programming
What is shell programming?
Shell programming is programming that
contains shell commands
A shell ‘program’ is called a Shell Script
Shell programs or Scripts do not need a
compiler because they contain shell
commands and can be executed from the
command prompt
Example: A simple C Shell Script
File name: list.csh
#!/bin/csh
finger
ls -a | more
In the above script, the first line signals that the
file list.csh contains a C Shell Script (csh)
To execute the above script we must change the
permissions of the file list.csh using the
chmod command as follows:
{/home1/staff/mblum} > chmod 700 list.csh
echo $programmingLanguages
– will display Pascal C LISP Java Ada on the screen
List Variables (cont.)
set colors=(red blue green)
– will create a list colors with three elements red,
blue and green
echo $colors[2]
– will display blue on the screen
#!/bin/csh
# create a list variable subjects
set subjects=(history, physics)
# add math to the list variable subjects
set subjects=($subjects math)
# display third element of the list
echo $subjects[3]
Input/Output Commands
The echo command displays information on the
screen
For example:
echo I am here
– will display "I am here" on the screen
#!/bin/csh
set myname = $<
echo $myname
Control Structures
The C shell offers three loop structures foreach,
while and repeat and three branching statements
if, switch and goto
foreach Loop
The foreach command allows a list of commands
to be repeated with a different variable value
The general form of the foreach loop is as
follows:
foreach .. end
foreach variable name(list)
commands
end
foreach loop
#!/bin/csh
foreach name (Bob Ben Bill)
echo $name
end
The above script will display:
Bob
Ben
Bill
Example: foreach loop
#!/bin/csh
set files=*.*
foreach filename ($files)
echo $filename
more $filename
end
while .. end
while (condition)
commands
end
Example: while loop
#!/bin/csh
set count = 1
echo $count
while ($count <= 10)
echo $count
@ count ++
end
For example:
repeat 3 echo CAT
– will display CAT three times
If Statement
The if statement has the following formats:
1) if (exp) command
2) if (exp) then
commands
endif
3) if (exp) then
commands
else
commands
endIf
Example if .. else statement
#!/bin/csh
set name1 = Bill
set name2 = Bart