150 likes | 319 Views
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
E N D
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)
Control structures for csh if (expression)simple command if (expression) then commands endif if (expression) then commands else commands endif
Control structures continued switch(testcase) casepattern1: commands breaksw casepattern2: commands breaksw default: commands endsw
Control structures continued while (expression) commands end foreachvar(wordlist) commands End
Variables • String variables set name = value • Integer variables @ name = value
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 % echo $colors red green blue % echo $colors[1-2] red green % echo $colors[4] Subscript out of range
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 #!/bin/csh echo $argv[*] while ( $#argv > 0 ) echo $argv[1] shift end
Display parameters in reverse order set i = $#argv while ( $i ) echo $argv[$i] @ i-- end
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 • 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 -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