【问题标题】:Hide JQ Error Messages from Curl command从 Curl 命令隐藏 JQ 错误消息
【发布时间】:2017-12-04 20:25:25
【问题描述】:

我正在运行以下 curl 命令:

installer_to_delete=$(curl -s -u username:password "URL/api/search/dates?dateFields=created&from=${Two_Years_Ago}&today&repos=npm-local-lrn" | jq -r '.results[].uri'|sed 's=/api/storage==')

if [[ $installer_to_delete == "" ]]
then
    echo "No installers found"
else
    for installer in $installer_to_delete
    do
    echo $installer
    done
fi

这个错误/输出是:

assertion "cb == jq_util_input_next_input_cb" failed: file "/usr/src/ports/jq/jq-1.5-3.x86_64/src/jq-1.5/util.c", line 371, function: jq_util_input_get_position

只要 Curl 命令找不到文件,它就会显示此输出。找不到文件时如何使 JQ 错误不再弹出?

【问题讨论】:

  • 这里不需要sed -- jq 可以在内部进行字符串替换。

标签: bash curl jq


【解决方案1】:

一个(不那么热门的)选项是将 STDERR 重定向到 /dev/null:

jq  -r '.results[].uri' 2> /dev/null

由于显然存在错误的可能性,更好的选择可能是将管道分解为多个步骤,以便您可以在此过程中根据需要处理不同的错误。

顺便说一句,这个断言表明 jq 本身存在某种错误。你能告诉我们curl的相应输出吗?

【讨论】:

    【解决方案2】:

    如果curl 成功,则使用curl --fail 仅继续运行jq 命令:

    url="URL/api/search/dates?dateFields=created&from=${Two_Years_Ago}&today&repos=npm-local-lrn"
    
    if result=$(curl -s --fail  -u username:password "$url"); then
      readarray -t installers < <(jq -r '.results[].uri' <<<"$result" | sed 's=/api/storage==')
      if (( ${#installers[@]} )); then
        for installer in "${installers_to_delete[@]}"; do
          echo "$installer"
        done
      else
        echo "Empty list of installers retrieved" >&2
      fi
    else
      echo "HTTP error retrieving installers" >&2
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多