Disk Alert Script

I ve written this script, upon execution I get error as :
[health@autofbk ~]$ ./spacealert
./spacealert: line 3: unexpected EOF while looking for matching ``'
./spacealert: line 22: syntax error: unexpected end of file

Now I have modified the script but still having some issues, the output of usage1.txt is 37 55 5 0 as per the following script alerttext should display all lines having nos greter than 2o but its not ...

I see a lot of activity on this one, so I thought I would chime in. My
script is for an AIX 6.x system, so may need a bit of tweaking for other
OSs.
#! /bin/sh
#
############################################## ##################################
# This scripts monitors the disk space utilization and sends an email to
the Admins if
# the amount of disk space for any file system reaches 95% or more.
This script will
# run as an hourly cron job.
#
#
#
# Written by Mike Van Hoff
#
############################################## ##################################
#
df -m |grep -iEv "filesystem|proc" |awk '{print $7 " " $4}' |while read
LINE; do
PERC=`echo $LINE |cut -d"%" -f1 |awk '{print $2}'`
if [ $PERC -gt 94 ]; then
echo "The Filesystem ${LINE} full on `hostname`" |mail
-s "Disk Space Alert " email@removed
fi
done

(ignore the . at the begining of the indented lines of code examplesthey are there to
just preserve whitespace.)
.
You don't need to cat a file to awk it can read a file.
.
echo " Server IP = `awk '/'$HOSTNAME'/{print $1}' pqr `" > /home/health/alerttext;
echo " HOSTNAME = `awk '/'$HOSTNAME'/{print $2}' pqr`" >> /home/health/alerttext;
.
Also I think you could change these 2 lines to be a single awk call like this.
.
awk '/'$HOSTNAME'/{printf("Server IP = %s\nHOSTNAME = %s\n" ,$1,$2 )}' pqr
.
by using a single awk call you reduce the numbers of fork/execs and that
is always a good thing.
.
I would consider using awk to do all of the parsing and not just to
find a field with % in it. You could get rid of the sed|cut pipeline
altogether then. Not sure where $PERC is assigned it looks like it isn't
at least in this snippet. So you could remove it.
This example is using awk instead of the awk/sed/cut pipeline.
ALERT=`df -h | awk '{for (i=1;i<=NF;i++) {
. if ($i ~ /%$/) {
. gsub(/%/," ");
. printf("%s ", $i);
. }
. }
}'`
.
I think that your wanting to read the file abc.txt and your expecting to have
the filesystem sizes in it. However your ";echo $PERC >" will just create an
empty file So I think what you really wanted was ";echo $ALERT > abc.txt "
.
However that is not required you could remove the ALERT var altogether and just have
awk create the file for you in one shot. Like this:
df -h | awk '{for (i=1;i<=NF;i++) {
. if ($i ~ /%$/) {
. # now we found the % to match on so we remove it.
. gsub(/%/," ");
. printf("%s ", $i);
. }
. }
}' > abc.txt
.
Last thing I would consider doing is setting vars for the "65" as well as
for the various file names. That way if you ever change or add to them
you can make the change in a single place. You could also put the
'if [ $i -ge 65 ];then ' in the above awk program and only print the
if you chose to. ( which is likely what I would do).
.
Just a style thing I like whitespace and indentation. keep the lines
short and simple. Observe standard C like indentation for both the shell
and awk programs. 2 years from now when you go back to the program that
will go along way toward helping you relearn what you did. Oh and comments
make sure you include some you can use the # sign for comments in your awk
blocks as well. Of course everyone has their own style so pick what you
like then try to stick with it.
.

Many thanks for the suggestions and ideas you shared with me, Really
appreciate that you took some time to explain me the following. Surely
ur code looks much better ans simpler than mine, its just that Im newbie
to shell to I sometimes write complicated codes:-)

I m really gaining invaluable knowledge here on this blog..thanks to all