我爲json響應蜷縮一個端點並將響應寫入文件。 到目前爲止,我已經得到了一個腳本:Bash:如果字符串存在,請檢查json響應並寫入文件
1)。如果該文件不存在,則執行卷曲操作並且2)。否則設置變量
#!/bin/bash
instance="server1"
curl=$(curl -sk https://my-app-api.com | python -m json.tool)
json_response_file="/tmp/file"
if [ ! -f ${json_response_file} ] ; then
${curl} > ${json_response_file}
instance_info=$(cat ${json_response_file})
else
instance_info=$(cat ${json_response_file})
fi
問題是,該文件可能存在響應錯誤或爲空。 可能使用bash直到,我想
(1)。檢查(使用JQ)curl響應中的字段包含$ instance,然後才寫入該文件。
(2)。重試卷曲XX次,直到響應包含$實例
(3)。寫入文件一旦響應包含$實例
(4)。當上述操作正確完成時,設置變量instance_info = $(cat $ {json_response_file})。
我開始喜歡這個......然後就死......
until [[ $(/usr/bin/jq --raw-output '.server' <<< ${curl}) = $instance ]]
do
做你的條件無法工作,或者這是一個問題搞清楚如何實現邏輯? –
爲什麼你需要'python -m json.tool'當你有'jq'? –
它試圖找出邏輯的問題。我真的不需要python -m json.tool。 – Narrabit10