Wednesday, May 1, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1517  / 1 Year ago, wed, january 25, 2023, 7:09:04

I have a variable named Seconds_Behind_Master from one of my scripts. The problem is that this variable can either have a numeric value or can also take a string NULL as its value. Now, when I try to execute this script in shell it gets executed but gives a warning like this:



[: Illegal number: NULL


I believe it is due to the fact that in this case the value is NULL but when it compares it with numeral value 60 it gives this warning. How can I rectify it?


More From » command-line

 Answers
5

In this case you should use an arithmetic evaluation - (( expression )):



if (( $Seconds_Behind_Master >= 60 )); then
echo "replication delayed > 60."
elif [ "$Seconds_Behind_Master" = "NULL" ]; then
echo "Delay is Null."
fi





If you want to respect the standard POSIX, then you can use:



if echo $Seconds_Behind_Master | egrep -q '^[0-9]+$' && [ "$Seconds_Behind_Master" -ge "60" ] ; then
echo "replication delayed >= 60."
elif [ "$Seconds_Behind_Master" = "NULL" ]; then
echo "Delay is Null."
fi


More about: Shell - Test a numeric variable.


[#28655] Thursday, January 26, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
montwim

Total Points: 103
Total Questions: 112
Total Answers: 120

Location: Vietnam
Member since Mon, Mar 14, 2022
2 Years ago
;