250 likes | 425 Vues
Lecture 4. C Shell Scripts(Chapter 10). Shell script/program. Shell script: a series of shell commands placed in an ASCII text file Commands include Anything you can type on the command line Variables (including shell vars, env vars, command args…) and Expressions
E N D
Lecture 4 C Shell Scripts(Chapter 10)
Shell script/program • Shell script: a series of shell commands placed in an ASCII text file • Commands include • Anything you can type on the command line • Variables (including shell vars, env vars, command args…) and Expressions • Control statements (if, while, for)
Script execution • Two ways to run a shell script: • Launch a subshell with the script name as the argument. e.g. % csh my_script.sh • Specify which shell to use within the script • First line of script is as #!/usr/bin/csh • #!/usr/bin/csh –f to not read in .cshrc • Make the script executable using chmod • Make sure the PATH includes the current directory • Run directly from the command line
Shell Script Example file ./hello.sh: #!/usr/bin/csh -f echo Hello World % chmod +x ./hello.sh % ./hello.sh
Expressions • C Shell expressions are used with @ or with (if/while) statements, where variable can be • Expression are formed by variables + operators • @ operator: assigns the value of arithmetic expressions to a variable • Example of @: % set n=2 % @ a=$n + 2 % @ a* = 2 Spaces must surround operators!
Operators • Arithmetic operators • Assignment Operators ( =, +=, -=…) • Comparison Operators (==, !=, <=…) • Bitwise and logic operators (!, &&, ||), (>>, <<) • Pattern matching (=~, !~)
File test operators (operator + filename) • -d file: the file is a dir? • -e file: the file exists? • -f file: the file is a plain file? • -o file: the user owns the file • -r/w/x file: the user has read/write/execute permission • -z file: the file has 0 size • ! + any above: reverse the meaning
Control Statements • if… then…[else…]endif if (condition) then statements else statements endif • While…end Syntax: while (condition) statements end • foreach...end foreach var (list) statements end
Parameter Passing Review • Positional Parameters • $0 – the currently executing script • $n – the nth parameter • $# -- the number of parameters • Argument Array • $argv[n] – the nth parameter (n > 0) • $#argv – the size of argv
Example 1 • Task: Write a script that lists all its command line arguments prepended by arguments positional index • Ex: % arg.csh a1 a2 the output after running the script is: arg1 is a1 arg2 is a2
Script: arg_v1.csh #!/usr/bin/csh –f set n = 1 #positional index while ( $n <= $#argv ) echo “Arg $n is $argv[$n]” #increment the value of n @ n++ end ================= Notes: • # for comments • After @ a space is required • Other ways to do it?
Script: arg_v2.csh #!/usr/bin/csh –f set n = 1 #positional index while ( $#argv > 0) echo “Arg $n is $1” shift @ n++ end
Example 2 • Changing the access rights of the files in a directory recursively • It is simple but is very useful
Simple Solution #!/bin/csh -f foreach file (`ls`) if ( -d $file ) then chmod –R 750 $file else chmod 750 $file endif end
A Solution Using Arguments #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif foreach file (`ls`) if ( -d $file ) then chmod -R $1 $file else chmod $1 $file endif end
A Solution Using while loop #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif set file_set = `ls` set n = 1 while($n <= $#file_set) set file = $file_set[$n] echo "The name of file is : $file ." if ( -d $file ) then chmod -R $1 $file else chmod $1 $file endif @ n ++ end
Another Solution #!/bin/csh -f if ($#argv == 0) then echo "Please give a permission" exit 1 endif set files_set = `ls` foreach file (files_set) if ( -d $file ) then chmod -R $1 $file else chmod $1 $file endif end Doesn’t Work!!
A Corrected Solution • #!/bin/csh -f • if ($#argv == 0) then • echo "Please give a permission" • exit 1 • endif • set files_set = `ls` • foreach file ($files_set[*]) • if ( -d $file ) then • chmod -R $1 $file • else • chmod $1 $file • endif • end
Example 3 • Task: Write a script that prints similar info. as ls –l, but in more user-friendly way. • Ex: % fileinfo.csh bob (can take multiple args) (Assume: ls –l bob => -rwsr-xr-x bill ….) the output after running the script is: bob is a regular file you own the file you have read permission you have write permission you have execute permission
#!/usr/bin/csh -f set n = 1 while ($n <= $#argv) if(-d $argv[$n]) then echo "$argv[$n] is a directory" endif if(-f $argv[$n]) then echo "$argv[$n] is a regular file" endif if(-o $argv[$n]) then echo "You own the file" else echo "You do not own the file" endif if(-r $argv[$n]) then echo "You have read permission" endif if(-w $argv[$n]) then echo "You have write permission" endif if(-x $argv[$n]) then echo "You have execute permission" endif @ n++ end Script: fileinfo.csh
Example 4 • Task: Write a script called average.csh that reads a list of integers on stdin and prints how many numbers were read, their sum and integer average. • Note: should handle the case there are not numbers read and not produce “division by 0” error • Ex: % average.csh << . > 10 > 20 > 30 > . 3 numbers have been read the sum is 60 Integer average was 20
#!/usr/bin/csh -f set sum = 0 set count = 0 set avg = 0 set num = $< while(($num !~ [a-z]*) && ($num != "")) @ sum += $num @ count++ set num = $< end echo "the total numbers are $count" echo "the sum is $sum" if($count >0) then @ avg += $sum @ avg /= $count echo "Integer average is $avg" else echo "Integer average is 0" endif Script : average.csh
Debugging C Shell Script • C Shell has two command line options to help to debug scripts by echoing each line of the script before actually executing it. • -v (verbose): echoes each line even before performing variable substitution • -x: echoes each line after all substitution has been performed just before executing the actual commands • How to use it: • % csh –xv script.csh • #!/bin/csh –vx • Or manually set echo points to avoid verbosity
Arguments to set • set echo • Display each lines after variable substitution and before execution • set verbose • Display each line of script before execution, just as you typed it
Reading Assignment • Reading Chapter 10