Operating System - Linux
1828888 Members
2699 Online
109985 Solutions
New Discussion

if evaluation doesn't work fine

 
SOLVED
Go to solution
Carles Viaplana
Valued Contributor

if evaluation doesn't work fine

Hello,

I've following script:

"check_disk2" 10 lines, 330 characters
# ./check_disk2 75 90 /dev/vglabccs/lvol1
DISK CRITICAL - [7926736 used (39%) on /dev/vglabccs/lvol1]
# more check_disk2
FREE="$(bdf $3 | tail -1 | cut -d ' ' -f 21)"
PERC="$(bdf $3 | tail -1 | cut -d ' ' -f 25 | cut -d % -f 1)"

if [ $FREE -gt $2 ] ; then
echo "DISK CRITICAL - [$FREE used ($PERC%) on $3]"
elif [ $FREE -gt $1 ] ; then
echo "DISK WARNING - [$FREE used ($PERC%) on $3]"
else
echo "DISK OK - [$FREE used ($PERC%) on $3]"
fi

PERC is filesystem used percentage (without "%") and $1 and $2 are numbers. Problem is when I check if PERC is greater than $2 or $1 as always show DISK CRITICAL message:

# ./check_disk2 75 90 /dev/vglabccs/lvol1
DISK CRITICAL - [7926736 used (39%) on /dev/vglabccs/lvol1]
# ./check_disk2 20 20 /dev/vglabccs/lvol1
DISK CRITICAL - [7926736 used (39%) on /dev/vglabccs/lvol1]
#

I think problem is script check values as strings instead numbers, so it can evaluate as I need.

How can I evaluate them as numbers?

Thanks in advande for your help.
Regards,

Carles
5 REPLIES 5
Peter Godron
Honored Contributor

Re: if evaluation doesn't work fine

Carles,
by wrapping a " around your FREE and PERC evaluation you signal strings.
try:
FREE=`bdf $3 | tail -1 | cut -d ' ' -f 21`
PERC=`bdf $3 | tail -1 | cut -d ' ' -f 25 | cut -d % -f 1`

Kenan Erdey
Honored Contributor

Re: if evaluation doesn't work fine

bdf $3 | tail -1 | cut -d ' ' -f 25 | cut -d % -f 1, is this ok ?

is there 25. field in bdf's output ?
Computers have lots of memory but no imagination
Carles Viaplana
Valued Contributor

Re: if evaluation doesn't work fine

Thanks for your replies.

Both bdf commands has numbers (I already checked it).

About Peter message, I need numbers instead strings in order to evaluate if percentage I get with bdf command is greater than number I pass as parameter.

Thanks for your help.
Regards,

Carles
James R. Ferguson
Acclaimed Contributor
Solution

Re: if evaluation doesn't work fine

Hi Carles:

You need to test 'PERC' not 'FREE' since 'PERC' has had the "%" string trimmed:

if [ $PERC -gt $2 ] ; then
echo "DISK CRITICAL - [$FREE used ($PERC%) on $3]"
elif [ $PERC -gt $1 ] ; then

Regards!

...JRF...
Carles Viaplana
Valued Contributor

Re: if evaluation doesn't work fine

Joder! You're right!

I was evaluating wrong variable, so script and if construction are fine.

Thanks for showing my little mistake that will allows me save a lot of work to some people.

Regards,

Carles