tdc368 unix and network programming

29
TDC368 UNIX and Network Programming Camelia Zlatea, PhD Email: czlatea @ cs . depaul . edu Week 9: Introduction to UNIX Shells C-Shell Interactive Commands C Shell Programming

Upload: faith-bradford

Post on 01-Jan-2016

33 views

Category:

Documents


3 download

DESCRIPTION

TDC368 UNIX and Network Programming. Week 9: Introduction to UNIX Shells C-Shell Interactive Commands C Shell Programming. Camelia Zlatea, PhD Email: [email protected]. Ellie Quingley, UNIX Shells by Example, Prentice Hall PTR, NJ, 1997 chap.9 (pp.317-414) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: TDC368 UNIX and Network Programming

TDC368UNIX and Network Programming

Camelia Zlatea, PhD

Email: [email protected]

Week 9: Introduction to UNIX Shells C-Shell Interactive Commands C Shell Programming

Page 2: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 2Spring 2003

References

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, O’Reilly & Associates, Inc. , section 5.

Page 3: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 3Spring 2003

Introduction 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 (pid=1), 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

Page 4: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 4Spring 2003

Introduction to UNIX Shells

? How to change your login shell: passwd –e (on HP) File .cshrc contains C shell variables settings Example:

set history=20 # previous commands to remember.

set savehist=20 # number to save across sessions.

set system=`hostname` # name of this system.

set prompt = "$system\:> " # command prompt.

set autologout=20

alias l ls

alias info ‘(who am i; date )’

Page 5: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 5Spring 2003

Introduction to UNIX Shells

File .login contains environment variables and terminal settings; this information is inherited by shell’s children processes.

Example:setenv TERM ansiumask 077echo Hello $LOGNAMEecho The date is 'date'echo Your home shell is $SHELLecho Good-bye $LOGNAME 

? How to re-execute a modified .chsrc or .login , in the current shell source .login or source .cshrc

Page 6: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 6Spring 2003

C-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 >csh.doc &

Command line history: history– !! # re-executing last command– !6 # the sixth command from history is executed

Page 7: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 7Spring 2003

C-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 ControljobsBgfg

suspend jobs (^Z) Metacharacters

ls a* # list all files starting wirh als [aA]* #list all files starting with a or Als *[0-9] #list all file ending with a digitls *[.][0-9] #list allfiles ending with a period followed by a digitls ?? #list all files with two-character namels [A-Z][A-Z][A-Z] #list all files with three uppercase character namels *{11,22} #list all files ending in 11 or 22

Page 8: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 8Spring 2003

C-Shell Interactive Commands

