230 likes | 243 Views
Writing Shell Scripts. Professor Ching-Chi Hsu 1998 年 4 月. Write Shell Scripts(1). Invoking Shell Scripts csh file [arg...] file [arg…] if mode if readable and executable example #this shell script consults an on-line telephone databases grep -i $1 ~/phonenumber chmod +rx tel tel smith
E N D
Writing Shell Scripts Professor Ching-Chi Hsu 1998年4月
Write Shell Scripts(1) • Invoking Shell Scripts • csh file [arg...] • file [arg…] if mode if readable and executable • example #this shell script consults an on-line telephone databases grep -i $1 ~/phonenumber chmod +rx tel tel smith grep -i smith ~/phonenumber • Executable Text File Format %csh file fork csh file Wait() Sub shell
Write Shell Scripts(2) executable text file csh Read first line standard input invoking csh # file i invoking csh file line * cmd first word input file ….. command arguments
Write Shell Scripts(3) • Example %tryme > tryme.out #! /bin/cat now is the time for all good man to come to the aid of the country bin/cat tryme >tryme.out • subshell : • 1 .cshrc • 2 variables • #! /bin/csh -f
Write Shell Scripts(4) • csh scripts • Positional Parameter $1,$2,… $argv[1],$argv[2],… $#argv • foreach command foreach var (wordlist) commandlist end Ex. # foreach x ($argv[*]) grep -i $x $home/phonenumbers end #foreach file (.* *) echo $file end
Write Shell Scripts(5) • if if(expr) simple-command if(expr) then commandlist1 [else commandlist2] endif ex. # If($#argv>2 || $#argv<1) then echo usage : $0 \[from-file\] to-file else if($#argv==2) then set from=$argv[1] set to=$argv[2] else set to=$argv[1] endif endif
Write Shell Scripts(6) • else if if(expr1) then commandlist1 else if(expr2) then commandlist2 [else commandlist3 endif • shift shift var • switch switch(str) case pattern1: commandlist1 breaksw case pattern2: commandlist2 breaksw ….
Write Shell Scripts(7) default : commandlist endsw ex. #append $1 to $2 or standard input to $1 switch($#argv) case 1 : cat >> argv[1] breaksw case 2 : cat >> $argv[2] < $argv[1] breaksw default : echo ‘usage : append (from) to’ endws
Write Shell Scripts(8) • while while(expr) commandlist end ex. # set i=$#argv while($i) echo $argv[i] @i-- end • Numerical Computation @var=expr @var[n]=expr ex. @x=$#argv/2 @x+=3 @i++
Write Shell Scripts(9) • goto word • break and continue • Expressions • logical operator • logical constant • relational operator label || or && and ! not 0 false 1 true = = != =~ string match !~ string nomatch <= >= > <
Write Shell Scripts(10) * + / - % • binary operator • bitwise logical operator ex. ($1=!-*) ($name!~*.c) ($char != \# && $char !=@) ex. @i= if($string) then & << | >> ^ ~ String with leading 0 is considered octal null string : arith=0 logical =false
Write Shell Scripts(11) • csh file guerries -r file is readable by uesr -w file is writable by user -x file is executable by user -e file exists -o file is owned by user -z file zero size -f file ordinary file -a file directory file • Input Output In: echo -n type yes or no : out : $< ex.
Write Shell Scripts(12) ex. echo -n enter command set cmd=$< eval $cmd • Using Variables • Positional parameters $1,$2,… • special variables such as $cwd and $nomatch • Environment variables,$HOME • ordinary variables
Write Shell Scripts(13) Example: ccp ##script name:ccp ##conditional copy ## ##usage: ## ccp from to[file….] ##where: ## ‘from’ is the source directory ## ‘to’ is the destination directory ## [file…] is a list of optional file names to be copied,if not ## given,all files in ‘from’ directory will be processed if($#argv<2) then echo usage : $0 from-dir to-dir ‘[file…]’;exit(1) else if !-d $1 || ! -d $2) then echo usage : $0 from-dir to-dir ‘[file…]’ exit(1) endif set dir = $cwd; chdir $2; set to= $cwd
Write Shell Scripts(14) chdir $dir; chdir $1; ##now in from-dir if($#argv == 2) then set files = * else set files = $argv[3-] endif foreach file($files) if(-d $file) continue #if file doesn’t exit then cp if(! -e $to/$file) then echo $to/$file is a new file cp $file $to continue endif # if file in $from is more recent then cp find $file -newer $to/$file -exec cp $file $to\; end
Write Shell Scripts(15) Example: total ##script name : total ## compute total disk space used in bytes for a directory hier #a recursive script set nonomatch ##empty directories will not cause problems if($#argv != 1) then echo usage : $0 directory exit(1) endif set count=0 foreach file($1/*) if(-f $file) then set x=‘/bin/ls -l $file’ else if(-d $file) then set x=‘/bin/ls -1d $file’ set y=‘$0 $file’ ## recursive call @count=$count+$y else
Write Shell Scripts(16) echo $file not included in the total >> ! /tmp/total continue endif @count=$count+$x[4] end echo $count
Write Shell Scripts(17) clean directory display the name of each file in the given directory and allows the user to decide interactively whether or not to keep or delete specific files.The clean script is programmed as follows: ## script name : clean ## helps to remove unwanted files from a directory if($#argv != 1) then echo usage: $0 directory;exit(1) else if(! -d $1) then echo $1 not a directory; exit(1) endif set dir = $1 chdir $dir set files = * #process files
Write Shell Scripts(18) foreach file ($files) if(! -f $file) continue echo ‘ ‘ ## gives a blank line echo “file=$file” ## identifies file being processed echo ‘ ‘ head $file while(1) echo -n rm $file ‘?? (y, n, !, or q)’; set c=$< switch($c) case y: if( {rm $file}) then echo ‘*****’ $file rm-ed else echo cannot rm $file endif break ## break out of while case n: echo ‘*****’ $file not rm-ed break
Write Shell Scripts(19) case q: exit(0) case \!: echo command: eval $< ## in $< the variable $file can be used endsw end ## of while end ## of foreach
Write Shell Scripts(20) Example: mapc The mapc script takes a user command entered interactively and applied it to files in a directory( or a hierachy if the flag -r is given). The mapc script is: ## script name : mapc ## asks the user to input a command that is applied to each file ## contained in the given directory( or hierarchy ) ## foe example ## mapc . ## input command : ls -l $file ## will execute the given command with the variable $file ## going through each regular file in the current directory ## the -r option is used to indicate application to ## all files in the hierarchy set nonomatch ## empty directories will not cause problems if($#argv<1 || $#argv>2) then echo usage : $0 ‘[-r]’ directory; exit(1)
Write Shell Scripts(21) else if ($#argv == 1) then set r = 0; set dirs = ($1) else if (f$1 = = f-r) then set r = 1; set dirs = ($2) else echo usage : $0 ‘[-r]’ directory; exit(1) endif if(! -d $dirs[1]) then echo $dirs[1] not a directory; exit(1) endif ## obtain in interactively entered command line echo -n input command set cmd = $< ## main loop while ($#dirs > 0) set dir = $dirs[1]; shift dirs; chdir $dirs foreach file ($cwd/*) if( -f $file ) then eval $cmd ## execute user command
Write Shell Scripts(22) else if ( $r == 1 && -d $file) then set dirs = ( $file $dirs) endif end ## of foreach end ## of while echo done