CSCI 330 T Unix S: HE Ystem
CSCI 330 T Unix S: HE Ystem
3
EXAMPLE SCRIPT OUTPUT
% chmod u+x hello
% ./hello
Hello ege!
5
INPUT TO A C SHELL SCRIPT
Reading/prompting for user input
Providing input as command line arguments
6
READING USER INPUT WITH $<
Use a special C shell variable:
$<
7
EXAMPLE: ACCEPTING USER INPUT
#! /bin/csh
echo "What is your name?"
8
EXAMPLE: ACCEPTING USER INPUT
% chmod u+x greetings
% ./greetings
User entered
9
COMMAND LINE ARGUMENTS
Use arguments to modify script behavior
10
COMMAND LINE ARGUMENTS
Meaning
$0 name of the script
11
EXAMPLE: COMMAND LINE ARGUMENTS
#! /bin/csh
# Usage: greetings name1 name2
12
EXAMPLE: COMMAND LINE ARGUMENTS
$0 => greetings
$1 => Mark
13
DECISION LOGIC
if Statement: simplest forms
if ( expression ) then
command(s)
endif
14
DECISION LOGIC
if-then-else Statement
15
DECISION LOGIC
if-then-else Statement
16
BASIC OPERATORS IN EXPRESSIONS
Meaning
() grouping
|| Logical or
&& Logical and
17
EXPRESSION EXAMPLES
if ( $1 == next ) echo $2
18
EXAMPLE: COMMAND LINE ARGUMENTS
#! /bin/csh
if ( $#argv == 0 ) then
19
EXAMPLE: READING FILE CONTENTS
#! /bin/csh
# Usage: lookup nameOrNumber
condition-based iterations
while
23
FIXED NUMBER ITERATIONS
Syntax:
Examples:
repeat 5 ls
repeat 2 echo go home
24
THE FOREACH STATEMENT
foreach name ( wordlist )
commands
wordlist is:
list of words, or
multi-valued variable
each time through,
foreach assigns the next item in wordlist to the
variable $name
25
EXAMPLE: FOREACH STATEMENT
foreach word ( one two three )
echo $word
or
28
EXAMPLE: WHILE
#! /bin/csh
@ var = 5
29
EXAMPLE: WHILE
#! /bin/csh
echo -n "Enter directory to list: "
30
LOOP CONTROL
break
ends loop, i.e. breaks out of current loop
31
LOOP CONTROL EXAMPLE
#! /bin/csh
while (1)
32
LOOP CONTROL EXAMPLE
#! /bin/csh
while ( 1 )
33
THE SWITCH STATEMENT
Use when a variable can take different values
Use switch statement to process different cases
34
THE SWITCH STATEMENT
switch ( string ) C shell compares string
case pattern1: to each pattern until it
35
THE SWITCH STATEMENT
switch (string)
case pattern1:
40
USERUTIL SHELL SCRIPT 1 OF 2
#! /bin/csh
# Usage: userutil
41
USERUTIL SHELL SCRIPT 2 OF 2
set answer = $<
switch ($answer)
case "1":
Trapping Signals
Functions ?
calling other scripts
exec, source, eval
43
QUOTING
mechanism for marking a section of a command
for special processing:
44
DOUBLE QUOTES
prevents breakup of string into words
turn off the special meaning of most wildcard
Examples:
echo "* isn't a wildcard inside quotes"
echo "my path is $PATH"
45
SINGLE QUOTES
wildcards, variables and command substitutions
are all treated as ordinary text
Examples:
echo '*'
echo '$cwd'
echo '`echo hello`'
echo 'hi there !'
46
BACKSLASH
backslash character \
treats following character literally
47
THE HERE COMMAND
Command Syntax Meaning
command << keyword Read lines from input until keyword is
Example:
ispell -l << DONE
I was running along quite nicely
when I was acosted by the mail man
whio insisted that my name is Raimund
but I did not believe him
DONE 48
DEBUGGING SCRIPTS
% csh n scriptname
parse commands but do not execute them
% csh x scriptname
Displays each line of the script after variable
substitutions and before execution
49
can also be added to shebang line !
TRAPPING SIGNALS
any Unix process can be interrupted by a signal
common signal:
50
TRAPPING SIGNAL
Syntax:
onintr label
onintr
ignore interrupt signal
onintr
restore previous interrupt signal behavior
51
ONINTR EXAMPLE
#! /bin/csh
onintr label
label:
echo signal received
52
DIVIDE AND CONQUER
how to modularize a shell script
call Unix commands and utilities
in place
53
CALLING OTHER SCRIPTS
as subshell, via:
csh scriptname
54
EXAMPLE: OUTER
#! /bin/csh
csh inner
#! /bin/csh
56
SOURCE OTHER SCRIPT: NO SUBSHELL
#! /bin/csh
source inner
exec ./inner
Example:
set x = 23
set y = x
eval echo \$$y
59
EXAMPLE: THE EVAL COMMAND
#!/bin/csh
set A = 1
60