Shoaib
Shoaib

Reputation: 112

Shell script error while executing

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

Upvotes: 0

Views: 41

Answers (2)

Slava Semushin
Slava Semushin

Reputation: 15214

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 ]

Upvotes: 0

clt60
clt60

Reputation: 63972

Maybe?

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

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

Upvotes: 1

Related Questions