【发布时间】:2019-01-02 23:24:22
【问题描述】:
local response=response.json
local status=$(curl -s -w %{http_code} "http://www.google.com" -o $response)
local name=$(cat $response | jq -r .name)
local address=$(cat $response | jq -r .address)
if [ $status = 200 ] && [ $name = "John" ]; then
# Process response
else
# Error
fi
我正在使用上述方法处理更多请求。在上述情况下,所有请求都失败
如果 URL 无效,则 response.json 不可用。当我解析 $response 2 次时,我收到错误如下 2 次p>
response.json not found, No such file or directory
response.json not found, No such file or directory
如何使用 curl 和 jq 以更好的方式处理这些类型的错误?
【问题讨论】:
-
为什么不直接测试是否创建了response.json? (我会先 mv response.json 如果它存在,以便人们可以更轻松地检查它是否已创建。)
-
如何以及在哪里检查?
-
请阅读 bash,尤其是
if和test。