1 / 19

Shell Scripting (C shell)

Shell Scripting (C shell). SungHo Maeung (sxm3011@cs.rit.edu) 10/27/2000 Tutorial section Computer Science Lab. Contents. C shell First Script Getting User Input Command Line Arguments Conditionals and File Testing Switch & Loops Programming with C Shell mailingList

pembroke
Télécharger la présentation

Shell Scripting (C shell)

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. Shell Scripting (C shell) SungHo Maeung (sxm3011@cs.rit.edu) 10/27/2000Tutorial section Computer Science Lab

  2. Contents • C shell • First Script • Getting User Input • Command Line Arguments • Conditionals and File Testing • Switch & Loops • Programming with C Shell • mailingList • Summary & Resources Computer Science Lab

  3. C Shell • #! /bin/csh • set name = hello -> echo $name • $array = ( element1 elemsnt2 ) $array[1] , $array[2] Computer Science Lab

  4. C Shell – First Script • ${LOGNAME} • Print user’s name • `unix command` • Enclosed in back quotes • Performed command substitution • Ex) `date` • uname –n • Display the machine name • ps –ef | grep “^ *$LOGNAME” • User’s processes are printed • chmond –u+x greetme Computer Science Lab

  5. C shell Getting User Input • $< • user input (std) without newline • $array = ($name) • An array is created • Arithmetic ( operators ): @ integer + , - , / , * , % , << , >> += , -=, *=, /=, ++, -- • Ex) @ sum = 4+6 • Space is required after @ Computer Science Lab

  6. Cshell Debugging scripts • Echo (-x) and Verbose (-v) • csh –x scriptname (command ) • Display each line after variable substitution and before execution • set echo , unset ( in the file ) Computer Science Lab

  7. Cshell command line argument • $0 - The name of the script • $1, $2, $3,..${10} • Parameters are referenced by number • $* - All positional parameters • $argv[1], $argv[2] • The first argument , second … • $argv[*] or $argv – all arguments • $#argv – The number of arguments (size) • $argv[$#argv] – The last argument Computer Science Lab

  8. Cshell Conditional constructs • == , != , <, > , >= , <= , ! • =~ , !~ • String matches ex) $ans =~ [Yy]* • || , && • Logical operator Computer Science Lab

  9. Cshell if examples • If( expression ) then Command else Command endif Ex) if ( $#argv != 1) then echo “$0 requires an argument” exit 1 endif Computer Science Lab

  10. set name = $<If( “$name” != “” ) then grep “$name” data file endif If( $answer =~ [Yy]* ) then mail bob < message else mail jon < datafile endif set x = 1 , y = 2, z = 3If( (“$x” && “$y”) || ! “$z”) then echo TRUE If( $#argv == 0 ) exit 1 Cshell if expression Computer Science Lab

  11. Cshell if command • If { (command) } then command endif • If { ( who | grep $1 >& /dev/null ) } then echo $1 is logged onendif • startover:set grade = $<if( “$grade” < 0 || “$grade” > 100 ) then echo “Illegal grade” goto startover endif Computer Science Lab

  12. if( ! –e $file ) : file exists if( -d $file ) : a direcotry file if( -f $file ) : a plain file if( ! –z $file ) : zero length if( -r $file && -w $file): readable and writable if( -x $file ): executable Cshell if file testing Computer Science Lab

  13. Cshell Switch • set color = $<switch (“$color”)casebl*: echo I feel $colorbreakswdefault: echo $color not one of thembreakswendsw Computer Science Lab

  14. Cshell for • foreach variable (wordlist) commandsend • foreach person ( bob sam sue fred) mail $person < letterend • foreach person (`cat maillist`) mail $person <<EOF Hi $person EOFend Computer Science Lab

  15. Cshell while • set num = 0while ($num < 10 ) echo $num @ num++end • repeate takes two argument( number, command) • Ex) %repeate 3 echo hellohellohellohello Computer Science Lab

  16. Cshell While • shift command • Shifts the argv array by one word • Ex) While( $#argv) echo $argv shift end %loop a b c a b c b c c Computer Science Lab

  17. Cshell While • break && continue commandset answer = $< while( “$answer” !~ [Mm]* ) echo “Wrong\! Try again.” set answer = $< if( “$answer” =~ [Mm]*) break; end Computer Science Lab

  18. Programming with C shell • mailingList • data • Hello XXX, • friends ( emails ) Computer Science Lab

  19. Resources • Tutorial sites • http://tobias.fritz.net/cs/csh-programming.html#A • http://www.msi.umn.edu/~dudley/cshell/programming.html • http://dpleated.ee.ic.ac.uk/unix/old/shell/cshell/prog/intro.html Computer Science Lab

More Related