Compare a variable that can have numeric or string as value
Posted
by
Tarun
on Ask Ubuntu
See other posts from Ask Ubuntu
or by Tarun
Published on 2013-10-30T08:41:43Z
Indexed on
2013/10/30
10:21 UTC
Read the original article
Hit count: 233
command-line
|shell-scripting
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?
Here is the piece of code:
Seconds_Behind_Master=$Show_Slave_Status | grep "Seconds_Behind_Master" | awk -F": " {' print $2 '}
if [ "$Seconds_Behind_Master" -ge "60" ]; then
echo "replication delayed greater than or equal to 60."
else
if [ "$Seconds_Behind_Master" = "NULL" ]; then
echo "Delay is Null."
fi
fi
© Ask Ubuntu or respective owner