1 / 29

TDC368 UNIX and Network Programming

TDC368 UNIX and Network Programming. Week 9: Introduction to UNIX Shells C-Shell Interactive Commands C Shell Programming. Camelia Zlatea, PhD Email: czlatea@cs.depaul.edu. Ellie Quingley, UNIX Shells by Example, Prentice Hall PTR, NJ, 1997 chap.9 (pp.317-414)

Télécharger la présentation

TDC368 UNIX and Network Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. References

  3. 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

  4. 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 )’

  5. Introduction to UNIX Shells • File .login contains environment variables and terminal settings; this information is inherited by shell’s 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

  6. 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

  7. 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 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-Z][A-Z][A-Z] #list all files with three uppercase character name ls *{11,22} #list all files ending in 11 or 22

  8. C-Shell Interactive Commands • Redirectation , Pipes • ( > , < , >&, >>, >>& , << HERE, |, |& • Example: Here Document cat << 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

  9. 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.

  10. 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 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 after${var} 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

  11. Arrays 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 $persons[$#persons] # display the last element echo $persons[1] #display first element of the array set $persons[1] = John #reassign first element echo $persons[3-4] # list elements 3 and 4 of the array echo $persons[10] # 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

  12. Special variables • $$ - displays the PID of current shell • $< - accepts input from the user • Backslash Character: - escape the interpretation of a single character (ex. ?, !, newline)

  13. 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

  14. 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

  15. 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

  16. 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 $dd echo Hope you have a nice day, $argv[1] \! echo Good-bye $argv[1] $argv[2] $argv[3] greetings Joe Doe

  17. 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

  18. Example #!/bin/csh -f # Scriptname: filecheck # Usage: filecheck filename set file=$1 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

  19. 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

  20. Loops • 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 condor:> csh t2 a b c d e a b c d e b c d e c d e d e e condor:>

  21. Loop • 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"

  22. 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

  23. Setting Executable Scripts • setuid scripts • chmod u+sx script_file

  24. A 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 = $< # 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

  25. 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

  26. 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 endif end # end while ... correct echo "$lname, $fname $phnbr" >> $phonefile end # first while echo "Done"

  27. 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"

  28. A 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 counter=0 foreach pattern ($*) if {(grep "$pattern" $phonefile)} then @ counter++ else echo "$pattern not found in $phonefile" endif end breaksw endsw echo "Matched $counter patterns" echo "Done"

  29. 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 file set phonefile = phone.list switch ($#argv) case 2: sed "s/$1/$2/g" $phonefile > /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"

More Related