1

I'm a newbie to Linux shell scripting. I have this script but I can't get one portion of the script to work. This script is supposed to email me everytime the cpu number reach 90 or above. The issue is, its comparing to a decimal and its not functioning as it should. Can you all please advise:

#!/bin/ksh
top -b -n 1 |head -8 >/tmp/cpu.txt
sed -e '1,5d' /tmp/cpu.txt >/tmp/cpu2.txt
Test_CPU=`cat /tmp/cpu2.txt|tail -1|awk '{print $9}'`
host=`hostname`
if [ $Test_CPU -gt  90 ];
then
mail -s "Test CPU USE on $host" email@whatever.com < /tmp/cpu2.txt
fi
exit

I'm receiving the below error message: test.sh: line 6: [: 78.9: integer expression expected

I have tried using if [ $High_CPU > 90 ];

I receive the error test.sh: line 6: 90.0: command not found

Please help.

  • Can you post the link to the question that is a duplicate so that I can see the answer....i looked at it, mine has a decimal and a regular interger. its different from that question. Plus there was no solution. can you unmark this because this is different from that. – user3426629 Dec 28 '18 at 17:15

1 Answers1

0

Try with quotes:

if [ "$Test_CPU" -gt  90 ]; then
...
fi

it should work...

Dev.rb
  • 487
  • 6
  • 14
  • No go, just tried it, that didn't work, I received: test.sh: line 6: [: 98.0: integer expression expected – user3426629 Dec 28 '18 at 17:08
  • the error is itself suggesting that `$Test_CPU` is not having integer value. Please check what `$Test_CPU` is returning? – Dev.rb Dec 28 '18 at 17:22
  • The $Test_CPU is returning a decimal always, so its comparing a decimal to 90, an example would be 85.7 > 90 and I believe that, its not fully doing the comparision because of it. I was able to get it to work like this if [ "$Test_CPU -gt 90" ]; but its not doing the comparison, im receiving an email, even if it is below 90. I should only receive if CPU is higher than 90, i can't figure out why the comparison is not working and thanks a million for helping out. – user3426629 Dec 28 '18 at 17:31