這是我的 script.sh:
emails=$(curl -X GET http://0.0.0.0:5000/v1/user/all | jq 'map(.[].email)')
echo $emails
for email in ${emails[@]}
do
echo $email
FROM=$(date "%Y-%m-%d %T")
TO=$(date -d "$today -1 month" " %Y-%m-%d %T")
SEND=true
JSON_STRING='{"email":"'"$email"'","from":"'"$FROM"'","to":"'"$TO"'","send":"'"$SEND"'"}'
curl -X POST http://0.0.0.0:5000/v1/invoice/print -d JSON_STRING
done
我從第一個 API 呼叫中獲得的電子郵件資料如下:
["[email protected]", "[email protected]", "[email protected]"]
API POST curl 執行此操作:
{"email":"[","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
{"email":"]","from":"2022-03-28 09:59:07","to":"2022-02-28 09:59:07","send":"true"}
{"message": "Internal Server Error"}
編輯:腳本回顯回傳:
["[email protected]", "[email protected]", "[email protected]"]
[
{"email":"[","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
"[email protected]",
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
"[email protected]",
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
"[email protected]",
{"email":""[email protected]",","from":"2022-03-28 09:59:06","to":"2022-02-28 09:59:06","send":"true"}
{"message": "Internal Server Error"}
]
{"email":"]","from":"2022-03-28 09:59:07","to":"2022-02-28 09:59:07","send":"true"}
{"message": "Internal Server Error"}
我不明白為什么電子郵件回圈處理括號 [ 和 ] 并回傳“[email protected]”,而不僅僅是“[email protected]”?我究竟做錯了什么?
uj5u.com熱心網友回復:
這是您從中獲得的輸出jq:
[
"[email protected]",
"[email protected]",
"[email protected]",
]
bash-wise,它不是一個陣列,它是一個多行字串。
對它進行索引不會[@]改變任何內容并回傳整個字串。迭代它for var in $multiline_string執行字串的每個空格分隔標記的代碼。由于您的電子郵件不包含空格,因此這是字串的每一行。
您需要jq生成一個以空格分隔的電子郵件地址串列,不帶引號,以便 bash 輕松處理它們。只需將您的 jq 命令更改為以下內容:
jq -r 'map(.[].email)[]'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451378.html
