340 likes | 812 Views
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
E N D
Shell Scripting (C shell) SungHo Maeung (sxm3011@cs.rit.edu) 10/27/2000Tutorial 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 • Summary & Resources Computer Science Lab
C Shell • #! /bin/csh • set name = hello -> echo $name • $array = ( element1 elemsnt2 ) $array[1] , $array[2] Computer Science Lab
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
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
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
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
Cshell Conditional constructs • == , != , <, > , >= , <= , ! • =~ , !~ • String matches ex) $ans =~ [Yy]* • || , && • Logical operator Computer Science Lab
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
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
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
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
Cshell Switch • set color = $<switch (“$color”)casebl*: echo I feel $colorbreakswdefault: echo $color not one of thembreakswendsw Computer Science Lab
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
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
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
Cshell While • break && continue commandset answer = $< while( “$answer” !~ [Mm]* ) echo “Wrong\! Try again.” set answer = $< if( “$answer” =~ [Mm]*) break; end Computer Science Lab
Programming with C shell • mailingList • data • Hello XXX, • friends ( emails ) Computer Science Lab
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