Computer Science Lab 1
2 SShheellll SSccrriippttiinngg ((CC sshheellll)) by
3 CCoonntteennttss C shell – First Script – Getting User Input – Command Line Arguments – Conditionals and File Testing – Switch & Loops Programming with C Shell – mailingList Summary & Resources
4 CC SShheellll #! /bin/csh  set name = hello -> echo $name $array = ( element1 elemsnt2 ) $array[1] , $array[2]
5 CC SShheellll –– FFiirrsstt SSccrriipptt  ${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
6 CC sshheellll GGeettttiinngg UUsseerr IInnppuutt $< – user input (std) without newline $array = ($name) – An array is created Arithmetic ( operators ): @ integer + , - , / , * , % , << , >> += , -=, *=, /=, ++, -- • Ex) @ sum = 4+6 – Space is required after @
7 CCsshheellll DDeebbuuggggiinngg ssccrriippttss Echo (-x) and Verbose (-v) – csh –x scriptname (command ) • Display each line after variable substitution and before execution – set echo , unset ( in the file )
8 c Cshell coommmmaanndd lliinnee aarrgguummeenntt $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
9 Cshell Conditional ccoonnssttrruuccttss  == , != , <, > , >= , <= , !  =~ , !~ – String matches ex) $ans =~ [Yy]* || , && – Logical operator
10 CCsshheellll iiff eexxaammpplleess IIff(( eexxpprreessssiioonn )) tthheenn CCoommmmaanndd eellssee CCoommmmaanndd eennddiiff Ex) if ( $#argv != 1) then echo “$0 requires an argument” exit 1 endif
11 CCsshheellll iiff eexxpprreessssiioonn  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
12 CCsshheellll iiff ccoommmmaanndd  If { (command) } then command endif  If { ( who | grep $1 >& /dev/null ) } then echo $1 is logged on endif  startover: set grade = $< if( “$grade” < 0 || “$grade” > 100 ) then echo “Illegal grade” goto startover endif
13 CCsshheellll iiff ffiillee tteessttiinngg  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
14 CCsshheellll SSwwiittcchh set color = $< sswwiittcchh ((“$color”)) ccaassee bl*:: echo I feel $color bbrreeaakkssww ddeeffaauulltt:: echo $color not one of them bbrreeaakkssww eennddssww
15 CCsshheellll ffoorr ffoorreeaacchh vvaarriiaabbllee ((wwoorrddlliisstt)) ccoommmmaannddss eenndd – foreach person ( bob sam sue fred) mail $person < letter end – foreach person (`cat maillist`) mail $person <<EOF Hi $person EOF end
16 CCsshheellll wwhhiillee  set num = 0 wwhhiillee ($num < 10 ) echo $num @ num++ eenndd  rreeppeeaattee takes two argument( number, command) – Ex) %repeate 3 echo hello hello hello hello
17 CCsshheellll WWhhiillee sshhiifftt 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
18 CCsshheellll WWhhiillee break && continue command set answer = $< while( “$answer” !~ [Mm]* ) echo “Wrong! Try again.” set answer = $< if( “$answer” =~ [Mm]*) break; end
19 PPrrooggrraammmmiinngg wwiitthh CC sshheellll mailingList – data • Hello XXX, – friends ( emails )
20 RReessoouurrcceess Our sites  http://vibranttechnologies.co.in/  http://vibrantgroupmumbai.blogspot.in/
Computer Science Lab 21

Shell and perl scripting classes in mumbai

  • 1.
  • 2.
    2 SShheellll SSccrriippttiinngg((CC sshheellll)) by
  • 3.
    3 CCoonntteennttss Cshell – First Script – Getting User Input – Command Line Arguments – Conditionals and File Testing – Switch & Loops Programming with C Shell – mailingList Summary & Resources
  • 4.
    4 CC SShheellll #! /bin/csh  set name = hello -> echo $name $array = ( element1 elemsnt2 ) $array[1] , $array[2]
  • 5.
    5 CC SShheellll–– FFiirrsstt SSccrriipptt  ${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
  • 6.
    6 CC sshheellllGGeettttiinngg UUsseerr IInnppuutt $< – user input (std) without newline $array = ($name) – An array is created Arithmetic ( operators ): @ integer + , - , / , * , % , << , >> += , -=, *=, /=, ++, -- • Ex) @ sum = 4+6 – Space is required after @
  • 7.
    7 CCsshheellll DDeebbuuggggiinnggssccrriippttss Echo (-x) and Verbose (-v) – csh –x scriptname (command ) • Display each line after variable substitution and before execution – set echo , unset ( in the file )
  • 8.
    8 c Cshellcoommmmaanndd lliinnee aarrgguummeenntt $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
  • 9.
    9 Cshell Conditionalccoonnssttrruuccttss  == , != , <, > , >= , <= , !  =~ , !~ – String matches ex) $ans =~ [Yy]* || , && – Logical operator
  • 10.
    10 CCsshheellll iiffeexxaammpplleess IIff(( eexxpprreessssiioonn )) tthheenn CCoommmmaanndd eellssee CCoommmmaanndd eennddiiff Ex) if ( $#argv != 1) then echo “$0 requires an argument” exit 1 endif
  • 11.
    11 CCsshheellll iiffeexxpprreessssiioonn  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
  • 12.
    12 CCsshheellll iiffccoommmmaanndd  If { (command) } then command endif  If { ( who | grep $1 >& /dev/null ) } then echo $1 is logged on endif  startover: set grade = $< if( “$grade” < 0 || “$grade” > 100 ) then echo “Illegal grade” goto startover endif
  • 13.
    13 CCsshheellll iiffffiillee tteessttiinngg  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
  • 14.
    14 CCsshheellll SSwwiittcchh set color = $< sswwiittcchh ((“$color”)) ccaassee bl*:: echo I feel $color bbrreeaakkssww ddeeffaauulltt:: echo $color not one of them bbrreeaakkssww eennddssww
  • 15.
    15 CCsshheellll ffoorr ffoorreeaacchh vvaarriiaabbllee ((wwoorrddlliisstt)) ccoommmmaannddss eenndd – foreach person ( bob sam sue fred) mail $person < letter end – foreach person (`cat maillist`) mail $person <<EOF Hi $person EOF end
  • 16.
    16 CCsshheellll wwhhiillee  set num = 0 wwhhiillee ($num < 10 ) echo $num @ num++ eenndd  rreeppeeaattee takes two argument( number, command) – Ex) %repeate 3 echo hello hello hello hello
  • 17.
    17 CCsshheellll WWhhiillee sshhiifftt 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
  • 18.
    18 CCsshheellll WWhhiillee break && continue command set answer = $< while( “$answer” !~ [Mm]* ) echo “Wrong! Try again.” set answer = $< if( “$answer” =~ [Mm]*) break; end
  • 19.
    19 PPrrooggrraammmmiinngg wwiitthhCC sshheellll mailingList – data • Hello XXX, – friends ( emails )
  • 20.
    20 RReessoouurrcceess Oursites  http://vibranttechnologies.co.in/  http://vibrantgroupmumbai.blogspot.in/
  • 21.

Editor's Notes

  • #5 After the csh program starts, it is programmed to execute two files in the user’s directory: These files allow users to initialize their own environments.
  • #6 When you use environment variable, you must have variable name with $ and brace
  • #7 The string is broken up into a list of words and assigned to the variable , name. C shell supports integer arithmetic only.
  • #11 The C shell evaluates the expression, resulting in either a zero or nonzero numeric value.