PAGEFLOP
FORWARD
P35
Shell Programming|Command Grouping|4-9.1|9,54
Several commands may be grouped on the same
line by separating the commands with a semicolons.
For example:
ls; date; tbl *.doc | nroff | lpr
Using a semicolon allows you to separate
commands without typing them on subsequent lines.
P36
Shell Programming|Command Grouping|4-9.2|12,54
A user may start a subshell, i.e., another level
of command processing, by typing the command sh or csh,
or by grouping commands with ( and ).
( cd x_dir; rm junk; cc game.c; a.out )
The Subshell in the above example is alive until the
last command in the parentheses is finished.
NOTE: You must precede a <CR> with a backslash (\)
to continue a long shell command onto a second line.
P37
Shell Programming|The Programming Environment|4-10.1|14,54
Setting up one's programming environment is a
process whereby one initializes one or more variables
and makes them known to other subshells.
Setting up the environment is most often done in
your login command file:
.profile (under the Bourne Shell)
.login (under the C-Shell)
Variables may be also be assigned values on
a command line.
P38
Shell Programming|The Programming Environment|4-10.2|15,54
The process of making variables available to
other shells is a two-step process under the Bourne
Shell. It involves:
1. assigning a value to a variable and
2. using the Bourne Shell command export with
the variable as the argument. For example:
TERM=wy50; export TERM
The above process is accomplished in a single
step under the C Shell, by using the setenv command:
setenv TERM wy50
P39
Shell Programming|The Programming Environment|4-10.3|6,54
You can see the value of all Shell variables
(i.e., your current programming environment) by using:
set (under sh) or
printenv (under csh).
P40
Shell Programming|The Programming Environment|4-10.4|12,54
After you have logged on to a UNIX system, the
first program that the login process invokes is your
shell program, usually sh or csh. This program initially
executes the commands in the system-wide login command file.
The system-wide login command file for sh is
/etc/profile; for csh it is /etc/cshrc.
These files are accessible to the system administrator
and should contain parameters with system-wide effects.
P41
Shell Programming|The Programming Environment|4-10.5|5,54
Following the execution of the commands in
/etc/profile or in /etc/cshrc, the Shell goes into
your home directory, and executes your private login
command file (.profile under sh or .login under csh),
thereby setting up your personal environment.
P42
Shell Programming|The Programming Environment|4-10.6|14,53
The contents of a typical .profile file may be
as shown below:
% cat .profile
HOME=/usr/home/deutsch
SHELL=/bin/csh
MAIL=$HOME/mailfile
PATH=:/usr/nelson/bin:/usr/ucb:/bin:/usr/bin
TERM=vt100
USER=deutsch
export HOME PATH MAIL TERM
date
echo "NUMBER OF USERS `who | wc -l`"
P43
Shell Programming| More Shell Scripts|4-11.1|10,60
TASK: Assume a file called address contains your
company address and you want to print 1000 shipping labels.
We'll show you an example of a Bourne Shell script and
a C Shell script to accomplish the task. Both examples
will use the "while" loop, with a FALSE condition being
reached when the "while" statement evaluates to zero.
Note the use of the command expr which is used to
evaluate arithmetic expressions.
P44
Shell Programming| More Shell Scripts|4-11.2|16,40
-------- example of sh script --------
count=1000
while test $count -ne 0
do
cat address
count=`expr $count - 1`
done
---------------------------------------
-------- example of csh script --------
set count=1000
while ($count)
cat address
set count=`expr $count - 1`
end
----------------------------------------
P45
Shell Programming| More Shell Scripts|4-11.3|11,54
TASK: Write a "cleanup" script under the Bourne Shell
that will look recursively through all of a user's
directories, starting at the current one, and search
for executable and object files. Ask whether the user
wants to delete the file. Have a positive response
remove the file.
NOTE: the line if test "$i" = ".o -o -x $i tests
whether the file $i is either an object or an executable
file. if test "$response" = y tests whether the value
of the string "response" equals "y".
P46
Shell Programming| More Shell Scripts|4-11.4|15,54
Bourne Shell cleanup script
-----------------------------------------------
for i in *
do
if test -d $i
then (cd $i; $HOME/cleanup;)
else if test "$i" = *.o -o -x $i
then echo "you want to delete file $i"
read response
if test "$response" = y
then rm $i
fi
fi
fi
done