The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Please help - disk space check script maddhadder71 Shell Programming and Scripting 0 05-08-2008 05:16 AM
Disk space script asadlone Shell Programming and Scripting 8 06-03-2007 12:32 PM
Frustrating Disk space script vivsiv Shell Programming and Scripting 4 06-05-2006 04:53 PM
disk space script debug - posted before bryan Shell Programming and Scripting 3 04-28-2005 04:50 PM
available disk space on disk device??? alan UNIX for Dummies Questions & Answers 4 01-01-2004 11:06 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-23-2007
Registered User
 

Join Date: Mar 2007
Posts: 22
Disk Space Monitoring Script

#!/bin/bash
# Disk Space Monitoring for more than 95 %
# and Sending Alerts by Mail


if [ df -k |awk '$5 > 95 ];

then

`df -k |awk '$5 > 95 {print $1 " ----------- " $5}' |mailx -s "More than 95% disk usage in DEV" email@test.com';

else

exit 0

fi


I get the below error:
================
bash-3.00$ ./disk_usage_mail_alert-sriram.sh
./disk_usage_mail_alert-sriram.sh: line 14: syntax error near unexpected token `else'
./disk_usage_mail_alert-sriram.sh: line 14: `else '
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-23-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,444
that's very kludgy. the usage of awk is more than meets the eye
try this
Code:
df -k | awk 'int($5) > 95{ 
  subject = $1 " More than 95% disk usage "
  email = "email@test.com"
  cmd = "mailx -s \"" subject "\" " email
  cmd  | getline #or system(cmd)
  }
'
Reply With Quote
  #3 (permalink)  
Old 08-23-2007
Registered User
 

Join Date: Mar 2007
Posts: 22
Can you suggest what is the syntax I am missing ?

bash-2.03$ ./new.sh
awk: syntax error near line 5
awk: illegal statement near line 5
./new.sh: line 8: 11058 Broken Pipe df -k
11059 Exit 2 | awk 'int($5) > 95{
subject = $1 " More than 95% disk usage "
email = "email@test.com"
cmd = "mailx -s \"" subject "\" " email
cmd | getline #or system(cmd)
}
'

====================================
bash-2.03$ cat new.sh (the script)
====================================
1
2 df -k | awk 'int($5) > 95{
3 subject = $1 " More than 95% disk usage "
4 email = "email@test.com"
5 cmd = "mailx -s \"" subject "\" " email
6 cmd | getline #or system(cmd)
7 }
8 '
====================================
bash-2.03$

Last edited by sriram003; 08-23-2007 at 06:55 AM.
Reply With Quote
  #4 (permalink)  
Old 08-23-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by ghostdog74 View Post
that's very kludgy. the usage of awk is more than meets the eye
try this
Code:
df -k | awk 'if ( int($5) > 95) { 
  subject = $1 " More than 95% disk usage "
  email = "email@test.com"
  cmd = "mailx -s \"" subject "\" " email
  cmd  | getline #or system(cmd)
  close(cmd)
  }
'
modified the original.....
Reply With Quote
  #5 (permalink)  
Old 08-23-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,444
Quote:
Originally Posted by vgersh99 View Post
modified the original.....
thanks, its good practice to close that. However, for my version of awk (GNU) , the "if" part, i don't think its needed cause i tested it. i think it differs across versions
Reply With Quote
  #6 (permalink)  
Old 08-23-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by ghostdog74 View Post
thanks, its good practice to close that. However, the "if" part, i don't think its needed. but if you could enlighten why its needed, it would be great.
oooops, my bad - I didn't see clearly what you're doing - sorry 'bout that:
Code:
df -k | awk 'int($5) > 95 { 
  subject = $1 " More than 95% disk usage "
  email = "email@test.com"
  cmd = "mailx -s \"" subject "\" " email
  cmd  | getline #or system(cmd)
  close(cmd)
  }
'
for the OP benefit.... if you're under Solaris - use 'nawk' instead of 'awk'.

And once again - sorry for the confusion!

Last edited by vgersh99; 08-23-2007 at 08:27 AM.
Reply With Quote
  #7 (permalink)  
Old 08-23-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,444
Quote:
Originally Posted by vgersh99 View Post
And once again - sorry for the confusion!
no worries..
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0