我有一個名為“input.json”的 json 檔案:
{
"item1": "banana",
"item2": false
}
我有一個 bash 命令,它接受一個 json 輸入,然后接受上述檔案內容的哈希作為“輸入”鍵的值:
executable '{"input": {"item1": "banana", "item2": false}, "option1": "value1", "option2": "value2" }'
如何將 input.json 檔案中的 json 內容傳遞到命令中?我試過了:
- 從 input.json 創建單行 json 哈希
jq -c . input.json并將哈希傳遞給變數,然后在命令中參考該變數 - 它不起作用(缺少預期值 - 可能是因為單引號弄亂了參考)。
任何幫助或指標表示贊賞。
uj5u.com熱心網友回復:
您可以按照您指示的步驟進行操作,這應該可以。創建 base64 內容,將其存盤在變數中并將其傳遞給jq
#!/usr/bin/env bash
# Bash script to construct a base64 of a JSON content and passing
# it to an another command
# Create the base64
base=$(jq -c . input.json | base64)
# Create the JSON that stores the above hash
json=$(jq -cn --arg b64 "$base" '{"input": $b64, "option1": "value1", "option2": "value2" }')
# If you want the JSON to be a literal string, use the '@text' filter
# with 'jq'
jsonString=$(jq -cn --arg b64 "$base" '{"input": $b64, "option1": "value1", "option2": "value2" } | @text')
# Pass the contents of either 'json' or 'jsonString', depending on
# how your executable parses the command line argument
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462968.html
上一篇:繪制影像出現偏移
