在下面的函式中,我使用 JQ 來決議 JSON 資料并獲取httpOperations鍵/值對。
#!/usr/bin/env bash
function batch_post() {
SETTINGS_INPUT=$1 #! Variable that holds the argument value
n=0
:> "${SETTINGS_INPUT}"_req.tmp #! This will just create an empty file
while read setting
do
# Get parameters that need to go in the batch put/post request
url=$(echo $setting | ./jq -r '.url')
body=$(echo $setting | ./jq -r '.response.body')
# Get the request method
method=$(cat settings.json | ./jq --arg URL ${url} '.settings[] | select(.pathPattern==$URL|tostring).httpOperations[] | select(.method == "PUT" or .method == "POST").method')
# Save individual requests to be added to the batch request
let n=n 1
done <<< $(cat ${SETTINGS_INPUT} | ./jq -c '.')
# Format individual requests in the expected batch json format
cat "${SETTINGS_INPUT}"_req.tmp | ./jq -n '.requests |= [inputs]' > "${SETTINGS_LIST}"_batch_post_put_req
# TODO: Validate this has the correct format and PUT/POST according to the settings
cat "${SETTINGS_LIST}"_batch_post_put_req
#! DO NOT execute API call with put/post request
}
我將一個 JSON 檔案作為引數傳遞給這個函式,下面是 JSON 檔案的內容。請注意,這不是我實際使用的完整 JSON 資料。
{
"settings" : [ {
"key" : "AccountingRules",
"description" : "Accounting Rules settings",
"context" : "Entity",
"pathPattern" : "/accounting-rules",
"httpOperations" : [ {
"method" : "GET",
"url" : "/settings/accounting-rules",
"parameters" : [ ],
"responseType" : {
"$ref" : "#/definitions/AccountingRules",
"definitions" : {
"AccountingRules" : {
"additionalProperties" : false,
"type" : "object",
"properties" : {
"allowRevenueScheduleNegativeAmounts" : {
"type" : "boolean"
},
"allowBlankAccountingCodes" : {
"type" : "boolean"
},
"allowCreationInClosedPeriod" : {
"type" : "boolean"
},
"allowUsageInClosedPeriod" : {
"type" : "boolean"
},
"differentCurrencies" : {
"type" : "boolean"
}
}
}
}
}
}, {
"method" : "PUT",
"url" : "/settings/accounting-rules",
"parameters" : [ ],
"requestType" : {
"$ref" : "#/definitions/AccountingRules",
"definitions" : {
"AccountingRules" : {
"additionalProperties" : false,
"type" : "object",
"properties" : {
"allowRevenueScheduleNegativeAmounts" : {
"type" : "boolean"
},
"allowBlankAccountingCodes" : {
"type" : "boolean"
},
"allowCreationInClosedPeriod" : {
"type" : "boolean"
},
"allowUsageInClosedPeriod" : {
"type" : "boolean"
},
"differentCurrencies" : {
"type" : "boolean"
}
}
}
}
},
"responseType" : {
"$ref" : "#/definitions/AccountingRules",
"definitions" : {
"AccountingRules" : {
"additionalProperties" : false,
"type" : "object",
"properties" : {
"allowRevenueScheduleNegativeAmounts" : {
"type" : "boolean"
},
"allowBlankAccountingCodes" : {
"type" : "boolean"
},
"allowCreationInClosedPeriod" : {
"type" : "boolean"
},
"allowUsageInClosedPeriod" : {
"type" : "boolean"
},
"differentCurrencies" : {
"type" : "boolean"
}
}
}
}
}
} ]
}, {
"key" : "AgingBuckets",
"description" : "Aging Buckets",
"context" : "Entity",
"pathPattern" : "/aging-buckets",
"httpOperations" : [ {
"method" : "GET",
"url" : "/settings/aging-buckets",
"parameters" : [ ],
"responseType" : {
"$ref" : "#/definitions/AgingBucket",
"definitions" : {
"Bucket" : {
"additionalProperties" : false,
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"maxLength" : 100
},
"fromDaysPastDue" : {
"type" : "integer"
},
"toDaysPastDue" : {
"type" : "integer"
}
},
"required" : [ "name" ]
},
"AgingBucket" : {
"additionalProperties" : false,
"type" : "object",
"properties" : {
"includeNegativeInvoice" : {
"type" : "boolean"
},
"buckets" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/Bucket"
}
}
}
}
}
}
} ]
}
]}
每當我顯示名為“method”的變數的輸出時,回傳的輸出都會堆疊在一起。舉個例子,當顯示 $method 值時,問題看起來像這樣。
Display method below
"PUT"
"POST"
"PUT"
"POST"
"PUT"
"POST"
"PUT"
"POST"
"PUT"
"POST"
"PUT"
"POST"
"PUT"
"PUT"
"POST"
"PUT"
"POST"
"POST"
"PUT"
"POST"
Done displaying method
Wherein I'm expecting the output to be just a single "PUT" or "POST" only. Also, the thing I observed that's kind of weird for me is that this issue will show up when I try to display my variable executing "$ echo ${method}" command but when I remove the curly braces it sometimes works fine.
uj5u.com熱心網友回復:
我設法通過將 JQ 過濾器中的單引號替換為雙引號來使其作業。
# Get the request method
method=$(cat settings.json | ./jq ".settings[] | select(.pathPattern==$URL|tostring).httpOperations[] | select(.method == \"PUT\" or .method == \"POST\").method")
我不需要使用--arg選項來定義 JQ 變數,而是我直接呼叫了我的 bash 變數,幸運的是它運行良好。
uj5u.com熱心網友回復:
select(.pathPattern==$URL|tostring)
這不太可能是您想要的。管道|運算子的優先級低于相等==運算子。此過濾器等效于:
select((.pathPattern==$URL)|tostring)
...這將作為以下任一項作業:
select(true|tostring)
select(false|tostring)
......這將是:
select("true")
select("false")
...也許你可以看到這是怎么回事。所有字串都被視為類真值。所以這些過濾器實際上每次都會選擇所有設定!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327051.html
標籤:bash variables while-loop jq
