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

zena
Download Presentation

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. % tcsh my_script.sh • Specify which shell to use within the script • First line of script is as #!/usr/bin/tcsh • #!/usr/bin/tcsh –f to not read in .cshrc • Make the script executable using %chmod +x script_file • Run directly from the command line

  4. Shell Script Example file ./hello.sh: #!/usr/bin/tcsh -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 (==, !=, <=…) • Logic operators (!, &&, ||) • Pattern matching (=~, !~)

  7. File test operators (operator + filename) • -d file: the file is a dir? • -e file: the file exists? • -o file: the user owns the file • -r/w/x file: the user has read/write/execute permission • ! + any above: reverse the meaning • ! –d the file not a dir?

  8. Control Statements • if… then…[else…]endif if (condition) then statements else statements endif • While…end Syntax: while (condition) statements end • foreach...end foreachvar (list) statements end

  9. Here Document • How to provide input to interactive programs in script? %gnuplot >plot x*x >exit %gnuplot <<EOF plot x*x exit EOF Not necessary EOF, It can be any label you like

  10. Two Types of Parameter Passing • Positional Parameters • $0 – the currently executing script • $n – the nth parameter • $# -- the number of parameters • $* -- all arguments • Argument Array • $argv[n] – the nth parameter (n > 0) • $#argv – the size of argv • $argv[*] – all arguments

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

  12. Script: arg_v1.csh #!/usr/bin/tcsh –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

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

  14. Example 2 • Changing the access rights of the files in a directory recursively

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

  16. A Solution Using Arguments #!/bin/tcsh -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

  17. A Solution Using while loop #!/bin/tcsh -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

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

  19. Script: fileinfo.csh #!/usr/bin/tcsh -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

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

  21. Script : average.csh #!/usr/bin/tcsh -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

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

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

  24. Reading Assignment • Reading Chapter 10

  25. Announce lab2

More Related