I'm trying to create a simple script that checks if the CPU Usage is at a certain level. I have a Raspberry Pi for this project.
The only issue I have currently is that I have the variable CPU which is the CPU usage, when the script runs, I get the error
line 25: [: 4.8: integer expression expected
I'm not sure why bash returns this if BASH really doesn't care about int, String, etc. like JAVA does. Found that out here.
Also, I have the if statement: if $CPU -gt 1 only for testing (it's supposed to return true right now)
red='\e[0;31m'
yellow='\e[1;33m'
NC='\e[0m' # No Color
echo -e "${NC}Starting Server!${NC}"
echo -e "${red}Errors in red!${NC}"
echo -e "${yellow}Info in yellow!${NC}"
sleep 1
echo -e "\n\n"
##CPU USAGE STUFF
echo -e "${yellow}Current CPU Usage:"
CPU=$(top -bn1 | grep "Cpu(s)" | \
sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
awk '{print 100 - $1""}')
echo $CPU %
echo -e "${NC}**********${NC}"
cd /root/mc_server/
sleep 1
##CPU USAGE LOOP CHECKER
while :
do
sleep 5
echo $CPU
if [ $CPU -gt 1 ]
then
echo -e "${red}ALERT! CPU Usage is TOO HIGH!${red}"
fi
done
##
##java -Xmx400M -jar bukkit.jar -o true
case $CPU in *\.* ) CPU=${CPU%%.*} ;; esacmight help. Good luck.$CPU. The most efficient way is to remove everything up to and including the decimal beginning at the right with substring extraction.CPU=${CPU%.*}%%works, the only difference being it say remove all occurrences of.beginning at the right, compared to%which isfirst occurrence.