Need help on Linux script to monitor hard drive space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on Linux script to monitor hard drive space
# 1  
Old 02-10-2010
Need help on Linux script to monitor hard drive space

I'm new to Linux and have very limited experience with shell scripts in general. I am taking a class and I have to research online and come up with a shell script that monitors disk space. I also have to be able to explain it line by line. I've researched various sites and came across this shell script. I typed it in and saved it but when I try to run it I get this error message: line 10: [: Use: integer expression expected. Line 10 is this one:
Code:
if [ $usep -ge $ALERT ]; then

Now that line uses the output from line 8, as well as the defined variable from line 4. Now line 4 is an integer, and when I look at line 8, it looks like it should resolve be an integer...so I can't figure out what is wrong here. Any help would be appreciated.

Code:
#!/bin/bash
# comment
ADMIN="(I have an email address here)"
ALERT=90
df -H | grep -vE '^none' | awk ' { print $5 " " $1 }' | while read output;
do
           #echo $output
           usep=$(echo $output | awk ' { print $1 }' | cut -d'%' -f1 )
           partition=$(echo $output | awk '{ print $2 }' )
           if [ $usep -ge $ALERT ]; then
                     echo "Running out of space \"$partition ($usep%)\" on$(hostname) as on $(date)"|
                               mail -s "Alert: Almost out of disk space $usep" $ADMIN
           fi
done


Last edited by pludi; 02-10-2010 at 05:28 PM.. Reason: code tags, please...
# 2  
Old 02-10-2010
You can try forcing the issue this way:

Code:
# First declare usep as an integer
typeset -i usep 
usep=$(echo $output | awk ' { print $1 }' | cut -d'%' -f1 )

# 3  
Old 02-10-2010
Quote:
Originally Posted by wbathurs
You can try forcing the issue this way:

Code:
# First declare usep as an integer
typeset -i usep 
usep=$(echo $output | awk ' { print $1 }' | cut -d'%' -f1 )

That seems to have worked. Thanks.

Just out of curiosity, do you have any idea why it didn't work as originally scripted? It seems to me that it should have evaluated out to an integer so I'm not understanding what went wrong.
# 4  
Old 02-11-2010
for this warning " message: line 10: [: Use: integer expression expected "
i think it because bash shell cant read -gt,-lt, etc
u can use ksh so they can read it

and i've tried that script before but im using ksh shell
like this
Code:
#!/bin/ksh
# comment
ADMIN="(I have an email address here)"
ALERT=90
df -H | grep -vE '^none' | awk ' { print $5 " " $1 }' | while read output;
do
           #echo $output
           usep=`(echo $output | awk ' { print $1 }' | cut -d'%' -f1 )`
           partition=`(echo $output | awk '{ print $2 }' )`
           if [ $usep -ge $ALERT ]; then
                     echo "Running out of space \"$partition ($usep%)\" on$(hostname) as on $(date)"|
                               mail -s "Alert: Almost out of disk space $usep" $ADMIN
           fi
done

hope this can help u Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question about setting up a hard drive for a videoserver under Linux

Hi there, I'm currently preparing for an exam and came across a question that I don't really know how to answer. "You want to set up a hard drive for a videoserver under linux. - The videofiles will have a size of at least 10MB and a maximum of 8BG. - The hard drive has a disk space of... (1 Reply)
Discussion started by: Eggsy
1 Replies

2. UNIX for Dummies Questions & Answers

Copy everything from remote Linux to external hard drive in windows

Hi, I`m trying to copy an entire directory(with numerous files, folders and subfolders etc, a content size of 500gigs) from a linux machine running Red Hat Enterprise Linux 6 to an external hard drive on my windows. I downloaded and installed psftp on my windows and I can login using psftp... (9 Replies)
Discussion started by: newbie83
9 Replies

3. Fedora

Need to incrwase PHYSICAL VOLUME space on hard drive with free space on it

Hi, I run Fedora 17. I created a physical volume of 30GB on a disk with 60GB of space so there is 30GB of free space. On the physical volume, I created my volume group and logical volumes. I assigned all the space in the physical volume to my volume group. I need to add the 30GB of free space... (1 Reply)
Discussion started by: mojoman
1 Replies

4. UNIX for Dummies Questions & Answers

Reformat WD 500 GB hard drive for Linux machine

Linux Red Hat machine GNOME version 2.16.0 External hard drive is a Western Digital 500 GB My Book Essential. How can I reformat the external hard drive so that I can backup my Linux machine? Thanks (1 Reply)
Discussion started by: jm4smtddd
1 Replies

5. UNIX for Advanced & Expert Users

How to install Linux on an external 320 gigabyte hard drive

Hi all, I would to know if I can install Linux on an external 320 gigabyte hard drive..I have Windows XP on my internal hard drive which is 80 GB , but this installation on 320 external HDD is for testing purpose If it is possible to install Linux on the external drive, will it cause any... (5 Replies)
Discussion started by: bsandeep_80
5 Replies

6. UNIX for Dummies Questions & Answers

Hard Disk drive space gone missing...

Sorry if this is totally the wrong place to post this but I have a question or something rather thats bugging me. I got a new Dell Inspiron laptop the other day and I was expecting it to have 80G on it, well atleast 70 or so after installation of OS and such but after looking carefully yesterday, I... (8 Replies)
Discussion started by: Ecclesiastes
8 Replies

7. Filesystems, Disks and Memory

The best partitioning schem for a 250GB Sata hard drive & a 75GB SCSI hard drive

Hi I have 2 75GB SCSI hard drives and 2 250GB SATA hard drives which are using RAID Level 1 respectively. I wana have both FTP and Apache installed on them as services. I'm wondering what's the best partitioning schem? I wana use FC3 as my OS, so, I thought I can use the 75GB hard drive as the /... (0 Replies)
Discussion started by: sirbijan
0 Replies

8. UNIX for Dummies Questions & Answers

Re-allocating hard drive space

Hi, Is their an easy way to realloate hard drive space on Solaris 10. For example : /c20td0 10G /space 90 G I would like to move some of the hard-drive space from "/space" and add it to "/c20td0". In Windows this can be easily done using Partition magic, anything similar for UNIX? (4 Replies)
Discussion started by: annointed3
4 Replies

9. UNIX for Dummies Questions & Answers

Format Hard Drive in Linux

What Command do I need to use to Format my hard Drive in Linux. Please help me out. -Regards -Iftikhar:) (2 Replies)
Discussion started by: syedifti
2 Replies
Login or Register to Ask a Question