Title: TDC368 UNIX and Network Programming
1TDC368UNIX and Network Programming
- Week 9
- Introduction to UNIX Shells
- C-Shell Interactive Commands
- C Shell Programming
-
- Camelia Zlatea, PhD
- Email czlatea_at_cs.depaul.edu
2References
- Ellie Quingley, UNIX Shells by Example, Prentice
Hall PTR, NJ, 1997 chap.9 (pp.317-414) - Dave Curry, UNIX Systems Programming for SVR4,
O'Reilly Assoc., Sebastopol, CA, 1996. - UNIX in a Nutshell , by Daniel Gilly, OReilly
Associates, Inc. , section 5.
3Introduction to UNIX Shells
- shell the program interface between the user
and the kernel functions - reading user input and parse the command line
- managing processes and jobs redirection, pipes
- handling signals
- executing scripts
- System Startup
- process init (pid1), the first process to run
after the system boots will fork and exec a
program that handle terminals (getty). - getty activates the terminal port stdin, stdout
and stderr. - same process executes next the command
"/bin/login" prompts for user name and password
validate the input by reading the correspondent
entry from /etc/passwd setup working environment
- executes user predefined shell (ex. "/bin/csh")
- Environment Initialization (C Shell)
- C shell executes the file .cshrc (this is
executed anytime when a new C shell is started) - C shell executes .login file
- C shell prompt is displayed as a fact that shell
is waiting to process user command
4Introduction to UNIX Shells
- ? How to change your login shell passwd e (on
HP) - File .cshrc contains C shell variables settings
- Example
- set history20 previous commands to
remember. - set savehist20 number to save across
sessions. - set systemhostname name of this
system. - set prompt "system\gt " command prompt.
- set autologout20
- alias l ls
- alias info (who am i date )
5Introduction to UNIX Shells
- File .login contains environment variables and
terminal settings this information is inherited
by shells children processes. - Example
- setenv TERM ansi
- umask 077
- echo Hello LOGNAME
- echo The date is 'date'
- echo Your home shell is SHELL
- echo Good-bye LOGNAME
-
- ? How to re-execute a modified .chsrc or .login ,
in the current shell - source .login or source .cshrc
6C-Shell Interactive Commands
- Command Line
- C shell status variable returns is set to the
exit status (0 to 255) of the last executed
command (0 success) - Sequential execution of commands ()
- Example who am i pwd date
- Conditional Execution ( )
- Example grep czlatea /etc/passwd talk
czlatea - if command grep is successful (status 0) then
mail command is executed - Example grep tdc368 /etc/passwd echo "user
tdc368 unknown" - Pipelined Execution ()
- ls l sort
- Background Execution ()
- man csh cat gtcsh.doc
- Command line history history
- !! re-executing last command
- !6 the sixth command from history is executed
7C-Shell Interactive Commands
- Command Line Arguments
- !1 first , !2 second , !3 third ..
- ! - last , ! - first , ! - all arguments
- Example
- echo a b c d
- !0 ! re-execute the last command (echo)
with only its last argument - Job Control
- jobs
- Bg
- fg
- suspend jobs (Z)
- Metacharacters
- ls a list all files starting wirh a
- ls aA list all files starting with a or A
- ls 0-9 list all file ending with a digit
- ls .0-9 list allfiles ending with a
period followed by a digit - ls ?? list all files with two-character name
- ls A-ZA-ZA-Z list all files with three
uppercase character name - ls 11,22 list all files ending in 11 or 22
8C-Shell Interactive Commands
- Redirectation , Pipes
- ( gt , lt , gt, gtgt, gtgt , ltlt HERE, ,
- Example Here Document
- cat ltlt HERE
- This is a test for input re-directation.
- This show a here document.
- HERE
- the output of command cat is the "here document"
from first HERE to second HERE
9C-Shell Interactive Commands
- Redirectation of both output and error standard
files gt - ls -l grep May gt outfile
- separate redirectation of stderr and stdin
- ( ls -l grep May gt result1 ) gt error
- C shell process forks a child to execute the
above command - The child process redirects its stderr and stdout
to file errors - The child process forks a new process
- The new process executes the command file in ( ..
) it inherits the file descriptors of its
father it redirects again its stdout to file
results.
10Variables in C Shell
- Local variables visible in the shell were was
defined - Environment variables visible in the shell and
all processes spawned by this shell - Example
- set history 50 set the build-in variables
- set savehist 10 history and savehist
- set person Doe set user variable username
- set machine uname n and machine
- echo ?machine test to see if variable machine
is defined (set) - echo machine echo the value of variable
machine, if it is defined - set var noon user variable
- echo aftervar
- set var "Hello World"
- echo var
- setenv PERSON"Joe Doe" set an environment
variable - echo PERSON
- set displays all local variables set for the
current shell - env display all environment variables
- setenv
11Arrays and Strings
- Examples
- set persons (Joe Liz Tom Dan) an array
variable - echo persons display the variable
- echo persons same
- echo persons display the number of elements
- echo personspersons display the last
element - echo persons1 display first element of the
array - set persons1 John reassign first element
- echo persons3-4 list elements 3 and 4 of
the array - echo persons10 out of range
- shift persons shift one position to the left
the array values - set days "Monday Tuesday Wednesday" a string
variable, not an array - set days (days) converts the string into an
array - set var1 date the output of the command
date is stored as an array - set var2 date" the output is stored as a
single string
12Special variables
- - displays the PID of current shell
- lt - accepts input from the user
- Backslash Character - escape the interpretation
of a single character (ex. ?, !, newline)
13C-Shell Programming
- Shell Scripts
- The magic number - !/bin/csh first line of the
script - Examples
- !/bin/csh
- echo Hello LOGNAME
- echo The date is date
- echo Your home shell is SHELL
- echo "This machine is uname n"
- echo The calendar for this month is
- cal 5 2003
- echo The processes you are running are
- ps -al grep " LOGNAME"
- echo "Thanks for coming. See you soon\!\!"
- echo Good-bye LOGNAME
14Debugging scripts set/unset echo
- set/unset verbose
- !/bin/csh
- echo Hello LOGNAME
- echo The date is date
- set verbose
- echo Your home shell is SHELL
- unset verbose
- echo Good-bye LOGNAME
15Variables
- set varname assignment
- lt variable reading user input
- Numeric Variables - Arithmetic Expressions
- Operators - / ltlt(left shift) gtgt (right
shift) - , -, , / , , --
- Examples
- _at_ var 5 3
- echo var
- _at_ sum 1 same with _at_ sum _at_ sum 1
- echo sum
16Command Line Arguments
- 0, 1, .., 9 , 10, 11, ..
- - all arguments
- argv1, argv2, . first, second arg. ..
- argv number of arguments
- argv all arguments
- Example
- !/bin/csh -f
- The greetings script
- This script greets a user whose name is typed
in at the command line. - echo 0 to you 1 2 3
- set d date
- set dd d1-3
- echo Welcome to this day dd
- echo Hope you have a nice day, argv1 \!
- echo Good-bye argv1 argv2 argv3
- greetings Joe Doe
17Decisions
- if , if-else, switch
- Examples Conditions and File testing
- !/bin/csh -f
- Script name logical
- set x 1
- set y 2
- set z 3
- if (( "x" "y" ) ! "z" ) then grouping
- echo TRUE
- else
- echo FALSE
- endif
18Example
- !/bin/csh -f
- Scriptname filecheck
- Usage filecheck filename
- set file1
- if ( ! -e file ) then
- echo "file does not exist"
- exit 1
- endif
- if ( -d file ) then
- echo "file is a directory"
- else if ( -f file ) then
- if ( -r file -x file ) then nested if
construct - echo "You have read and execute permission on
file file. - endif
- else
- print "file is neither a plain file nor a
directory. " - endif
- endif
19Example system type Testing
- !/bin/csh -f
- Program to determine the type of system you are
on. - echo "Your system type is "
- set release uname -n echo release
- switch ( uname -s)
- case SunOS
- echo SunOs
- breaksw
- case HP-UX
- echo HP
- breaksw
- case Linux
- echo Linux
- breaksw
- endsw
20Loops
- foreach variable (list)
- commands
- end
- Example
- !/bin/csh -f
- foreach file (.o)
- rm file
- end
- while (condition)
- Command
- end
- Example
- !/bin/csh -f
- Script is called loop.args
- while ( argv )
- echo argv
- shift
- end
condorgt csh t2 a b c d e a b c d e
b c d e c d e
d e e
condorgt
21Loop
- repeat n commands
- !/bin/csh -f
- while ( argv )
- echo argv
- shift
- end
- !/bin/csh -f
- while (1)
- echo "Hello, in 1st loop"
- while (1)
- echo "In 2nd loop"
- while (1)
- echo "In 3rd loop"
- repeat 3 break
- end
- end
- end
- echo "Out of all loops"
22Signals Handling
- An INT signal (ctrl-C) can be caught only from
with a script - !/bin/csh
- onintr done install signal handler label
done - sleep 30 other scripts commands
- echo Bye noINT
- exit
- done signal handler
- echo hello int
- onintr - disable interupts
- ls -l
- echo Bye
23Setting Executable Scripts
- setuid scripts
- chmod usx script_file
24A C shell script to add new data entries to a
phone list file 1/3
- !/bin/csh -f
- set phonefile phone.list
- startover
- while (1)
- echo "First Name (stop to exit) "
- set fname lt
read input variable - if (fname SsTtOoPp) then
- break exit if
stop is typed - endif
- echo "Last Name (XX to restart) "
- set lname lt
- if (lname "XX")then
- goto startover skip rest of
the loop - endif
- echo "fname lname's phone number (XX to
restart) " - set phnbr lt
25A C shell script to add new data entries to a
phone list file 2/3
- search for a duplicate entry
- if (egrep "lname, fname phnbr"
phonefile) then - echo "Duplicates were found in
the list " - endif
- while (1)
- echo "Cancel this entry?
(y/n) " - set answer lt
- if (answer Yy)
then - goto startover
- else break
- endif
- end
- endif
-
26A C shell script to add new data entries to a
phone list file 3/3
- insert entry
- set correctn
- while ("correct" ! Yy)
- echo "1 fname echo "2 lname
echo "3 phnbr echo "Is this correct ?
(y/n)" - set answerlt
- if ("answer" Yy) then
- set correcty
- else
- echo "Number to correct
" - set num lt
- switch
(num) -
case 1 echo "Last Name " -
set fname lt -
breaksw -
case 2 echo "Last Name " -
set lname lt -
breaksw -
case 3 echo "Phone Number " -
set phnbr lt
27A C shell script to find a pattern in a phone
list file
- !/bin/csh -f
- set phonefile phone.list
- switch (argv)
- case 0
- echo "Usage 0 pattern
- echo "pattern is what to look for in the phone
list" - breaksw
- default foreach pattern ()
- echo pattern
- grep "pattern" phonefile echo
"pattern not found in phonefile - end
- breaksw
- endsw
- echo "Done"
28A C shell script to find a multiple pattern in a
phone list file
- !/bin/csh -f
- set phonefile phone.list
- switch (argv )
- case 0 echo "Usage 0 pattern
- echo "pattern is what to look for in the phone
list" - breaksw
- default set counter0
- foreach pattern ()
- if (grep "pattern" phonefile) then
- _at_ counter
- else
- echo "pattern not found in phonefile"
- endif
- end
- breaksw
- endsw
29Example
- !/bin/csh -f
- A C shell script to take two arguments and
substitute the second argument - for the first, writing the resulting
corrections back into the phone list file - set phonefile phone.list
- switch (argv)
- case 2
- sed "s/1/2/g" phonefile gt
/tmp/chg.tmp - mv /tmp/chg.tmp phonefile
- breaksw
- default
- echo "Usage 0 old new
- echo "0 substitutes old for new in the
phonelist" - breaksw
- endsw
- echo "Done"