Shell Script Writing

 

A shell script is a sequence of shell commands in a file which are executed

with one command

 

example:

% cat whoson

#!/bin/csh

date

rwho | grep jpf7

rwho | grep jpf3

% chmod u+x whoson

% whoson

...

%

 

first line #!/bin/csh tells which shell should be used if this is run

as an executable file

 

otherwise the # will indicate the rest of the line is a comment

 

different ways to call a script

 

% whoson $ whoson

the file must be executable and the first line should tell which shell is

to be used to interpret the commands

 

% source whoson $ . whoson

the script commands will be executed within the current shell

 

% csh whoson $ sh whoson

% sh whoson $ csh whoson

the script commands will be executed within a new shell of the type indicated

and then it will return back to the current shell on completion of the script

 

% exec whoson $ exec whoson

the script commands will be executed within a new shell of the same type as

the current shell, but when the shell completes, it will not come back to

the current shell

 

see script.examples/calling directory

for examples of using these to call other scripts

 

 

Arguments to a shell script

 

Arguments are automatically stored in an array named argv

 

% thisscr one two 3 4

 

inside of this script $argv = one two 3 4

$argv[*] = one two 3 4

$argv[1] = one

$argv[4] = 4

$argv[0] = thisscr // not on our system

$0 = thisscr

$2 = two

$3 = 3

 

$#argv = 4

$# = 4

 

 

Example of Arguments Passed to a shell script

 

% numfiles

will tell how many children the current directory has

% numfiles2 directoryname

will tell the number of children in the named directory

 

Conditionals

 

1) Conditional Execution

|| and && as shown in cond.execution same as

we have already addressed on the shell line

command1 && command2

2) if Conditional

can test many other things besides if a task completed successfully

formats:

if (expression) simple-command

or

if (expression) then

commands

endif

or

if (expression) then

commands1

else

commands2

endif

or

if (expression1) then

commands1

else if (expression2) then

commands2

else if (expression3) then

commands3

else

commands4

endif

 

if ($#argv == 3) echo "3 arguments"

if ($?varname) echo $varname

if ($num > 40) then

echo "too big"

@ num = ($num - 40)

echo "$num is new value"

endif

if ($num > 40) then

echo big

else

echo small

endif

if ($#argv >5) then

echo "too many arguments"

else if ($#argv < 1) then

echo "not any arguments"

else if ($#argv == 3) then

echo "just right"

else

echo "acceptable number"

endif

 

expressions

 

relations: < > <= >= == !=

testing of files:

e exists

z is size 0

d is a directory

f is a file

r user has read permission

w user has write permission

x user has execute permission

 

if ($#argv != 3) echo "wrong number"

if (-e $1) echo "$1 already exists"

if (-w this.file) then

echo " line added" >> this.file

echo "this.file modified"

else

echo "can not modify"

endif

 

test -r this.file || echo "can not read"

 

EXIT COMMAND

 

leave the script immediately without question

possibly assigning a value to the $status variable

 

exit [numeric expression]

 

 

#displays the lines that contain the string "and"

# in the file named as the argument

# Usage: andcount filename

test -r $argv[1] || exit 2

grep and $1 || exit 0

exit 1

 

in this case exit value 2 means file was unavailable or unreadable

exit value 0 means the grep didn't find anything

exit value 1 means everything was OK and lines were

displayed

 

 

GOTO Command

 

will tell the execution of a script to continue at a line with

a corresponding label

 

label is indicated by alphabetic string and a :

 

 

# goto example

echo before goto

goto this

echo after goto

echo this won't be done

exit 1

this:

echo this ends the program

 

Loops

 

while loop

while (expression)

commands

end

 

example: output:

set num = 0 0

while ($num < 3 ) 1

echo $num 2

@ num = ($num + 1)

end

 

break

when hit inside a loop, will terminate that loop

and continue the execution after the end of the loop

 

 

continue

when hit inside a loop, will terminate that iteration of the loop

and continue the execution at the top of the next iteration

 

example: output:

set num = 0 1 at top

while ($num < 10) 1 in middle

@ num = ($num + 1) 1 at bottom

echo "$num at top" 2 at top

if ($num == 3) then 2 in middle

continue 2 at bottom

else if ($num == 5) then 3 at top

break 4 at top

else 4 in middle

echo "$num in middle" 4 at bottom

endif 5 at top

echo "$num at bottom" line after

end

echo line after

foreach loop

 

foreach varname (varlist)

commands

end

 

examples: output:

foreach name (bob fred alice) bob

echo $name fred

end alice

 

set names = (bob fred alice) bob fred alice

foreach i ($names) bob fred alice

echo $names bob fred alice

end

 

foreach j ($argv) argval1

echo $j argval2

end ...

 

foreach file (*)

if (!-f $file) then

echo "not a file"

else if (!-z $file) then

echo -n $file

wc -l $file

else

break

endif

end

 

foreach argname ($1*)

echo $argname

cat $argname

end

 

onintr

transfers control to a labeled line in the case of an interrupt

set once usually at the beginning of a script and remembered in

case it is ever needed

 

example:

onintr endit

startover:

sleep 10

echo sleeping

goto startover

 

endit:

echo awake now

 

shift

moves the value of the array down one (to the left and the left

most element falls off)

 

%cat shifting

# shorter shift demo

echo "arg1 = $1 arg2 = $2"

shift

echo "arg1 = $1 arg2 = $2"

shift

echo "arg1 = $1 arg2 = $2"

shift

% shifting bob bill fred %shifting 1 2

arg1 = bob arg2 = bill arg1 = 1 arg2 = 2

arg1 = bill arg2 = fred arg1 = 2 arg2 =

arg1 = fred arg2 = arg1 = arg2 =

error : can not shift

 

% cat shift2 output:

set names = (bill bob tom) names horizontally:

echo "names horizontally:" bill bob tom

echo "$names" names vertically:

echo "names vertically:" bill

while ($#names != 0) bob

echo $names[1] tom

shift names

end

user input

 

echo 'enter a line'

set userline = $<

echo $userline

echo $userline[1]

 

echo 'enter a line'

set userline = `head -1`

echo $userline

set uline = ($userline)

echo $uline[1]

 

Another Conditional - the SWITCH

 

see switchdemo

it is like a big if - the - else if ...

 

Bourne Shell

see bscr1 which calls bscr2