BASH comparing strings/integers error

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











up vote
0
down vote

favorite












I'm working on this script to check pricing.
It gets to line 16 and shoots an error.



#!/bin/bash

echo " Date Time Price"
echo "-----------------------------"
while [ true ]
do

new_price=$(curl -s "https://coinbase.com/api/v1/prices/spot_rate" | jq -r ".amount")

balance=0.000001

if [ -z $current_price ]; then
current_price=0
fi

if [ $current_price < $new_price ]; then
ARROW="+"
else if [ $current_price > $new_price ]; then
ARROW="-"
else
ARROW="="
fi
fi

echo "$(date '+%m/%d/%Y | %H:%M:%S') | $new_price | $ARROW"

current_price=$new_price
sleep 30
done


Outputs error



-PC:~/scripts/ticker$ ./arrows.sh
Date Time Price
-----------------------------
./arrows.sh: line 16: 7685.00: No such file or directory
11/17/2017 | 15:45:28 | 7685.00 | -


And this is the verbose



-PC:~/scripts/ticker$ bash -x ./arrows.sh
+ echo ' Date Time Price'
Date Time Price
+ echo -----------------------------
-----------------------------
+ '[' true ']'
++ curl -s https://coinbase.com/api/v1/prices/spot_rate
++ jq -r .amount
+ new_price=7685.00
+ balance=0.000001
+ '[' -z ']'
+ current_price=0
+ '[' 0 ']'
+ ARROW=+
++ date '+%m/%d/%Y | %H:%M:%S'


Please help!







