Operating System - HP-UX
1833210 Members
3040 Online
110051 Solutions
New Discussion

Re: I have problem in scripting

 
SOLVED
Go to solution
Ammar_4
Frequent Advisor

I have problem in scripting

Hi
How can i take and compare numeric value in "if" condition i have following script but that is not comparing numeric value and is not working according to condition

1 #!/usr/bin/sh
2 NUM=`df -k /stand |awk '/allocation used/'`
3 if [ $NUM -gt 70 ]
4 then
5 echo "$NUM"
6 fi
7 NUM=`df -k / |awk '/allocation used/'`
8 if [ $NUM -gt 70 ]
9 then
10 echo "$NUM"
11 fi
12 NUM=`df -k /tmp |awk '/allocation used/'`
13 if [ $NUM > 70 ]
14 then
15 echo "$NUM"
16 fi
17 NUM=`df -k /var |awk '/allocation used/'`
18 if [ $NUM > 70 ]
19 then
20 echo "$NUM"
21 fi
10 REPLIES 10
Rodney Hills
Honored Contributor
Solution

Re: I have problem in scripting

Try-

2 NUM=`df -k /stand |awk '/allocation used/{print $1}'`
3 if [ $NUM -gt 70 ]
4 then
5 echo "$NUM"
6 fi

NUM was being set to the number plus the text that follows. The print $1 will print the 1st argument from the line.

HTH

-- Rod Hills
There be dragons...
Alan Meyer_4
Respected Contributor

Re: I have problem in scripting

I believe that you are missing quotes around the variable. It should be

if [ $NUM" -gt 70 ]
then
.
.
.
fi
" I may not be certified, but I am certifiable... "
Alan Meyer_4
Respected Contributor

Re: I have problem in scripting

I believe that you are missing quotes around the variable. It should be

if [ "$NUM" -gt 70 ]
then
.
.
.
fi

oops I missed one myself
" I may not be certified, but I am certifiable... "
Matthew_50
Valued Contributor

Re: I have problem in scripting

declare NUM as integer

---------------- sample --------------------
integer NUM=`df -k /var |awk '/allocation used/ {print $1}'`
if [[ ${NUM} -gt 70 ]]; then
echo "${NUM}"
fi
---------------- sample --------------------
Alan Meyer_4
Respected Contributor

Re: I have problem in scripting

Nevermind... I was going by a syntax book, Sir Rodney's right. ~warmsmile~
" I may not be certified, but I am certifiable... "
Ammar_4
Frequent Advisor

Re: I have problem in scripting

Please also tell me now if file system is running more than 70% then system send me email. i have used "mailx" but i didnot know how to link that script and how email work on specific condition
Orhan Biyiklioglu
Respected Contributor

Re: I have problem in scripting

actually

NUM=`df -k /stand |awk '/allocation used/'`
echo $NUM

returns something like:

52 % allocation used

which is not just numeric

you should use
NUM=`df -k /stand |awk '/allocation used/{print $0}'`

instead.
Rodney Hills
Honored Contributor

Re: I have problem in scripting

Usually to send email I would do-

echo "Over 70 percent /tmp" | mailx -s "70% problem" joeuser@company.com

HTH

-- Rod Hills
There be dragons...
Alan Meyer_4
Respected Contributor

Re: I have problem in scripting

.
.
.
NUM=df -k /var |awk '/allocation used/{print $1}'
if [ $NUM -gt 70 ]
then
echo "/var is above 70% utilized" |mailx email@address
fi
.
.
.
" I may not be certified, but I am certifiable... "
Ammar_4
Frequent Advisor

Re: I have problem in scripting

Thanks a lot
i am learning too much from u guys