Operating Systems - Projects and Exercises

Shell Programming

The left arrow above will return you to the Home page
The left arrows below will return you to the Previous page

Go back top
Go back top

References:

‘UNIX for Programmers and Users’, Graham Glass & King Ables, Prentice Hall, 2nd edition, 1999, ch 6, pp. 197-236

tcsh: http://www.tcsh.org/Home

Go back top

What is a Shell Program?

After logging onto the system a prompt for input appears which is generated by a Command String Interpreter program called the shell. The shell

The shell can be used either

Note: Shell scripts are dynamically interpreted, NOT compiled.

Go back top

Common Shells

Identifying your shelltop

Type the following line:

echo $RANDOM

and then press the Enter key. If you get a blank line it’s the Bourne shell (sh). If you get the following line:

RANDOM: Undefined variable

it’s the C shell (csh or tcsh). If you get a five-digit random number, it’s the Korn shell (ksh) or BASH. To tell ksh from BASH, type

help

If you get a page of command descriptions, you have BASH. If not, you’ve got the Korn shell.

Go back top

Shell Scripts

Running a shell in the standard intreractive mode, you just type in the commands you want to be executed. However, you can execute your commands in "batch mode" by putting them into a normal text file. For example, a simple C Shell Script in file name list.csh

#! /bin/csh
ls -al | 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 can either execute it with csh:

csh list.csh

or you can execute it directly from the command line but you must first change the permission of the file list.csh using the chmod command so that it has execute privileges

(chmod +x list.csh).

topGeneral Shell Scripts

Glass - page 101

C Shell Commandstop

Glass - page 188

A simple command is a sequence of words, the first of which specifies the command to be executed. A series of simple commands joined by `|' characters forms a pipeline. The output of each command in a pipeline is connected to the input of the next. Simple commands and pipelines may be joined into sequences with `;', and will be executed sequentially. Commands and pipelines can also be joined into sequences with `||' or `&&', indicating, as in the C language, that the second is to be executed only if the first fails or succeeds respectively. A simple command, pipeline or sequence may be placed in parentheses, `()', to form a simple command, which may in turn be a component of a pipeline or sequence. A command, pipeline or sequence can be executed without waiting for it to terminate by following it with an `&'.

Variables

The C Shell offers a command "Set" to assign a value to a variable.

For example:

% set myname= Fred
% set myname = "Fred Bloggs"
% set age=20

where % is the command shell prompt

A $ sign operator is used to recall the variable values.

For example:

% echo $myname will display Fred Bloggs on the screen

A @ sign can be used to assign the integer constant values.

For example:

% @myage=20
% @age1=10
% @age2=20
% @age=$age1+$age2
% echo $age

List variablestop

% set programming_languages= (C LISP)
% echo $programming _languages
 
C LISP
 
% set files=*.*
% set colors=(red blue green)
% echo $colors[2]
 
blue
 
% set colors=($colors yellow)/add to list

Input/Output Commandstop

Echo command displays information on the screen.

For example:

echo I am here will display "I am here" on the screen.

$< command reads data from the keyboard

For example:

set myname = $< waits for data ( name)

EXAMPLE 1-write a script that reads name from the keyboard and display on the screen.

#!/bin/csh
set myname = $<
echo $myname

Control Structurestop

The C Shell offers three loop structures foreach, while and repeat and 3 branching statements if, switch and goto.

foreach .. end
foreach variable name(list)
	commands
end

EXAMPLE 2

1. foreach color  (red yellow green)
	    echo one color is $color
   end
2. set files = *.csh
   foreach filename=($files)
	    echo $filename
	    cat $filename
   end
   while .. end
   while (expression)
	    commands
   end

EXAMPLE 3

set count=1
while ($count <= 2)
 	echo $count
 	@ count++
end

repeat

repeat (expression) command
repeat 3 echo I am Ian

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 4

set myname = ian
If ($myname == ian) then
   echo "my name is Ian Graham"
else
   echo "my name is not Ian Graham"
endif  

switch .. case .. endsw

switch (expr)
   case pattern1:
      list
      breaksw
   case pattern2:
      ...
      breaksw
   default:
      defaultlist
      breaksw