share|improve this question
























    up vote
    0
    down vote

    favorite












    I'm working on this script to check pricing.
    It gets to line 16 and shoots an error.



    #!/bin/bash

    echo " Date Time Price"
    echo "-----------------------------"
    while [ true ]
    do

    new_price=$(curl -s "https://coinbase.com/api/v1/prices/spot_rate" | jq -r ".amount")

    balance=0.000001

    if [ -z $current_price ]; then
    current_price=0
    fi

    if [ $current_price < $new_price ]; then
    ARROW="+"
    else if [ $current_price > $new_price ]; then
    ARROW="-"
    else
    ARROW="="
    fi
    fi

    echo "$(date '+%m/%d/%Y | %H:%M:%S') | $new_price | $ARROW"

    current_price=$new_price
    sleep 30
    done


    Outputs error



    -PC:~/scripts/ticker$ ./arrows.sh
    Date Time Price
    -----------------------------
    ./arrows.sh: line 16: 7685.00: No such file or directory
    11/17/2017 | 15:45:28 | 7685.00 | -


    And this is the verbose



    -PC:~/scripts/ticker$ bash -x ./arrows.sh
    + echo ' Date Time Price'
    Date Time Price
    + echo -----------------------------
    -----------------------------
    + '[' true ']'
    ++ curl -s https://coinbase.com/api/v1/prices/spot_rate
    ++ jq -r .amount
    + new_price=7685.00
    + balance=0.000001
    + '[' -z ']'
    + current_price=0
    + '[' 0 ']'
    + ARROW=+
    ++ date '+%m/%d/%Y | %H:%M:%S'


    Please help!







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm working on this script to check pricing.
      It gets to line 16 and shoots an error.



      #!/bin/bash

      echo " Date Time Price"
      echo "-----------------------------"
      while [ true ]
      do

      new_price=$(curl -s "https://coinbase.com/api/v1/prices/spot_rate" | jq -r ".amount")

      balance=0.000001

      if [ -z $current_price ]; then
      current_price=0
      fi

      if [ $current_price < $new_price ]; then
      ARROW="+"
      else if [ $current_price > $new_price ]; then
      ARROW="-"
      else
      ARROW="="
      fi
      fi

      echo "$(date '+%m/%d/%Y | %H:%M:%S') | $new_price | $ARROW"

      current_price=$new_price
      sleep 30
      done


      Outputs error



      -PC:~/scripts/ticker$ ./arrows.sh
      Date Time Price
      -----------------------------
      ./arrows.sh: line 16: 7685.00: No such file or directory
      11/17/2017 | 15:45:28 | 7685.00 | -


      And this is the verbose



      -PC:~/scripts/ticker$ bash -x ./arrows.sh
      + echo ' Date Time Price'
      Date Time Price
      + echo -----------------------------
      -----------------------------
      + '[' true ']'
      ++ curl -s https://coinbase.com/api/v1/prices/spot_rate
      ++ jq -r .amount
      + new_price=7685.00
      + balance=0.000001
      + '[' -z ']'
      + current_price=0
      + '[' 0 ']'
      + ARROW=+
      ++ date '+%m/%d/%Y | %H:%M:%S'


      Please help!







      share|improve this question












      I'm working on this script to check pricing.
      It gets to line 16 and shoots an error.



      #!/bin/bash

      echo " Date Time Price"
      echo "-----------------------------"
      while [ true ]
      do

      new_price=$(curl -s "https://coinbase.com/api/v1/prices/spot_rate" | jq -r ".amount")

      balance=0.000001

      if [ -z $current_price ]; then
      current_price=0
      fi

      if [ $current_price < $new_price ]; then
      ARROW="+"
      else if [ $current_price > $new_price ]; then
      ARROW="-"
      else
      ARROW="="
      fi
      fi

      echo "$(date '+%m/%d/%Y | %H:%M:%S') | $new_price | $ARROW"

      current_price=$new_price
      sleep 30
      done


      Outputs error



      -PC:~/scripts/ticker$ ./arrows.sh
      Date Time Price
      -----------------------------
      ./arrows.sh: line 16: 7685.00: No such file or directory
      11/17/2017 | 15:45:28 | 7685.00 | -


      And this is the verbose



      -PC:~/scripts/ticker$ bash -x ./arrows.sh
      + echo ' Date Time Price'
      Date Time Price
      + echo -----------------------------
      -----------------------------
      + '[' true ']'
      ++ curl -s https://coinbase.com/api/v1/prices/spot_rate
      ++ jq -r .amount
      + new_price=7685.00
      + balance=0.000001
      + '[' -z ']'
      + current_price=0
      + '[' 0 ']'
      + ARROW=+
      ++ date '+%m/%d/%Y | %H:%M:%S'


      Please help!









      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '17 at 21:51









      b1sr4t

      11




      11




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Replace:



          if [ $current_price < $new_price ]; then
          ARROW="+"
          else if [ $current_price > $new_price ]; then
          ARROW="-"
          else
          ARROW="="
          fi
          fi


          With:



          if echo "$current_price < $new_price" | bc | grep -q 1; then
          ARROW="+"
          elif echo "$current_price > $new_price" | bc | grep -q 1; then
          ARROW="-"
          else
          ARROW="="
          fi


          In test ([), the operator for numeric less-than comparison is -lt not <. (This is because < is for input redirection.) Thus, if your prices were integers, you could use:



          if [ "$current_price" -lt "$new_price" ]; then


          But, your prices are floating point so we need bc or equivalent to do the math. bc prints 1 if a logical condition is true and 0 if it is false. We follow that with grep -q 1 which sets a proper return code that if can use.



          Also, note that bash supports elif which provides simpler syntax than else if. Lastly, many users think that indentation makes bash commands like if-then-else-fi easier to read.






          share|improve this answer





























            up vote
            1
            down vote













            I hate if's, so, if I can avoid them:



            arrows=('=' '<' '>')
            ARROW=$ bc)]


            In slow-motion: c=$current_price;n=$new_price;(c<n)+2*(c>n) returns 0, 1 or 2, if the values are equal, smaller, or bigger. This is used to index the array of symbols.






            share|improve this answer




















              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%2f405359%2fbash-comparing-strings-integers-error%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote













              Replace:



              if [ $current_price < $new_price ]; then
              ARROW="+"
              else if [ $current_price > $new_price ]; then
              ARROW="-"
              else
              ARROW="="
              fi
              fi


              With:



              if echo "$current_price < $new_price" | bc | grep -q 1; then
              ARROW="+"
              elif echo "$current_price > $new_price" | bc | grep -q 1; then
              ARROW="-"
              else
              ARROW="="
              fi


              In test ([), the operator for numeric less-than comparison is -lt not <. (This is because < is for input redirection.) Thus, if your prices were integers, you could use:



              if [ "$current_price" -lt "$new_price" ]; then


              But, your prices are floating point so we need bc or equivalent to do the math. bc prints 1 if a logical condition is true and 0 if it is false. We follow that with grep -q 1 which sets a proper return code that if can use.



              Also, note that bash supports elif which provides simpler syntax than else if. Lastly, many users think that indentation makes bash commands like if-then-else-fi easier to read.






              share|improve this answer


























                up vote
                4
                down vote













                Replace:



                if [ $current_price < $new_price ]; then
                ARROW="+"
                else if [ $current_price > $new_price ]; then
                ARROW="-"
                else
                ARROW="="
                fi
                fi


                With:



                if echo "$current_price < $new_price" | bc | grep -q 1; then
                ARROW="+"
                elif echo "$current_price > $new_price" | bc | grep -q 1; then
                ARROW="-"
                else
                ARROW="="
                fi


                In test ([), the operator for numeric less-than comparison is -lt not <. (This is because < is for input redirection.) Thus, if your prices were integers, you could use:



                if [ "$current_price" -lt "$new_price" ]; then


                But, your prices are floating point so we need bc or equivalent to do the math. bc prints 1 if a logical condition is true and 0 if it is false. We follow that with grep -q 1 which sets a proper return code that if can use.



                Also, note that bash supports elif which provides simpler syntax than else if. Lastly, many users think that indentation makes bash commands like if-then-else-fi easier to read.






                share|improve this answer
























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  Replace:



                  if [ $current_price < $new_price ]; then
                  ARROW="+"
                  else if [ $current_price > $new_price ]; then
                  ARROW="-"
                  else
                  ARROW="="
                  fi
                  fi


                  With:



                  if echo "$current_price < $new_price" | bc | grep -q 1; then
                  ARROW="+"
                  elif echo "$current_price > $new_price" | bc | grep -q 1; then
                  ARROW="-"
                  else
                  ARROW="="
                  fi


                  In test ([), the operator for numeric less-than comparison is -lt not <. (This is because < is for input redirection.) Thus, if your prices were integers, you could use:



                  if [ "$current_price" -lt "$new_price" ]; then


                  But, your prices are floating point so we need bc or equivalent to do the math. bc prints 1 if a logical condition is true and 0 if it is false. We follow that with grep -q 1 which sets a proper return code that if can use.



                  Also, note that bash supports elif which provides simpler syntax than else if. Lastly, many users think that indentation makes bash commands like if-then-else-fi easier to read.






                  share|improve this answer














                  Replace:



                  if [ $current_price < $new_price ]; then
                  ARROW="+"
                  else if [ $current_price > $new_price ]; then
                  ARROW="-"
                  else
                  ARROW="="
                  fi
                  fi


                  With:



                  if echo "$current_price < $new_price" | bc | grep -q 1; then
                  ARROW="+"
                  elif echo "$current_price > $new_price" | bc | grep -q 1; then
                  ARROW="-"
                  else
                  ARROW="="
                  fi


                  In test ([), the operator for numeric less-than comparison is -lt not <. (This is because < is for input redirection.) Thus, if your prices were integers, you could use:



                  if [ "$current_price" -lt "$new_price" ]; then


                  But, your prices are floating point so we need bc or equivalent to do the math. bc prints 1 if a logical condition is true and 0 if it is false. We follow that with grep -q 1 which sets a proper return code that if can use.



                  Also, note that bash supports elif which provides simpler syntax than else if. Lastly, many users think that indentation makes bash commands like if-then-else-fi easier to read.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 17 '17 at 22:13

























                  answered Nov 17 '17 at 22:07









                  John1024

                  44.2k4100117




                  44.2k4100117






















                      up vote
                      1
                      down vote













                      I hate if's, so, if I can avoid them:



                      arrows=('=' '<' '>')
                      ARROW=$ bc)]


                      In slow-motion: c=$current_price;n=$new_price;(c<n)+2*(c>n) returns 0, 1 or 2, if the values are equal, smaller, or bigger. This is used to index the array of symbols.






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        I hate if's, so, if I can avoid them:



                        arrows=('=' '<' '>')
                        ARROW=$ bc)]


                        In slow-motion: c=$current_price;n=$new_price;(c<n)+2*(c>n) returns 0, 1 or 2, if the values are equal, smaller, or bigger. This is used to index the array of symbols.






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          I hate if's, so, if I can avoid them:



                          arrows=('=' '<' '>')
                          ARROW=$ bc)]


                          In slow-motion: c=$current_price;n=$new_price;(c<n)+2*(c>n) returns 0, 1 or 2, if the values are equal, smaller, or bigger. This is used to index the array of symbols.






                          share|improve this answer












                          I hate if's, so, if I can avoid them:



                          arrows=('=' '<' '>')
                          ARROW=$ bc)]


                          In slow-motion: c=$current_price;n=$new_price;(c<n)+2*(c>n) returns 0, 1 or 2, if the values are equal, smaller, or bigger. This is used to index the array of symbols.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 18 '17 at 0:13









                          xenoid

                          1,7051620




                          1,7051620



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405359%2fbash-comparing-strings-integers-error%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?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay