![]() |
![]() |
The left arrow above will return you to
the Home page
The left arrows below will return you to the Previous page
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
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
![]() |
![]() |
![]() |
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
- interprets the input,
- takes appropriate action, and
- finally prompts for more input.
The shell can be used either
- interactively - enter commands at the command prompt, or
- as an interpreter to execute a shell script
Note: Shell scripts are dynamically interpreted, NOT compiled.
![]() |
![]() |
![]() |
Common Shells
- C-Shell - csh
- The default on teaching systems
- Good for interactive systems
- Inferior programmable features
- Bourne Shell - bsh or sh - also restricted shell - bsh
- Sophisticated pattern matching and file name substitution
- Korn Shell
- Backwards compatible with Bourne Shell
- Regular expression substitution
- emacs editing mode
- Born Again Shell - BASH
- TENEX C-Shell - tcshh
- Based on C-Shell
- Additional ability to use emacs to edit the command line
- Word completion & spelling correction
Identifying your shell
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.
![]() |
![]() |
![]() |
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 | moreIn 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.cshor you can execute it directly from the command line but you must first change the permission of the file
list.csh
using thechmod
command so that it has execute privileges(chmod +x list.csh).
General Shell Scripts
Glass - page 101
- If the first line is just a
#
then the script is interpreted by a C shell:# This is a sample C shell script. Echo -n the date today is date # output today’s date.- If the first line is in the form
#! PathName
, then the executable programPathName
is used to interpret the script.#! /bin/ksh # This is a sample Korn shell. Echo -n the date today is date # output today’s date.- · If neither of the above rules apply, then the script is interpreted by a Bourne shell.
Echo -n the date today is date # output today’s date. # This would be interpreted by the Bourne shell.C Shell Commands
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=20where
%
is the command shell promptA
$
sign operator is used to recall the variable values.For example:
% echo $myname
will display Fred Bloggs on the screenA
@
sign can be used to assign the integer constant values.For example:
% @myage=20 % @age1=10 % @age2=20 % @age=$age1+$age2 % echo $ageList variables
% 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 listInput/Output Commands
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 keyboardFor 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 $mynameControl Structures
The C Shell offers three loop structures
foreach
,while
andrepeat
and 3 branching statementsif
,switch
andgoto
.foreach .. end foreach variable name(list) commands endEXAMPLE 2
1. foreach color (red yellow green) echo one color is $color end2. set files = *.csh foreach filename=($files) echo $filename cat $filename end while .. end while (expression) commands endEXAMPLE 3
set count=1 while ($count <= 2) echo $count @ count++ end
repeat
repeat (expression) command repeat 3 echo I am Ian
if
StatementThe
if
statement has the following formats:1) if (exp) command 2) if (exp) then commands endif 3) If (exp) then commands else commands endifEXAMPLE 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 Environment
%setenv TERM vt100 %echo $TERM vt100String Operators
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 wildcardsExample:
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 endifArithmetic Expressions
!
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 endifAliases
alias dir ‘ls-a’ unalias dirUseful Aliases
cd..
cd ..
rm
rm -i
(force interactive mode)
rm
mv \!* ~/old
h
history
Command Execution Expressions
Command Re-execution
!!
last command
!number
command with the specified event number
!prefix
last command that started with prefix
Metacharacters
{}
- #enumerated variable length strings
rm/cp {file1,file2,file3}.txt
Input/Output Redirection
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 variablenoclobber
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 ofname
. If the shell variablenoclobber
is set, then it is an error for the file not to exist, unless one of the `!
' forms is given.
Protecting Files Against Accidental Overwrites
e.g.
cc test.c >& errors set noclobber cc test.c >& errors File existstemporarily override:
cc test.c >&! errors
Changing Command Execution Priority
nice [-n number] commandChanges the execution priority by n. If no number is given, the priority is lowered by 10 (only a superuser can raise priority).
File-Oriented Expressions
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 fileEXAMPLE 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 endifEXAMPLE 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
#!/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
![]() |
![]() |
![]() |
Process Control in the C-Shell
;
%vi file.f ; job2 -? ; job3
|
%ps aux | grep igor | awk ‘{print $2}’
()
%(echo Status ; date ; who) > user_log
&&
or
||
%pc -o myprog.myprog && myprog
pc
; if successful, run myprog
)
%pc -c myprog.p || vi myprog.p
pc
; if unsuccessful, invoke vi
)
&
%matlab <input >& errors &
Can keep background job running after logout:
%nohup matlab <input >& errors &
Next login:
%ps aux | grep your_username
%jobs # list jobs ^Z # stops current foreground job ^C # abandons current foreground job %bg # restarts highest stopped job in # background mode %bg %2 # restarts job [2] in background mode %fg %ls # restarts job ls in foreground mode %kill %1 # terminate job %stop %1 # stop job
e.g.
% jobs % ls -R / >& /dev/null ... ^Z Stopped % jobs -l [1] + 8646 Stopped ls -R / >& /dev/null % bg [1] ls -R / >& /dev/null & % jobs [1] Running ls -R / >& /dev/null % fg %ls ls -R / >& /dev/null ^Z Stopped % jobs [1] + Stopped ls -R / >& /dev/null % find / -name '*.*' -print >& /dev/null ^Z Stopped % jobs [1] - Stopped ls -R / >& /dev/null [2] + Stopped find / -name *.* -print >& /dev/null % bg %2 [2] find / -name *.* -print >& /dev/null & % bg %1 [1] ls -R / >& /dev/null & % jobs [1] Running ls -R / >& /dev/null [2] + Running find / -name *.* -print >& /dev/null % kill %fi [2] Terminated find / -name *.* -print >& /dev/null % stop %1 % jobs [1] + Stopped (signal) ls -R / >& /dev/null % find / -name '*.*' -print >& /dev/null & [2] 8651 % jobs [1] + Stopped (signal) ls -R / >& /dev/null [2] - Running find / -name *.* -print >& /dev/null % kill %1 % jobs [1] Terminated ls -R / >& /dev/null [2] + Running find / -name *.* -print >& /dev/null % fg %2 find / -name *.* -print >& /dev/null ^C
![]() | ![]() | ![]() |
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).
©