0

I have written a shell script but it is giving error, following is my code

#!/bin/bash
CPU= cat /proc/loadavg | awk '{print$1}'
if  [ $CPU -gt 6 ]
then
echo "Current CPU is: $CPU" | mail -s "CPU Load is Getting High" [email protected]
fi 

When I execute this script, it gives the following error:

cpu_load.sh: line 3: [: -gt: unary operator expected
2
  • That means $CPU has non numeric data. Print it before if using echo "<$CPU>" Commented Aug 21, 2014 at 21:41
  • Are you missing a couple of back-ticks in the assignment of CPU? Commented Aug 21, 2014 at 21:42

2 Answers 2

1

Maybe?

CPU=$(cat /proc/loadavg | awk '{print $1}')

The $(...) called as command substituion - assigning the result of the command to variable

Sign up to request clarification or add additional context in comments.

Comments

0

It means that command cat /proc/loadavg | awk '{print$1}' has returned nothing. To workaround it you could assign to variable default value:

if  [ ${CPU:-0} -gt 6 ]

or you could check variable before use it, like that:

if  [ -n "$CPU" -a "$CPU" -gt 6 ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.