My VAR1 and VAR2 works fine and able to get the value. I want to do as "if VAR1 or VAR2 is bigger than max_loadavg then the code will run...then if VAR1 or VAR2 is lesser than min_loadavg then the other code will run...
The problem:
The script run but seems works at max_loadavg and not the min_loadavg line. Please help
p/s: thanks pludi to add the taqs
Part of the Code:
max_loadavg=50.0
min_loadavg=10.0
VAR1=$(cat /proc/loadavg | awk '{print $1}')
VAR2=$(ssh web2 -C cat /proc/loadavg | awk '{print $1}')
if [ $(echo "$VAR1 >= $max_loadavg && $VAR2 >= $max_loadavg"|bc) -eq 1 ]
execute the code
fi
if
if [ $(echo "$VAR1 <= $min_loadavg && $VAR2 <= $min_loadavg"|bc) -eq 1 ]
execute the code
fi
if [ $VAR1 -gt $max_loadavg ] || [ $VAR2 -gt $max_loadavg ] ; then
echo code will run
elif [ $VAR1 -lt $min_loadavg ] || [ $VAR2 -lt $min_loadavg ] ; then
echo code will run
else
echo code wont run
fi
I am not clear with the conditions you provided.
you wrote "or" in explanation and used "&&" in code. max and min name are also seems to be reverse..
by the way, you can handle that with you own.
use && instead || for "AND".
le and ge for adding ( less then or equal to or greater then or equal to )
If anyone of the values of variable VAR1 or VAR2 is greater than max value or less than min value,then you need to use || operator instead of && operator in your script.If you want both the values should be greater than max value or less than min value,then only you need to use && operator.
Below is the sample code for you to better understanding.
val=5
max=10
min=50
if [[ $val -gt $max || $val -gt $max ]];then
echo "Maximum Matched and its Value:$val"
elif [[ $val -lt $min || $val -lt $min ]];then
echo "Minimum Matched and its Value:$val"
fi
#!/bin/bash
# Load average ALERT value.
# Maximum load injection trigger
max_loadavg=15.0
# Minimum load injection trigger
min_loadavg=10.0
loadavg=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web1 current LOAD
VAR1=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web2 current LOAD
VAR2=$(ssh web2 -C cat /proc/loadavg | awk '{print $1}')
# To check current LOAD for both Database value and trigger the injection
if [ $(echo "$VAR1 >= $max_loadavg && $VAR2 >= $max_loadavg"|bc) -eq 1 ]
then
EXECUTE THE CODE
fi
# To check current LOAD for both Database value and trigger the injection
if [ $(echo "$VAR1 <= $min_loadavg && $VAR2 <= $min_loadavg"|bc) -eq 1 ]
then
EXECUTE THE CODE
fi
exit
---------- Post updated at 08:26 AM ---------- Previous update was at 08:19 AM ----------
I modify my code as below but the error come out:
recoverwebload: line 22: [[2.28: command not found
recoverwebload: line 22: [0.20: command not found
recoverwebload: line 36: [[2.28: command not found
recoverwebload: line 36: 0.20: command not found
#!/bin/bash
# Load average ALERT value.
# Maximum load injection trigger
max_loadavg=15.0
# Minimum load injection trigger
min_loadavg=10.0
loadavg=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web1 current LOAD
VAR1=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web2 current LOAD
VAR2=$(ssh web -C cat /proc/loadavg | awk '{print $1}')
# To check current LOAD for both Database value and trigger the injection
if [[$VAR1 -gt $max_loadavg || [$VAR2 -gt $max_loadavg]]
then
EXECUTE THE CODE
fi
# To check current LOAD for both Database value and trigger the injection
if [[$VAR1 -lt $min_loadavg || $VAR2 -lt $min_loadavg]]
then
EXECUTE THE CODE
fi
exit
I don't know why you're echoing the value and comparing with max value and passing it to bc.You simply take my example script and do your script easily.
You change your condition as follows.
if [[ $VAR1 -gt $max_loadavg || $VAR2 -gt $max_loadavg]];then
execute the code
elif [[ $VAR1 -lt $min_loadavg || $VAR2 -lt $min_loadavg]];then
execute another code
fi
You change the >= operator to -gt which is used for numeric comparison and <= into -lt.
after leaving a space and run the script i got this:
recoverwebload1: line 20: [[: 1.19: syntax error in expression (error token is ".19")
recoverwebload1: line 20: [[: 0.39: syntax error in expression (error token is ".39")
recoverwebload1: line 28: [[: 1.19: syntax error in expression (error token is ".19")
recoverwebload1: line 28: [[: 0.39: syntax error in expression (error token is ".39")
#!/bin/bash
# Load average ALERT value.
# Maximum load injection trigger
max_loadavg=15
# Minimum load injection trigger
min_loadavg=10
loadavg=8
#loadavg=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web1 current LOAD
VAR1=9
#VAR1=$(cat /proc/loadavg | awk '{print $1}')
# Grab Web2 current LOAD
VAR2=10
#VAR2=$(ssh web -C cat /proc/loadavg | awk '{print $1}')
# To check current LOAD for both Database value and trigger the injection
if [ $VAR1 -gt $max_loadavg ] || [ $VAR2 -gt $max_loadavg ]
then
echo EXECUTE THE CODE 1
fi
# To check current LOAD for both Database value and trigger the injection
if [ $VAR1 -lt $min_loadavg ] || [ $VAR2 -lt $min_loadavg ]
then
echo EXECUTE THE CODE 2
fi