Shell Script Question

Shell Script Question

Post by Reynold Ma » Fri, 08 May 1998 04:00:00



        Hi. We're running AIX 4.1.5.  I have to write a simple(?) cron
that checks the free space of a disk volume and send an email to a
user to inform him that the disk space is low.  

I can handle the cron part, email part and checking of free disk
space.  However, I'm kinda stuck when actually writing the thing.
Here's what I came up with so far:

-----------------------
#!/bin/sh
df -k /filesys1 > output   #This gives info about the filesys
awk '{print $3}' output > newoutput  #This gives me the word Free and
the actual disk free space

grep -v Free newoutput #This gives me the disk free space I want
-----------------------
I'm stuck at this point.  How do I compare the result of the last
statement to an actual integer?  How does the system know if the
number created from the grep command is actually a number and not a
string?  Do I have to specify variables and variable types?  

I can then run an if then statement that will check the disk free
space with a specified amount and email the results to the user.

Thanks for any help or suggestions.

 
 
 

Shell Script Question

Post by Glenn Wes » Fri, 08 May 1998 04:00:00



>         Hi. We're running AIX 4.1.5.  I have to write a simple(?) cron
> that checks the free space of a disk volume and send an email to a
> user to inform him that the disk space is low.

> I can handle the cron part, email part and checking of free disk
> space.  However, I'm kinda stuck when actually writing the thing.
> Here's what I came up with so far:

> -----------------------
> #!/bin/sh
> df -k /filesys1 > output   #This gives info about the filesys
> awk '{print $3}' output > newoutput  #This gives me the word Free and
> the actual disk free space

> grep -v Free newoutput #This gives me the disk free space I want
> -----------------------
> I'm stuck at this point.  How do I compare the result of the last
> statement to an actual integer?

You'll have to do something like:

free_space=`grep -v Free newoutput`
if [ $? -ne 0 ]; then
        echo Error on grep
        exit 1
fi

if [ "$free_space" -lt 10000 ]; then
#       send a mail message?
fi

Quote:>How does the system know if the
> number created from the grep command is actually a number and not a
> string?

It doesn't.  It's your responsibility to ensure that it is a valid
number.  You could use something like:

if [ -n "`echo $free_space|tr -d [0-9]`" ]; then
        echo $free_space is not a number!
fi

Quote:>Do I have to specify variables and variable types?

In the Bourne shell there is only one variable type: a string.

- Show quoted text -

Quote:

> I can then run an if then statement that will check the disk free
> space with a specified amount and email the results to the user.

> Thanks for any help or suggestions.


 
 
 

Shell Script Question

Post by Christian Bauernfei » Fri, 08 May 1998 04:00:00




Quote:> I'm stuck at this point.  How do I compare the result of the last
> statement to an actual integer?  How does the system know if the
> number created from the grep command is actually a number and not a
> string?  Do I have to specify variables and variable types?  

> I can then run an if then statement that will check the disk free
> space with a specified amount and email the results to the user.

The trick is which comparison operator you use. = compares strings,
-eq (or -gt, -lt etc) compares numbers.

So you would want to do something like

minfree=100

if [[ $(df -k $filesys | awk '/dev/ {print $3}') -lt $minfree ]]
then
   mail -s "You're running out of space" $user
fi

(all of which is, of course, documented, in the man pages.)

Christian
--
--
Christian Bauernfeind
Not speaking for Siemens
Not even working for IBM