Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command...

15
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Transcript of Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command...

Page 1: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Writing C-shell scripts

#!/bin/csh

# Author: Ken Berman

# Date: 8-8-2002

# Purpose: display command and parameters

echo $0

echo $argv[*]

Page 2: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Commands and Expressions

• Commands: – Sequence of UNIX commands, separated by ';' or on different

lines

– Typically returns value via stdout

• Expression – Logical expression similar to C language

– Returns Boolean value (or integer)

Page 3: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Control structures for csh

if ( expression ) simple command

if ( expression ) then commandsendif

if ( expression ) then commandselse commandsendif

Page 4: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Control structures continued

switch( testcase )case pattern1:

commandsbreaksw

case pattern2:commandsbreaksw

default:commands

endsw

Page 5: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Control structures continued

while ( expression )

commands

end

foreach var ( wordlist )

commands

End

Page 6: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Variables

• String variables set name = value

• Integer variables @ name = value

Page 7: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Examples of variables

% set name = Fred% echo namename% echo $nameFred% set name #not the same as% unset name% set colors = ( red green blue)% echo $colors[1]red

Page 8: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Variables continued% echo $colors

red green blue

% echo $colors[1-2]

red green

% echo $colors[4]

Subscript out of range

Page 9: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Parameters for calling a script

Parameters to a script are positional parameters $argv[1], $argv[2],… same as $1, $2,…

$#argv number of arguments

$argv[*] word list of all arguments

$0 name of command (i.e. filename of script)

$argv[0] is not defined

Page 10: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Different ways to display all parameters

#!/bin/csh

echo $argv[*]

while ( $#argv > 0 )

echo $argv[1]

shift

end

Page 11: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Display parameters in reverse order

set i = $#argv

while ( $i )

echo $argv[$i]

@ i--

end

Page 12: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Logical expressions

if ($#argv == 0) echo "No arguments given"

Logical expressions can be formed with

== equal != not equal

=~ string match !~ string nonmatch

&& and

|| or

! not

Expressions have to evaluate to an integer or simple string

Page 13: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Hints for hw3

• Create a shell vector variable containing usernames from first column of who (can use awk to do this).

• For each username use grep –c to count number of occurrences of username and apply sed to delete those usernames that occur less then n times.

• Use if-else control structure to check whether there are two arguments and whether flag –t has been used

• Use awk to output results in a table

Page 14: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Logical expressions involving files

-d filenamefile is a directory-e " file exists-f " file is an ordinary file-o " user owns file-r " user has read access-w " user has write access-x " user has execute access-z " file is 0 bytes long

Example: if (-e $file && -f $file ) then …

Page 15: Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: 8-8-2002 # Purpose: display command and parameters echo $0 echo $argv[*]

Finds a given command on the search path. The pathname found or a failure message is displayed. Simulates the command "which"

#!/bin/csh

set cmd = $1foreach dir($path) if (-e $dir/$cmd) then echo FOUND: $dir/$cmd exit(0) endifendecho $cmd NOT on $path