[英]Using jq how to pass multiple values as arguments to a function?
我有一個包含內容的 json 文件 test.json:
[
{
"name": "Akshay",
"id": "234"
},
{
"name": "Amit",
"id": "28"
}
]
我有一個包含內容的 shell 腳本:
#!/bin/bash
function display
{
echo "name is $1 and id is $2"
}
cat test.json | jq '.[].name,.[].id' | while read line; do display $line; done
我希望單個項目的名稱和 ID 作為參數一起傳遞給函數顯示,但輸出是這樣的:
name is "Akshay" and id is
name is "Amit" and id is
name is "234" and id is
name is "28" and id is
實現代碼的正確方法應該是什么? PS:我特別想使用 jq 所以請根據 jq 來回答
兩個主要問題,以及一些對您當前的示例用例可能無關緊要但在處理來自不受信任來源的真實世界數據時可能很重要的其他項目:
id
之前遍歷所有name
。while
循環迭代中。while
循環時,該循環將在子 shell 中運行; 當管道退出時,子外殼也會退出,因此循環設置的任何變量都將丟失。jq
直接從test.json
讀取相比,啟動/bin/cat
的副本並讓jq
從其輸出讀取管道是愚蠢且低效的。我們可以解決所有這些問題:
name
s 和id
s,您需要更像jq '.[] | (.name, .id)'
東西jq '.[] | (.name, .id)'
jq '.[] | (.name, .id)'
while IFS= read -r name && IFS= read -r id; do ...
while IFS= read -r name && IFS= read -r id; do ...
迭代這些對。-j
參數用於jq
,然后添加顯式"\ "
正在編寫的內容的元素。 要在 bash 端讀取此 NUL 分隔的內容,您需要將-d ''
參數添加到每個read
。while read
循環移出子外殼,我們可以使用進程替換,如BashFAQ #24 中所述。jq
直接從test.json
讀取,請使用<test.json
讓 shell 將文件直接連接到 jq 的標准輸入,或者在 jq 的命令行上傳遞文件名。以對包含 JSON 編碼的 NUL 的輸入數據穩健的方式執行上述所有操作將如下所示:
#!/bin/bash
display() {
echo "name is $1 and id is $2"
}
cat >test.json <<'EOF'
[
{ "name": "Akshay", "id": "234" },
{ "name": "Amit", "id": "28" }
]
EOF
while IFS= read -r -d '' name && IFS= read -r -d '' id; do
display "$name" "$id"
done < <(jq -j '
def stripnuls: sub("\u0000"; "<NUL>");
.[] | ((.name | stripnuls), "\u0000", (.id | stripnuls), "\u0000")
' <test.json)
您可以在https://replit.com/@CharlesDuffy2/BelovedForestgreenUnits#main.sh 上看到以上運行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.