1

Having a hard time passing a "greater than or equal to" value in my if-then-else statement with a decimal integer ran as a one-liner and curling the phpinfo.php page to check if the domain's PHP version is greater than or equal to 7.0. Obviously a php info file (phpinfo.php) is created in the main dir of where site is assigned. Here is my command displayed in human readable:

read -ep "Enter the domain: " DOMAIN ; 
PHPVERSION=$(curl -s https://$DOMAIN/phpinfo.php | grep -ioP 'php version\s\K\d.\d') ; 
if [ $PHPVERSION >= 7.0 ] ; 
  then 
    echo -e "PHP version at least 7.0? YES" ; 
  else 
    echo -e "PHP version at least 7.0? NO\nPHP version = $PHPVERSION" ; 
fi ; 
[~]# echo $PHPVERSION
7.0

Here are the tests/outputs I use/get when attempting to run the command

if [ $PHPVERSION >= 7.0 ]

-jailshell: [: 7.0: unary operator expected
PHP version at least 7.0? NO
PHP version = 7.0

if [ $PHPVERSION -ge 7.0 ]

-jailshell: [: 7.0: integer expression expected
PHP version at least 7.0? NO
PHP version = 7.0

if [[ $PHPVERSION >= 7.0 ]]

-jailshell: syntax error in conditional expression
-jailshell: syntax error near `7.0'

if [[ $PHPVERSION -ge 7.0 ]]

-jailshell: [[: 7.0: syntax error: invalid arithmetic operator (error token is ".0")
PHP version at least 7.0? NO
PHP version = 7.0

I can get it to work just fine as > or -gt but the PHP version I'm requiring for my test is greater than or equal to 7.0. What am I missing here? I've also tried passing 7.0 as a variable and testing it that way but get the same output on virtually all my tests. I've also tried saving 7.0 as a variable using echo and piping it into bc but I don't think I'm doing it right. Also double quotes around variable don't seem to help either. I feel like this is something simple but I've definitely looked around at all articles I could find on the subject and have yet to come up with anything that works. Thanks for the help.

5
  • 7.0 is no integer. Commented Dec 23, 2020 at 7:49
  • You were close. [[ $PHPVERSION > 7.0 || $PHPVERSION == 7.0 ]] is what you're looking for. Within double brackets > compares lexicographically, but >= doesn't work that way. Commented Dec 23, 2020 at 7:49
  • 7 is an integer. 7.0 is not an integer; it is a real number. Bash does not support operations on real numbers; only operations on integer numbers. Commented Dec 23, 2020 at 7:49
  • 1
    @oguzismail that was it thank you so much Commented Dec 23, 2020 at 7:53
  • The problem is with the number 7.0 is not an integer, it is a float. you have to treat it as a float. Commented Dec 23, 2020 at 8:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.