Redirectation , Pipes– ( > , < , >&, >>, >>& , << HERE, |, |&

Example: Here Documentcat << 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

Page 9: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 9Spring 2003

C-Shell Interactive Commands

Redirectation of both output and error standard files >&

– ls -l | grep May > & outfile– separate redirectation of stderr and stdin

– ( ls -l | grep May > result1 ) > & 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.

Page 10: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 10Spring 2003

Variables 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 variablesset savehist = 10 # history and savehistset 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 definedset var = noon # user variableecho after${var}set var = "Hello World"echo $varsetenv PERSON="Joe Doe" # set an environment variableecho $PERSONset # displays all local variables set for the current shell env #display all environment variables setenv

Page 11: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 11Spring 2003

Arrays and Strings

Examples:set persons = (Joe Liz Tom Dan) # an array variableecho $persons # display the variableecho $persons[*] #sameecho $#persons # display the number of elementsecho $persons[$#persons] # display the last elementecho $persons[1] #display first element of the arrayset $persons[1] = John #reassign first elementecho $persons[3-4] # list elements 3 and 4 of the arrayecho $persons[10] # out of rangeshift persons # shift one position to the left the array values

set days = "Monday Tuesday Wednesday" #a string variable, not an arrayset days = ($days) #converts the string into an arrayset var1 = `date` # the output of the command date is stored as an arrayset var2 = “`date`" # the output is stored as a single string

Page 12: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 12Spring 2003

Special variables

$$ - displays the PID of current shell $< - accepts input from the user

Backslash Character: - escape the interpretation of a single character (ex. ?, !, newline)

Page 13: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 13Spring 2003

C-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

Page 14: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 14Spring 2003

Debugging 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

Page 15: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 15Spring 2003

Variables

set varname = # assignment$< variable # reading user input

Numeric Variables - Arithmetic Expressions

Operators: + - / * <<(left shift) >> (right shift)+=, -+, *=, /= , ++, --

Examples:@ var = 5+ 3

echo $var

@ sum += 1 # same with @ sum = @ sum +1

echo $sum

Page 16: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 16Spring 2003

Command Line Arguments

$0, $1, .., $9 , ${10}, ${11}, ..$* - all arguments$argv[1], $argv[2], …. – 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 = $d[1-3]echo Welcome to this day $ddecho Hope you have a nice day, $argv[1] \!echo Good-bye $argv[1] $argv[2] $argv[3]

greetings Joe Doe

Page 17: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 17Spring 2003

Decisions

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

Page 18: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 18Spring 2003

Example

#!/bin/csh -f# Scriptname: filecheck# Usage: filecheck filenameset file=$1if ( ! -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 constructecho "You have read and execute permission on file

$file.endif

elseprint "$file is neither a plain file nor a directory. "

endifendif

Page 19: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 19Spring 2003

Example: 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

Page 20: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 20Spring 2003

Loops

foreach variable (list)commandsend

Example#!/bin/csh -fforeach file (*.o)

rm $fileend

while (condition)Commandend

Example#!/bin/csh -f# Script is called loop.argswhile ( $#argv )

echo $argvshift

end

condor:> csh t2 a b c d e

a b c d e

b c d e

c d e

d e

e

condor:>

Page 21: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 21Spring 2003

Loop

repeat n commands#!/bin/csh -fwhile ( $#argv )

echo $argvshift

end

#!/bin/csh -fwhile (1)

echo "Hello, in 1st loop"while (1)

echo "In 2nd loop"while (1)

echo "In 3rd loop"repeat 3 break

endend

endecho "Out of all loops"

Page 22: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 22Spring 2003

Signals 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

Page 23: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 23Spring 2003

Setting Executable Scripts

setuid scripts chmod u+sx script_file

Page 24: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 24Spring 2003

A C shell script to add new data entries to a phone list file 1/3

#!/bin/csh -fset phonefile = phone.list

startover:while (1) echo "First Name (stop to exit): " set fname = $< # read input variable if ($fname =~ [Ss][Tt][Oo][Pp]*) then break # exit if stop is typed endif

echo "Last Name (XX to restart) " set lname = $< if ($lname =~ "XX")then goto startover # skip rest of the loop endif

echo "$fname $lname's phone number (XX to restart): " set phnbr = $< if ($phnbr =~ "XX")then goto startover # skip rest of the loop

endif

Page 25: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 25Spring 2003

A 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 = $<

if ($answer =~ [Yy]*) then

goto startover

else break

endif

end

endif

Page 26: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 26Spring 2003

A C shell script to add new data entries to a phone list file 3/3# insert entry

set correct=n while ("$correct" !~ [Yy]*)

echo "1 $fname“ ; echo "2 $lname“ ; echo "3 $phnbr“ ; echo "Is this correct ? (y/n)" set answer=$< if ("$answer" =~ [Yy]*) then set correct=y else echo "Number to correct: " set num = $< switch ($num) case 1: echo "Last Name: " set fname = $< breaksw case 2: echo "Last Name: " set lname = $< breaksw case 3: echo "Phone Number: " set phnbr = $< breaksw endsw endifend # end while ... correctecho "$lname, $fname $phnbr" >> $phonefileend # first whileecho "Done"

Page 27: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 27Spring 2003

A 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"

Page 28: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 28Spring 2003

A C shell script to find a multiple pattern in a phone list file

#!/bin/csh -fset 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 counter=0foreach pattern ($*)if {(grep "$pattern" $phonefile)} then

@ counter++else

echo "$pattern not found in $phonefile"endif

endbreaksw

endsw

echo "Matched $counter patterns"echo "Done"

Page 29: TDC368 UNIX and Network Programming

UNIX Network Programming – TDC368-901 Page 29Spring 2003

Example

#!/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 fileset phonefile = phone.list

switch ($#argv)case 2:

sed "s/$1/$2/g" $phonefile > /tmp/chg.tmpmv /tmp/chg.tmp $phonefilebreaksw

default:echo "Usage: $0 old newecho "$0 substitutes old for new in the $phonelist"breaksw

endsw

echo "Done"