Shell-Script
從 HTTP 回復返回的 JSON 中提取 UUID 值
我正在嘗試在 shellscript 中製作一個小腳本,該腳本從響應中獲取一個 serviceUuid 並使用它。我只是在名為 final.txt 的文件上列印伺服器響應現在我需要提取“serviceUuid”之後的值:”
這是腳本:
uuid=$(curl -X POST -H "ACCEPT-LANGUAGE:en" -H "Content-Type: application/json" -H "Accept: application/json" -d {"username":"HereThereIsTheUsername"} Here there is the url ) echo $uuid >> final.txt
這是回應:
{"status":{"code":"STATUS_OK","message":"ServiceUUID sent successfully via..."},"body":{"data":{"userApps":{},"username":"HereTheUsername","fullName":"NameOfTheAccountPossessor","lang":"sq","blocked":false,"lastLogin":"2016-10-10T17:19:22","passwordResetUuid":"6147dc32-b72e-450a-8084-2fdb5319a931","userAccessLevel":5,"countDownSeconds":0,"serviceUuid":"7260276c-5c3f-41d3-9329-3603acecb7e5","userAttributes":{},"labelMap":{},"id":"APPUSER00000012","someLabel":"NameOfTheOrganisation"}}}
那麼有人可以幫我提取價值嗎?
現在我需要在“serviceUuid”之後提取值
因此,如果變數
$uuid
包含以下內容:echo "$uuid" {"status":{"code":"STATUS_OK","message":"ServiceUUID sent successfully via..."},"body":{"data":{"userApps":{},"username":"HereTheUsername","fullName":"NameOfTheAccountPossessor","lang":"sq","blocked":false,"lastLogin":"2016-10-10T17:19:22","passwordResetUuid":"6147dc32-b72e-450a-8084-2fdb5319a931","userAccessLevel":5,"countDownSeconds":0,"serviceUuid":"7260276c-5c3f-41d3-9329-3603acecb7e5","userAttributes":{},"labelMap":{},"id":"APPUSER00000012","someLabel":"NameOfTheOrganisation"}}}
…而您只想要
serviceUuid
價值,我會這樣做:echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p' 7260276c-5c3f-41d3-9329-3603acecb7e5
在您的情況下,它看起來像:
echo "$uuid" | sed -nE 's/.*"serviceUuid":"(.*)","user.*/\1/p' >> final.txt
…將其附加到文件中
final.txt
sed --version sed (GNU sed) 4.2.2
jq是一個 JSON 解析工具。你可以這樣做:
uuid=$(curl ...) service_uuid=$(jq -r '.body.data.serviceUuid' <<<"$uuid") echo "$service_uuid"
7260276c-5c3f-41d3-9329-3603acecb7e5