I'm struggling in if statment
I want the message "The system is highly utilized" pops whenever the memory usage or cpu usage exceeds 70%, now i tried the following 2 conditions in if statment but it gives me error.
# This script monitors CPU and memory usage
RED='\033[0;31m'
NC='\033[0m' # No Color
while :
do
Get the current usage of CPU and memory
limit=70.0
cpuUsage=$(top -bn1 | awk '/Cpu/ { print $2}')
memTotal=$(free -m | awk '/Mem/{print $2}')
memUsage=$(free -m | awk '/Mem/{print $3}')
memUsage=$(( (memUsage * 100) / memTotal ))
Print the usage
echo "CPU Usage: $cpuUsage%"
echo "Memory Usage: $memUsage%"
Sleep for 1 second
sleep 1
if (( $(echo "$cpuUsage > $limit ; $memUsage > $limit" |bc -l) ))
then
printf "${RED}The system is highly utilized${NC}\n"
else
echo The system is not highly utilized
fi
done
as far as i know the ; runs checks 1st condition and then goes to the 2nd regardless of success. I get this error anyways: 0 : syntax error in expression (error token is "0 ")
echo
insideif
? You don't need it. – user68186 Apr 25 '23 at 15:30