Vous êtes sur la page 1sur 26

Software Development 1:

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

 Run the script as follows:


 {/home1/staff/mblum} > ./list.csh
Variables
 The C shell offers a command "set" to assign a
value to a variable
 A $ sign operator is used to recall the variable
values
 Example: Assign a value to a variable
set myname = John
echo $myname will display John on the screen

set myname = “John Howard"


echo $myname will display John Howard on the screen

 The @ sign can be used to assign integer constant


values:
@ number = 10
Example: Assigning/Recalling
integer variable values
#!/bin/csh
@ myage = 20
@ age1 = 10
@ age2 = 20
@ age = $age1 + $age2 + $myage
echo $age

 The output of the above script will be 50


List Variables
 The C shell allows users to create and access list
variables
 Some examples are given below:
set programmingLanguages= (Pascal C LISP Java Ada)
– will create a list programmingLanguages with five
elements: Pascal, C, LISP, Java, Ada

 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

 set colors=($colors yellow)


– will add yellow to the list colors
Example: Create and display a list

#!/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

 The $< command reads data from the keyboard


 Example:
set myname = $<
– waits for data ( name )
Input/Output Example
 Example: Write a script that reads a name
from the keyboard and displays it 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

 This script will display the names and contents of


all files in the current directory
While loop
 The commands between the while and end are
executed as long as condition is true
 The general form of the while loop is as follows:

 while .. end

while (condition)
commands
end
Example: while loop
#!/bin/csh
set count = 1
echo $count
while ($count <= 10)
echo $count
@ count ++
end

 The above script will display the numbers from 1


to 10
Repeat Loop
 The repeat loop allows the execution of a single
command a specified number of times
 The syntax for the repeat loop is as follows:

repeat (expression) command

 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

if ($name1 == $name2) then


echo name1 and name2 are the same
else
echo name1 and name2 are different
endif
The case Statement
 The syntax for the switch and case statements are
as follows:
switch .. case .. endsw
switch (expr)
case pattern1:
list
breaksw
case pattern2:
finger
breaksw
default:
defaultlist
breaksw
endsw
Example: Write a script that
displays a menu
 The following C shell script displays a
menu of options (date, who, exit)
 The date and time will be displayed on the
screen if you select option 1, a list of all
logged on users will be displayed if you
select 2 and if you select 3, you will exit
from the script
Example (cont.)
#!/bin/csh
clear
echo menu test program
set stop = 0
while ($stop == 0)
echo 1 : date and time
echo 2 : who is logged on
echo 3 : exit
echo -n 'your choice '
set reply = $<
:
Example (cont.)
:
switch ($reply)
case 1 :
date
breaksw
case 2 :
who
breaksw
case 3 :
set stop=1
breaksw
default:
echo illegal choice
breaksw
endsw
end

Vous aimerez peut-être aussi