Shell Scripting (C shell)
SungHo Maeung
(sxm3011@[Link])
10/27/2000
Tutorial section
Computer Science Lab 1
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 2
C Shell
#! /bin/csh
set name = hello -> echo $name
$array = ( element1 elemsnt2 )
$array[1] , $array[2]
Computer Science Lab 3
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 4
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 5
Cshell Debugging scripts
Echo (-x) and Verbose (-v)
– csh –x scriptname (command )
• Display each line after variable substituti
on and before execution
– set echo , unset ( in the file )
Computer Science Lab 6
Cshell command line argume
nt
$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 7
Cshell Conditional construct
s
== , != , <, > , >= , <= , !
=~ , !~
– String matches ex) $ans =~ [Yy]*
|| , &&
– Logical operator
Computer Science Lab 8
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 9
Cshell if expression
set name = $< set x = 1 , y = 2, z = 3
If( “$name” != “” ) then If( (“$x” && “$y”) || ! “$z”)
grep “$name” data file
then
endif
echo TRUE
If( $answer =~ [Yy]* ) th If( $#argv == 0 ) exit 1
en
mail bob < message
else
mail jon < datafile
endif
Computer Science Lab 10
Cshell if command
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
Computer Science Lab 11
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
Computer Science Lab 12
Cshell Switch
set
color = $<
switch (“$color”
( )
case bl*:
echo I feel $color
breaksw
default:
echo $color not one of them
breaksw
endsw
Computer Science Lab 13
Cshell for
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
Computer Science Lab 14
Cshell while
set num = 0
while ($num < 10 )
echo $num
@ num++
end
repeate takes two argument( number,
command)
– Ex) %repeate 3 echo hello
hello
hello
hello
Computer Science Lab 15
Cshell While
shift command
– Shifts the argv array by one word
• Ex) While( $#argv)
echo $argv
shift
end
%loop a b c
abc
bc
c
Computer Science Lab 16
Cshell While
break && continue command
set answer = $<
while( “$answer” !~ [Mm]* )
echo “Wrong\! Try again.”
set answer = $<
if( “$answer” =~ [Mm]*) break;
end
Computer Science Lab 17
Programming with C shell
mailingList
– data
• Hello XXX,
– friends ( emails )
Computer Science Lab 18
Resources
Tutorial sites
[Link]
[Link]
[Link]
[Link]
[Link]
Computer Science Lab 19