how to convert a string to a float and then use it in a conditional operation? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















This question already has an answer here:



  • How to compare a program's version in a shell script?

    3 answers



I'm trying to write my own bash script that detects if Debian's release version is less than 9.0



code:



VERSION=$(cat /etc/debian_version)
echo $VERSION

if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi


but the error i'm receiving is:




./test.sh: line 4: [: 8.2: integer expression expected




basically, $VERSION is a string type, but it's 8.2. I'm trying to make it become 8.2, but as a float. so I can use <= on it.










share|improve this question















marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • By the way, Bash doesn't support float numbers, it only works with integers.
    – Giovanni Nunes
    Dec 24 '18 at 16:03










  • ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
    – girng rodriguez
    Dec 24 '18 at 20:25















0















This question already has an answer here:



  • How to compare a program's version in a shell script?

    3 answers



I'm trying to write my own bash script that detects if Debian's release version is less than 9.0



code:



VERSION=$(cat /etc/debian_version)
echo $VERSION

if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi


but the error i'm receiving is:




./test.sh: line 4: [: 8.2: integer expression expected




basically, $VERSION is a string type, but it's 8.2. I'm trying to make it become 8.2, but as a float. so I can use <= on it.










share|improve this question















marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • By the way, Bash doesn't support float numbers, it only works with integers.
    – Giovanni Nunes
    Dec 24 '18 at 16:03










  • ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
    – girng rodriguez
    Dec 24 '18 at 20:25













0












0








0


1






This question already has an answer here:



  • How to compare a program's version in a shell script?

    3 answers



I'm trying to write my own bash script that detects if Debian's release version is less than 9.0



code:



VERSION=$(cat /etc/debian_version)
echo $VERSION

if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi


but the error i'm receiving is:




./test.sh: line 4: [: 8.2: integer expression expected




basically, $VERSION is a string type, but it's 8.2. I'm trying to make it become 8.2, but as a float. so I can use <= on it.










share|improve this question
















This question already has an answer here:



  • How to compare a program's version in a shell script?

    3 answers



I'm trying to write my own bash script that detects if Debian's release version is less than 9.0



code:



VERSION=$(cat /etc/debian_version)
echo $VERSION

if [ "$VERSION" -lt "9.0" ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi


but the error i'm receiving is:




./test.sh: line 4: [: 8.2: integer expression expected




basically, $VERSION is a string type, but it's 8.2. I'm trying to make it become 8.2, but as a float. so I can use <= on it.





This question already has an answer here:



  • How to compare a program's version in a shell script?

    3 answers







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '18 at 7:05









msp9011

3,80843863




3,80843863










asked Dec 24 '18 at 5:55









girng rodriguez

33




33




marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, A.B, Stephen Harris, Thomas, icarus Dec 25 '18 at 17:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • By the way, Bash doesn't support float numbers, it only works with integers.
    – Giovanni Nunes
    Dec 24 '18 at 16:03










  • ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
    – girng rodriguez
    Dec 24 '18 at 20:25
















  • By the way, Bash doesn't support float numbers, it only works with integers.
    – Giovanni Nunes
    Dec 24 '18 at 16:03










  • ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
    – girng rodriguez
    Dec 24 '18 at 20:25















By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03




By the way, Bash doesn't support float numbers, it only works with integers.
– Giovanni Nunes
Dec 24 '18 at 16:03












ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25




ty @GiovanniNunes is it possible that bash is going to add support for float in the future?
– girng rodriguez
Dec 24 '18 at 20:25










2 Answers
2






active

oldest

votes


















0














Try this, I use to compare the float with bc.



VERSION=$(cat /etc/debian_version)
echo $VERSION
if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
echo "Debian version is less than 9"
else
echo "Debian version is greater than 9"
fi


one-liner using awk



awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version





share|improve this answer






















  • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
    – girng rodriguez
    Dec 24 '18 at 7:14






  • 1




    oh. i just did apt-get install bc. i think it's working now. thank you so much.
    – girng rodriguez
    Dec 24 '18 at 7:16



















1














Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:



check_versions()
# check_version <reference> <version to check>
if [[ $1 == $2 ]]; then
echo 'same'
else
low=$(echo -e "$1n$2"

ref='8.5' # reference version for check

check_versions $ref '8.2' # current version is lower!
check_versions $ref '8.5' # current version is equal!
check_versions $ref '8.12' # current version is greater!


I think that it should a better solution.






share|improve this answer



























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try this, I use to compare the float with bc.



    VERSION=$(cat /etc/debian_version)
    echo $VERSION
    if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
    echo "Debian version is less than 9"
    else
    echo "Debian version is greater than 9"
    fi


    one-liner using awk



    awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version





    share|improve this answer






















    • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
      – girng rodriguez
      Dec 24 '18 at 7:14






    • 1




      oh. i just did apt-get install bc. i think it's working now. thank you so much.
      – girng rodriguez
      Dec 24 '18 at 7:16
















    0














    Try this, I use to compare the float with bc.



    VERSION=$(cat /etc/debian_version)
    echo $VERSION
    if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
    echo "Debian version is less than 9"
    else
    echo "Debian version is greater than 9"
    fi


    one-liner using awk



    awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version





    share|improve this answer






















    • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
      – girng rodriguez
      Dec 24 '18 at 7:14






    • 1




      oh. i just did apt-get install bc. i think it's working now. thank you so much.
      – girng rodriguez
      Dec 24 '18 at 7:16














    0












    0








    0






    Try this, I use to compare the float with bc.



    VERSION=$(cat /etc/debian_version)
    echo $VERSION
    if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
    echo "Debian version is less than 9"
    else
    echo "Debian version is greater than 9"
    fi


    one-liner using awk



    awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version





    share|improve this answer














    Try this, I use to compare the float with bc.



    VERSION=$(cat /etc/debian_version)
    echo $VERSION
    if [ "`echo "$VERSION < 9.0" | bc`" -eq 1 ]; then
    echo "Debian version is less than 9"
    else
    echo "Debian version is greater than 9"
    fi


    one-liner using awk



    awk 'if ($1 < 9.0) print "Debian version is less than 9"; else if ($1 > 9.0) print "Debian version is greater than 9";' /etc/debian_version






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 24 '18 at 7:03

























    answered Dec 24 '18 at 6:30









    msp9011

    3,80843863




    3,80843863











    • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
      – girng rodriguez
      Dec 24 '18 at 7:14






    • 1




      oh. i just did apt-get install bc. i think it's working now. thank you so much.
      – girng rodriguez
      Dec 24 '18 at 7:16

















    • hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
      – girng rodriguez
      Dec 24 '18 at 7:14






    • 1




      oh. i just did apt-get install bc. i think it's working now. thank you so much.
      – girng rodriguez
      Dec 24 '18 at 7:16
















    hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
    – girng rodriguez
    Dec 24 '18 at 7:14




    hi msp9011, i'm getting ./test.sh: line 5: bc: command not found ./test.sh: line 5: [: : integer expression expected
    – girng rodriguez
    Dec 24 '18 at 7:14




    1




    1




    oh. i just did apt-get install bc. i think it's working now. thank you so much.
    – girng rodriguez
    Dec 24 '18 at 7:16





    oh. i just did apt-get install bc. i think it's working now. thank you so much.
    – girng rodriguez
    Dec 24 '18 at 7:16














    1














    Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:



    check_versions()
    # check_version <reference> <version to check>
    if [[ $1 == $2 ]]; then
    echo 'same'
    else
    low=$(echo -e "$1n$2"

    ref='8.5' # reference version for check

    check_versions $ref '8.2' # current version is lower!
    check_versions $ref '8.5' # current version is equal!
    check_versions $ref '8.12' # current version is greater!


    I think that it should a better solution.






    share|improve this answer

























      1














      Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:



      check_versions()
      # check_version <reference> <version to check>
      if [[ $1 == $2 ]]; then
      echo 'same'
      else
      low=$(echo -e "$1n$2"

      ref='8.5' # reference version for check

      check_versions $ref '8.2' # current version is lower!
      check_versions $ref '8.5' # current version is equal!
      check_versions $ref '8.12' # current version is greater!


      I think that it should a better solution.






      share|improve this answer























        1












        1








        1






        Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:



        check_versions()
        # check_version <reference> <version to check>
        if [[ $1 == $2 ]]; then
        echo 'same'
        else
        low=$(echo -e "$1n$2"

        ref='8.5' # reference version for check

        check_versions $ref '8.2' # current version is lower!
        check_versions $ref '8.5' # current version is equal!
        check_versions $ref '8.12' # current version is greater!


        I think that it should a better solution.






        share|improve this answer












        Version there isn't a simple float number and version 1.10 is greater than 1.9. I think that a solution using sort utility, that knows how to handle versions numbers:



        check_versions()
        # check_version <reference> <version to check>
        if [[ $1 == $2 ]]; then
        echo 'same'
        else
        low=$(echo -e "$1n$2"

        ref='8.5' # reference version for check

        check_versions $ref '8.2' # current version is lower!
        check_versions $ref '8.5' # current version is equal!
        check_versions $ref '8.12' # current version is greater!


        I think that it should a better solution.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 24 '18 at 23:44









        Giovanni Nunes

        1112




        1112












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?