endsw

goto

goto name
...
name:

Assigning Environmenttop

%setenv TERM vt100
%echo $TERM
 
vt100

String Operatorstop

The C shell supports the following operators:

==

Return true if the string operands are exactly equal

!=

Return true if the string operands are unequal

=~

Like ==, except that the right operand may contain wildcards

!~

Like !=, except that the right operand may contain wildcards

Example:

echo -n "Do you want to delete test.c?"
set answer = $<
If ($answer == "yes") then
   rm test.c
else If ($answer =~ a*) then 
   echo -n "Do you really mean that?"
   set answer = $<
   If ($answer == "yes") then
      echo "rm *.*"
   endif
endif

Arithmetic Expressionstop

!

logical negation

* / %

multiplication, division, remainder

+ -

|| && etc.,

Example:

set num1=5*6
set num2=6
If (num1>20 && num2==6) then
   echo $num1
endif
@ num1=5*6
set num2=6
if (num1>20 && num2==6)  then
   echo $num1
endif

Aliasestop

alias dir  ‘ls-a’
unalias dir

Useful Aliases

cd..

cd ..

rm

rm -i (force interactive mode)

rm

mv \!* ~/old

h

history

Command Execution Expressionstop

Command Re-execution

!!

last command

!number

command with the specified event number

!prefix

last command that started with prefix

 

topMetacharacters

{} - #enumerated variable length strings

rm/cp {file1,file2,file3}.txt

Input/Output Redirectiontop

The standard input and standard output of a command may be redirected with the following syntax:

< name
Open file name as the standard input.
<< word
Read the shell input up to a line which is identical to word.
> name
>! name
>& name
>&! name
The file name is used as standard output. If the file does not exist then it is created; if the file exists, it is truncated, its previous contents being lost. If the shell variable noclobber is set, then the file must not exist or be a character special file (e.g., a terminal or `/dev/null') or an error results. This helps prevent accidental destruction of files. In this case the `!' forms can be used to suppress this check. The forms involving `&' route the diagnostic output into the specified file as well as the standard output.
>> name
>>! name
>>& name
>>&! name
Like `>', but appends output to the end of name. If the shell variable noclobber is set, then it is an error for the file not to exist, unless one of the `!' forms is given.

topProtecting Files Against Accidental Overwrites

e.g.

cc test.c >& errors
set noclobber
cc test.c >& errors
 
File exists

temporarily override:

cc test.c >&! errors

topChanging Command Execution Priority

nice [-n number] command

Changes the execution priority by n. If no number is given, the priority is lowered by 10 (only a superuser can raise priority).

File-Oriented Expressionstop

Logical expression:

(-option filename)

1 (true) is returned if the selected option is true

0 (false) is returned if the selected option is false

0 if file name does not exist or is inaccessible

options:

r shell has read permission

w write permission

x execute

e file name exists

z file name exists and is zero byte in size

f file name is regular file(not a directory or special file)

d file name is directory file

EXAMPLE 1

echo -n ‘enter the name of the file
      you wish to erase’
set filename = $<
if (!(-w $filename) then
   echo you do not have permission
else
   rm $filename
   echo file erased
endif

EXAMPLE 2

set file_count=0
set dir_count=0
set filelist=*
foreach filename ($filelist)
   if (-d $filename)  then
      @ dir_count++
   endif
   if (-f $filename) then
      @ file_count++
   endif
end
echo $dir_count
echo $file_count

EXAMPLE - Menu

#!/bin/tcsh
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 = $<
   switch ($reply)
      case 1 : 
         date
         breaksw
      case 2 :
         who
         breaksw
      case 3 : 
         set stop=1
         breaksw
      default: 
         echo illegal choice
         breaksw
   endsw
end
Go back top

Process Control in the C-Shell

Go back top

For use only by students and instructors using the supplementary material available with the text book: "Operating Systems - Internals and Design Principles", William Stallings, Prentice Hall, 5th Edition, 2004. Not to be printed out or copied by any other persons or used for any other purpose without written permission of the author(s).

©