Shell Scripting II – Pipes and
Flow Control (IF STATEMENT)
Pipes:
A pipe is a basic form of interprocess
communication. For our purposes, it’s easier to say (for now) that a
pipe, which is denoted by the “|” symbol, takes the output of one
command as the input to another command. It’s easiest to understand
by seeing some examples. You can then think how these examples can be
incorporated into a shell script.
#echo “some
event occurred” | mail -s “subject here” username@mlp.com
The output of echo “some event occurred”,
becomes the input to the mail command.
#cat
/etc/passwd | grep tmurphy
In this case, the output of the cat
command has become the input to the grep command
#cat /etc/passwd
| grep tmurphy |awk ‘{FS=”:”}{print $5}’
This takes the output of grep, pipes it to
tmurphy, which then pipes it again to awk.
HOMEWORK:
Read: http://www.westwind.com/reference/os-x/commandline/pipes.html
FLOW CONTROL:
UNIX uses flow control to help decision
making in scripts. UNIX comes with a few different types that we will
discuss. If, if/else, while, until, select, case and for.
In UNIX, the syntax for flow control
statements are crucial.
THE IF STATEMENT:
Lets start with the if statement. It’s
probably the most common of the flow statements.
The Syntax is as follows:
if [ condition ]
then
statements
fi
the syntax must be as
follows:
if followed by a space
followed by a open bracket, followed by a space followed by a condition
followed by another space followed by a close bracket
Conditions: can be:
Testing strings:
= (equal),
!= (not equal),
<,
>,
Testing numbers:
-eq (equal),
-ne (not equal),
-lt (less than),
-gt (greater than).
We can also use attribute operators:
-a file file
exists
-d file file is a
directory
-f file file is a
regular file (i.e., not a directory or other special type of file)
-r file You have read
permission on file
-s file file exists and
is not empty
-w file You have write
permission on file
-x file You have
execute permission on file , or directory search permission if it is a
directory
-O file You own file
-G file Your group ID
is the same as that of file
file1 -nt file2 file1 is newer than
file2 [8]
file1 -ot file2 file1 is older than
file2
-z variable is empty
lets put some of this
into practice:
ex1
------------------------------------------------------
#!/bin/sh
FILE=”/etc/passwd”
if [ -f $FILE ]
then
echo “$FILE exists”
exit 0
else
echo “$FILE does NOT exist”
exit 1
fi
------------------------------------------------------
ex2 ($UID is a built in
variable that shows what your uid is)
------------------------------------------------------
#!/bin/sh
if [ $UID –ne 0 ]
then
echo “you
must be root to run this program”
exit 1
fi
exit 0
------------------------------------------------------
ex3
------------------------------------------------------
#!/bin/sh
USR=”tmurphy”
FOO=`ypcat passwd | grep
$USR | awk ‘{FS=“:”}{print $5}’`
if [ -z $FOO ]
then
echo “was
not able to obtain GEKOS for $USR”
exit 1
fi
echo “user is $FOO”
exit 0
------------------------------------------------------
To add some complexity to this, we
introduce two more operators:
“||” = or
“&&” = and
if [ -f /etc/passwd && -x /bin/ls
]
then
echo “condition
met”
fi
this says, “IF /etc/passwd is a
regular file AND /bin/ls is executable”, then the condition is true.
HOMEWORK: read http://www.dreamsyssoft.com/sp_ifelse.jsp
1- write
a shell script that tests for /sbin/ifconfig to be executable. If it is,
send an email
2- write
a shell script to see how many unique users are logged in and log the time/date
and number of users to a logfile (of your choosing)
3- write
a shell script that checks the load average (use the middle number)
[root@samojo biff]# uptime
16:48:20 up 48
days, 22:44, 28 users, load average: 0.00, 0.24, 0.41
.24 in this case
If it exceeds 1, send the output of ps –aux to yourself in an email
4- test
if a server is up (use the ping command). Send one packet to see if its
alive. If it isn’t log the event and send a notification message