0

pretty simple script but I am having issues with it. It will not compare the 2 variables, is this due to floating points or? I tried to use the | bc but still not working...

    #!/bin/bash

    x=$(curl -o /dev/null -s -w %{time_total}\\n  http://www.google.com) | bc
    y=.5 | bc

    if [[ $x -gt $y ]]; then 
        echo “fast”
    else
        echo “not as fast”
    fi

updated code to: #!/bin/bash

    x=$(curl -o /dev/null -s -w %{time_total}\\n  http://www.google.com)
    y=.5

    if (( $(bc <<<'$x > $y') )); then
        echo “fast”
    else
        echo “not as fast”
    fi

Receiving errors: (standard_in) 1: illegal character: $ (standard_in) 1: illegal character: $ “not as fast”

OblongMedulla
  • 1,471
  • 9
  • 21
  • By the way: please don't get into the habit of changing your question after it has been answered. That's just confusing for people who come by later. If the answer works for you, accept it; if not, add a comment (as you did) or ignore it. – rici Oct 13 '15 at 20:02

1 Answers1

1

cmd | bc means "redirect the output of cmd into the utility bc. It is not an obscure shell syntax for declaring numbers.

For example,

y=.5 | bc

executes the command y=.5 (which sets a local variable named y to the string .5), which produces no output, and then feeds that into bc. Since bc receives no input, it produces no output. Moreover, the variable y disappears when the left-hand command terminates.

Similarly,

x=$(curl ...) | bc

sets a local variable named x to the output of the curl command (using command substitution syntax). Again, the assignment produces no output, bc receives no input and thus does nothing, and the x variable disappears.

If you remove |bc from both assignments, then you will at least have managed to set x and y. You could then use bc to compare the floating point values:

if (( $(bc <<<"$x > $y") )); then

Here the (( ... )) conditional computation is being used to test whether the numerical expression inside it is non-zero.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks @rici for the in depth answer. However I am still seeing errors on my end: (standard_in) 1: illegal character: $ (standard_in) 1: illegal character: $ “not as fast” – OblongMedulla Oct 13 '15 at 19:57
  • @user3460432: Oops. I put the wrong quotes in the here-string; with single quotes no variable substitution occurs. Fixed, sorry. – rici Oct 13 '15 at 19:59
  • using this: if (( $(bc <<<“$x > $y”) )); then -I am still seeing errors: (standard_in) 1: illegal character: ^" (standard_in) 1: illegal character: ^? (standard_in) 1: illegal character: ^? “not as fast” – OblongMedulla Oct 13 '15 at 20:02
  • @user3460432: don't use a word processor to edit script files. You have a typographic quote, not a `"` at the beginning. Look closely. – rici Oct 13 '15 at 20:03
  • I got it, it was the wrong "" texteditor changing them to the slanted ones – OblongMedulla Oct 13 '15 at 20:03
  • Thank you sir, no more errors, however I cant make it say fast by adjusting the y variable? – OblongMedulla Oct 13 '15 at 20:04