lecture 4 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 4 PowerPoint Presentation
Download Presentation
Lecture 4

Loading in 2 Seconds...

play fullscreen
1 / 25

Lecture 4 - PowerPoint PPT Presentation


  • 135 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 4' - zena


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture 4

Lecture 4

C Shell Scripts(Chapter 10)

shell script program
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
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
shell script example
Shell Script Example

file ./hello.sh:

#!/usr/bin/tcsh -f

echo Hello World

% chmod +x ./hello.sh

% ./hello.sh

expressions
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
Operators
  • Arithmetic operators
  • Assignment Operators ( =, +=, -=…)
  • Comparison Operators (==, !=, <=…)
  • Logic operators (!, &&, ||)
  • Pattern matching (=~, !~)
file test operators operator filename
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?
control statements
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

here document
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

two types of parameter passing
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
example 1
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
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
script arg v2 csh
Script: arg_v2.csh

#!/usr/bin/tcsh –f

set n = 1 #positional index

while ( $#argv > 0)

echo “Arg $n is $1”

shift

@ n++

end

example 2
Example 2
  • Changing the access rights of the files in a directory recursively
simple solution
Simple Solution

#!/bin/tcsh -f

foreach file (`ls`)

if ( -d $file ) then

chmod -R 750 $file

else

chmod 750 $file

endif

end

a solution using arguments
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

a solution using while loop
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

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

script fileinfo csh
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

example 4
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

script average csh
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

debugging c shell script
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
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 Assignment
  • Reading Chapter 10