Download presentation
Presentation is loading. Please wait.
Published byNathan Charles Modified over 9 years ago
1
1 Shell Scripting (C shell) SungHo Maeung (sxm3011@cs.rit.edu) 10/27/2000 Tutorial section Computer Science Lab
2
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
3
Computer Science Lab3 C Shell #! /bin/csh set name = hello -> echo $name $array = ( element1 elemsnt2 ) $array[1], $array[2]
4
Computer Science Lab4 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
5
Computer Science Lab5 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 @
6
Computer Science Lab6 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 )
7
Computer Science Lab7 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
8
Computer Science Lab8 Cshell Conditional constructs ==, !=,, >=, <=, ! =~, !~ –String matches ex) $ans =~ [Yy]* ||, && –Logical operator
9
Computer Science Lab9 Cshell if examples If( expression ) then If( expression ) then Command Commandelse endif Ex) if ( $#argv != 1) then echo “$0 requires an argument” exit 1 endif
10
Computer Science Lab10 Cshell if expression 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 = 3 If( ( “ $x ” && “ $y ” ) || ! “ $z ” ) then echo TRUE If( $#argv == 0 ) exit 1
11
Computer Science Lab11 Cshell if command If { (command) } then command endif If { ( who | grep $1 >& /dev/null ) } then echo $1 is logged on endif startover: set grade = $ 100 ) then echo “Illegal grade” goto startover endif
12
Computer Science Lab12 Cshell if file testing 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
13
Computer Science Lab13 Cshell Switch switch () case : breaksw default: breaksw endsw set color = $< switch ( “ $color ” ) case bl*: echo I feel $color breaksw default: echo $color not one of them breaksw endsw
14
Computer Science Lab14 Cshell for foreach variable (wordlist) commands end foreach variable (wordlist) commands end –foreach person ( bob sam sue fred) mail $person < letter end –foreach person (`cat maillist`) mail $person <<EOF Hi $person EOF end
15
Computer Science Lab15 Cshell while while end set num = 0 while ($num < 10 ) echo $num @ num++ end repeate repeate takes two argument( number, command) –Ex) %repeate 3 echo hello hello hello hello
16
Computer Science Lab16 Cshell While shift 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
17
Computer Science Lab17 Cshell While break && continue command set answer = $< while( “ $answer ” !~ [Mm]* ) echo “ Wrong\! Try again. ” set answer = $< if( “ $answer ” =~ [Mm]*) break; end
18
Computer Science Lab18 Programming with C shell mailingList –data Hello XXX, – friends ( emails )
19
Computer Science Lab19 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.