Bash:检查 json 响应并写入文件(如果字符串存在)
Bash: Check json response and write to a file if string exists
我为 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)。重试 curl XX 次,直到响应包含 $instance
(3)。一旦响应包含 $instance
就写入文件
(4)。正确完成上述操作后,设置变量 instance_info=$(cat ${json_response_file}) 。
我开始是这样的...然后卡住了...
until [[ $(/usr/bin/jq --raw-output '.server' <<< ${curl}) = $instance ]]
do
一个合理的实现可能看起来像这样:
retries=10
instance=server1
response_file=filename
# define a function, since you want to run this code multiple times
# the old version only ran curl once and reused that result
fetch() { curl -sk https://my-app-api.com; }
instance_info=
for (( retries_left=retries; retries_left > 0; retries_left-- )); do
content=$(fetch)
server=$(jq --raw-output '.server' <<<"$content")
if [[ $server = "$instance" ]]; then
# Writing isn't atomic, but renaming is; doing it this way makes sure that no
# incomplete response will ever exist in response_file. If working in a directory
# like /tmp where others users may have write, use $(mktemp) to create a tempfile with
# a random name to avoid security risk.
printf '%s\n' "$content" >"$response_file.tmp" \
&& mv "$response_file.tmp" "$response_file"
instance_info=$content
break
fi
done
[[ $instance_info ]] || { echo "ERROR: Giving up after $retries retries" >&2; }
我为 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)。重试 curl XX 次,直到响应包含 $instance
(3)。一旦响应包含 $instance
就写入文件(4)。正确完成上述操作后,设置变量 instance_info=$(cat ${json_response_file}) 。
我开始是这样的...然后卡住了...
until [[ $(/usr/bin/jq --raw-output '.server' <<< ${curl}) = $instance ]]
do
一个合理的实现可能看起来像这样:
retries=10
instance=server1
response_file=filename
# define a function, since you want to run this code multiple times
# the old version only ran curl once and reused that result
fetch() { curl -sk https://my-app-api.com; }
instance_info=
for (( retries_left=retries; retries_left > 0; retries_left-- )); do
content=$(fetch)
server=$(jq --raw-output '.server' <<<"$content")
if [[ $server = "$instance" ]]; then
# Writing isn't atomic, but renaming is; doing it this way makes sure that no
# incomplete response will ever exist in response_file. If working in a directory
# like /tmp where others users may have write, use $(mktemp) to create a tempfile with
# a random name to avoid security risk.
printf '%s\n' "$content" >"$response_file.tmp" \
&& mv "$response_file.tmp" "$response_file"
instance_info=$content
break
fi
done
[[ $instance_info ]] || { echo "ERROR: Giving up after $retries retries" >&2; }