I need decimal comparing with if. Check if apache version is less than 2.2.17.
I tried this and not working.
#!/bin/bash
apachever=`/usr/local/apache/bin/httpd -v | head -1 | awk '{print $3}' |cut -d/ -f 2`
if [[ $(echo "$apachever < 2.2.17" |bc) -eq 1 ]]; then
echo "Apache version less than 2.2.17"
else
echo "Apache version greater or equal to 2.2.17"
fi
That won't work. That string comparison will consider 2.2.3 greater than 2.2.20. Each component of the version string needs to be considered separately and must be treated numerically.
Note that some non-numeric characters might be found in some v.r.l strings:
The version is always described with a triple <version,revision,level>
and is represented by a string which always matches the regular
expression ""[0-9]+\.[0-9]+[sabp.][0-9]+"".
-- excerpt from man shtool-version
although I have not seen the sabp in, for example, the apache2 version on my system. An example from the man page is 1.2b3
I have seen instances where an underline is used: java version "1.6.0_22"