Writing c shell scripts
Download
1 / 15

Writing C-shell scripts - PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on

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

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 ' Writing C-shell scripts' - adele-harding


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
Writing c shell scripts
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 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)


Control structures for csh
Control structures for csh

if (expression)simple command

if (expression) then

commands

endif

if (expression) then

commands

else

commands

endif


Control structures continued
Control structures continued

switch(testcase)

casepattern1:

commands

breaksw

casepattern2:

commands

breaksw

default:

commands

endsw


Control structures continued1
Control structures continued

while (expression)

commands

end

foreachvar(wordlist)

commands

End


Variables
Variables

  • String variables set name = value

  • Integer variables @ name = value


Examples of variables
Examples of variables

% set name = Fred

% echo name

name

% echo $name

Fred

% set name #not the same as

% unset name

% set colors = ( red green blue)

% echo $colors[1]

red


Variables continued
Variables continued

% echo $colors

red green blue

% echo $colors[1-2]

red green

% echo $colors[4]

Subscript out of range


Parameters for calling a script
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


Different ways to display all parameters
Different ways to display all parameters

#!/bin/csh

echo $argv[*]

while ( $#argv > 0 )

echo $argv[1]

shift

end


Display parameters in reverse order
Display parameters in reverse order

set i = $#argv

while ( $i )

echo $argv[$i]

@ i--

end


Logical expressions
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


Hints for hw3
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


Logical expressions involving files
Logical expressions involving files

-d filename file 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 …


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 = $1

foreach dir($path)

if (-e $dir/$cmd) then

echo FOUND: $dir/$cmd

exit(0)

endif

end

echo $cmd NOT on $path


ad