Need help with if [ ] in my script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with if [ ] in my script?
# 1  
Old 04-12-2002
Java Need help with if [ ] in my script?

In the following sccript I want to be able to only send different notifications based in whether the filesyste is over 80% or 90%. I need help with if [ ]. How would I make it so that if a filesystem was over 80% but less than 90% only do one thing. The way I currently have it setup I end up getting double notifications when a filesystem is over 90%.


#!/bin/bash

host=`uname -n`
NOTIFYEMAIL="joe.blow@me.com"
NOTIFYPAGE="joe.blow@me.com"

# Notification message
root_msg="FileSystem `df -h / | tail -1 | awk '{print $1 " is " $5 " Utilized Mounted on " $6 " with a Size of " $2 " has Used " $3 " Leaving only " $4 " of Available Space. "}'`"

# Set Severity level
sev1_Alarm="mailx -s $host $NOTIFYPAGE $NOTIFYEMAIL"
sev2_Warn="mailx -s $host $NOTIFYEMAIL"

# Extract filesystem % full integer
rootuse=`df -h / | awk '{print $5}' | cut -c1-2 | grep '[0-9]'`

# / filesystem
if [ "$rootuse" -gt "80" ]
then
echo $root_msg | $sev2_Warn
if [ "$rootuse" -gt "90" ]
then
echo $root_msg | $sev1_Alarm
fi
fi
# 2  
Old 04-12-2002
I don't use bash but it should work with &&

something like (this was from a csh script):
if ($sq[1] == "2" && $sq[2] == "2" && $sq[3] == "2") then
thehoghunter
# 3  
Old 04-12-2002
I couldn't get this to work.

Maybe I don't understand the concept of your example. Can you explain the [1] [2] [3] in your example and how it correalates to the 80 & 90?


Would it go something like this?

if ($rootuse[1] == "2" && $rootuse[2] == "2" && $rootuse[3] == "2")


This is the idea I had in my head I just don't know how to make it work. I know this is obviously syntactically incorrect but it makes more sense to me.

if [ "$rootuse" -gt "80" & less than "90"] then
echo $sev2_Warn
if [ "$rootuse" -gt "80" & -gt "90"] then
echo $sev1_Alarm
fi
fi
# 4  
Old 04-13-2002
First, your if-statement needs an "else" to make it do only one action at most. And of course you want that one action to be the most severe, so check for gt 90 first:
Code:
if [ "$rootuse" -gt 90 ]
then
   echo $root_msg | $sev1_Alarm
else
   if [ "$rootuse" -gt 80 ]
      then
         echo $root_msg | $sev2_Warn
   fi
fi

Also, = and != are for strings, and -gt -lt -eq etc are numeric tests, so I use 90 instead of "90".

And when you pull % full, your cut gets only 2 digits because you want to leave the %-sign behind. But when it hits 100, your code will pull only 10 instead of 100. I can suggest the following code instead, which pipes into only awk and avoids 2nd and 3rd pipe:

rootuse=`df -h / | awk 'BEGIN{getline}{print substr($5,1,length($5)-1)}'`

Last edited by Jimbo; 04-13-2002 at 09:04 AM..
Jimbo
# 5  
Old 04-14-2002
You wrote:
Maybe I don't understand the concept of your example. Can you explain the [1] [2] [3] in your example and how it correalates to the 80 & 90?

Would it go something like this?

if ($rootuse[1] == "2" && $rootuse[2] == "2" && $rootuse[3] == "2")

It's an example of having multiple statements - each has to be true for the "if (true) then command" to work. Like I wrote - I don't use bash and this was only an example - you can change it to use less than, equal, greater than... It was the && you needed for yours. Jimbo put you on the correct track.
thehoghunter
# 6  
Old 04-15-2002
That is what I needed ... Thanks

I didn't even think about accounting for a filesystem that reaches 100%. I used your suggestions and it is working great. Excellent suggestions Jimbo.

Ahh.... now I understand your example with the multiple statements, hoghunter. Thanks for the feedback.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question