compare two IP address using bc

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











up vote
0
down vote

favorite












I am trying to compare two IP address using bc command however the IP with lower numeric value is not being trapped in the test.



Here is my code:



TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q )


If TEST results to 1, the process continues else exits.



check the below example:



++ echo '191.35 < 187.254 && 191.35 > 184.1 '
+ TEST=0
++ echo '191.35 < 191.254 && 191.35 > 188.1 '
++ bc -q
+ TEST2=0
++ echo '191.35 < 195.254 && 191.35 > 192.1 '
++ bc -q
+ TEST3=0
+ [[ 0 = 1 ]]
+ [[ 0 = 1 ]]
+ [[ 0 = 1 ]]


In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.



I have tried double quoutes in the TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q ) however didnt had much success.



Any ideas how to fix it ?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I am trying to compare two IP address using bc command however the IP with lower numeric value is not being trapped in the test.



    Here is my code:



    TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q )


    If TEST results to 1, the process continues else exits.



    check the below example:



    ++ echo '191.35 < 187.254 && 191.35 > 184.1 '
    + TEST=0
    ++ echo '191.35 < 191.254 && 191.35 > 188.1 '
    ++ bc -q
    + TEST2=0
    ++ echo '191.35 < 195.254 && 191.35 > 192.1 '
    ++ bc -q
    + TEST3=0
    + [[ 0 = 1 ]]
    + [[ 0 = 1 ]]
    + [[ 0 = 1 ]]


    In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.



    I have tried double quoutes in the TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q ) however didnt had much success.



    Any ideas how to fix it ?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to compare two IP address using bc command however the IP with lower numeric value is not being trapped in the test.



      Here is my code:



      TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q )


      If TEST results to 1, the process continues else exits.



      check the below example:



      ++ echo '191.35 < 187.254 && 191.35 > 184.1 '
      + TEST=0
      ++ echo '191.35 < 191.254 && 191.35 > 188.1 '
      ++ bc -q
      + TEST2=0
      ++ echo '191.35 < 195.254 && 191.35 > 192.1 '
      ++ bc -q
      + TEST3=0
      + [[ 0 = 1 ]]
      + [[ 0 = 1 ]]
      + [[ 0 = 1 ]]


      In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.



      I have tried double quoutes in the TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q ) however didnt had much success.



      Any ideas how to fix it ?










      share|improve this question













      I am trying to compare two IP address using bc command however the IP with lower numeric value is not being trapped in the test.



      Here is my code:



      TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q )


      If TEST results to 1, the process continues else exits.



      check the below example:



      ++ echo '191.35 < 187.254 && 191.35 > 184.1 '
      + TEST=0
      ++ echo '191.35 < 191.254 && 191.35 > 188.1 '
      ++ bc -q
      + TEST2=0
      ++ echo '191.35 < 195.254 && 191.35 > 192.1 '
      ++ bc -q
      + TEST3=0
      + [[ 0 = 1 ]]
      + [[ 0 = 1 ]]
      + [[ 0 = 1 ]]


      In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.



      I have tried double quoutes in the TEST=$(echo "$i < $IP_BRD && $i > $IP_LOW " | bc -q ) however didnt had much success.



      Any ideas how to fix it ?







      shell-script scripting






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Atul

      3322517




      3322517




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote














          In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.




          No, it's not: 191.35 == 191.350 > 191.254. bc compares them numerically, it can't compare IPs directly. You should convert the IP to integer first. Then you can compare them using shell built-ins without resorting to external tools.



          Edit: check out this thread: IP Address Converter






          share|improve this answer










          New contributor




          divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474395%2fcompare-two-ip-address-using-bc%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote














            In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.




            No, it's not: 191.35 == 191.350 > 191.254. bc compares them numerically, it can't compare IPs directly. You should convert the IP to integer first. Then you can compare them using shell built-ins without resorting to external tools.



            Edit: check out this thread: IP Address Converter






            share|improve this answer










            New contributor




            divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





















              up vote
              0
              down vote














              In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.




              No, it's not: 191.35 == 191.350 > 191.254. bc compares them numerically, it can't compare IPs directly. You should convert the IP to integer first. Then you can compare them using shell built-ins without resorting to external tools.



              Edit: check out this thread: IP Address Converter






              share|improve this answer










              New contributor




              divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.



















                up vote
                0
                down vote










                up vote
                0
                down vote










                In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.




                No, it's not: 191.35 == 191.350 > 191.254. bc compares them numerically, it can't compare IPs directly. You should convert the IP to integer first. Then you can compare them using shell built-ins without resorting to external tools.



                Edit: check out this thread: IP Address Converter






                share|improve this answer










                New contributor




                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                In line echo '191.35 < 191.254 && 191.35 > 188.1 ' the IP 191.35 is less than 191.254 however its not being seen as it.




                No, it's not: 191.35 == 191.350 > 191.254. bc compares them numerically, it can't compare IPs directly. You should convert the IP to integer first. Then you can compare them using shell built-ins without resorting to external tools.



                Edit: check out this thread: IP Address Converter







                share|improve this answer










                New contributor




                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited 1 hour ago





















                New contributor




                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 1 hour ago









                divlamir

                11




                11




                New contributor




                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                divlamir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474395%2fcompare-two-ip-address-using-bc%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    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?