Home > front end >  I want to compare a value to the result of an expression instead of executing the result of the expr
I want to compare a value to the result of an expression instead of executing the result of the expr

Time:01-11

I'm trying to make a comparison between the result of an expression and a number but it's trying to execute something instead and it gives an error. I want to break from the while loop if the popularity of the first package is less than the popularity variable.

This is something similar to the json I get from get_packages:

curl -sX "GET" -H "accept: application/json" "https://pkgstats.archlinux.de/api/packages?limit=2&offset=0" | jq "."
{
  "total": 65871,
  "count": 2,
  "limit": 2,
  "offset": 0,
  "query": "",
  "packagePopularities": [
    {
      "name": "attr",
      "samples": 18330,
      "count": 18330,
      "popularity": 100,
      "startMonth": 202112,
      "endMonth": 202112
    },
    {
      "name": "acl",
      "samples": 18330,
      "count": 18330,
      "popularity": 100,
      "startMonth": 202112,
      "endMonth": 202112
    }
  ]
}

offset=0
limit=1000
total=65871
res=()
pkgs=$(get_packages $popularity $limit $offset)

# Get 'limit' number of packages each time
while [ $offset -le $total ]; do
    pkgs=$(get_packages $popularity $limit $offset)
    res =$pkgs
    if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then
      break
    fi
    offset=$(($offset $limit))
done

The error I'm getting is:

./get_popular_packages.sh: line 63: "gnutls": command not found
./get_popular_packages.sh: line 63: [: -lt: unary operator expected

Which is the line for if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then

CodePudding user response:

The errors seem to be about this line:

    if [ $($pkgs | jq ".packagePopularity[0].popularity") -lt $popularity ]; then

The $pkgs | jq ".packagePopularity[0].popularity" within your command substitution says

  • expand $pkgs (variable expansion, and since the variable is unquoted, the result will be subject to several additional expansions, including word splitting); then
  • run the result as a command and pipe the output into the specified jq command.

But it looks like you actually want to send the value of $pkgs itself to jq, not the result of executing that as a command.

With Bash, the easiest way to modify your code to do that would be to replace the above line with

    if [ $(jq ".packagePopularity[0].popularity" <<<"$pkgs") -lt $popularity ]; then

The <<< introduces a redirection from a "here string", which is a bash-specific feature. It would be more portable (but much harder to read) to use a standard heredoc instead:

    if [ $(jq ".packagePopularity[0].popularity" <<EOF
$pkgs
EOF
          ) -lt $popularity ]; then

Note also that you have numerous potential issues with (lack of) quoting. There are probably other issues, too. Shellcheck will do a better job of identifying all of those than I am prepared to try to do manually.

  •  Tags:  
  • Related