0

I need some advice.. i am writing a BASH script, to check for information in a JSON file. But i need check for value which are greater than a number entered into by the user.

So if the user enters in 5.6, (has to be decimal), then i will check for anything greater than or equal to this value.

"Buzz", 4.5
"Trashy Blonde", 4.1
"Berliner Weisse With Yuzu - B-Sides", 4.2
"Pilsen Lager", 6.3
"Avery Brown Dredge", 7.2
"Electric India", 5.2

So it should show

"Pilsen Lager", 6.3
"Avery Brown Dredge", 7.2

I am very close.. But need some advice on the expression, please.

                read -p "Enter the ABV value greater to display: "  ABV_Value
                curl 'https://s3-eu-west-1.amazonaws.com/kg-it/devopsTest/api-punkapi-com-v2-beers.json' | jq  '.[] | .name,.abv' | {
                      while read name;
                        do
                                read abv
                                #if [ $abv -ge 5.1 ]; then
                                if [ $abv -ge $ABV_Value ]; then
                                     echo "$name, $abv"
                                fi
                        done
                        }

Currently i am getting - [: 4.5: integer expression expected

Any advice will be appericated.

Junes786
  • 13
  • 7
  • If you use [Xidel](http://videlibri.sourceforge.net/xidel.html), then there's no need for curl, jq, or even a bash-script! `xidel -s "https://s3-eu-west-1.amazonaws.com/kg-it/devopsTest/api-punkapi-com-v2-beers.json" -e '"Enter the ABV value greater to display: "' -e 'let $a:=read() return $json()[abv >= $a]/join((""""||name||"""",abv),", ")'` – Reino Oct 11 '19 at 13:05

1 Answers1

0

Bash does not support flating-point arithmetic. But jq does.

read -p "Enter the ABV value greater to display: " -r ABV_Value
curl 'https://s3-eu-west-1.amazonaws.com/kg-it/devopsTest/api-punkapi-com-v2-beers.json' |
jq -r --arg lim "$ABV_Value" '.[] | select(.abv>=($lim|tonumber)) |
  "\"" + .name + "\", " + (.abv|tostring)'
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks...i used the first recommendation and worked if (( $(echo "$abv < $ABV_Value_Less" | bc -l ) )); then – Junes786 Oct 10 '19 at 11:54
  • That's horribly convoluted compared to using the built-in facilities of `jq` which you are using for the JSON manipulation anyway. `while read` is pathologically slow and somewhat brittle (especially if you forgot to use `read -r` to disable legacy oddities in `read` behavior). – tripleee Oct 10 '19 at 12:07