1 / 25

Lecture 4

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

deion
Télécharger la présentation

Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 C Shell Scripts(Chapter 10)

  2. 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)

  3. 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

  4. Shell Script Example file ./hello.sh: #!/usr/bin/csh -f echo Hello World % chmod +x ./hello.sh % ./hello.sh

  5. 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!

  6. Operators • Arithmetic operators • Assignment Operators ( =, +=, -=…) • Comparison Operators (==, !=, <=…) • Bitwise and logic operators (!, &&, ||), (>>, <<) • Pattern matching (=~, !~)

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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?

  12. Script: arg_v2.csh #!/usr/bin/csh –f set n = 1 #positional index while ( $#argv > 0) echo “Arg $n is $1” shift @ n++ end

  13. Example 2 • Changing the access rights of the files in a directory recursively • It is simple but is very useful

  14. Simple Solution #!/bin/csh -f foreach file (`ls`) if ( -d $file ) then chmod –R 750 $file else chmod 750 $file endif end

  15. 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

  16. 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

  17. 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!!

  18. 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

  19. 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

  20. #!/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

  21. 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

  22. #!/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

  23. 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

  24. 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

  25. Reading Assignment • Reading Chapter 10

More Related