Edit:本问题已根据背景情况进行了编辑.1.将原问题中的sh和2.sh脚本分别替换为它们的真实实现:monitor or.sh和api-check.sh.
Function:个
function checkAlive() {
script="$1"
args="${@:2}"
startCheck=$(date +%s)
echo "$@"
tempResult="$( "$@" )"
T_RC="$?"
if [ "$T_RC" -ne 0 ]; then
if [ "$result" = "" ]; then
result="ERRORS: $tempResult"
else
result="$result ,, $tempResult"
fi
fi
RC="$((RC > T_RC ? RC : T_RC))"
timePassed
}
api-check.sh:个
method="$1"
url="https://$2"
jwt="$3"
body="$4"
echo "$body"
if ! response="$(curl -s --request "$method" "$url" -w "%{http_code}" --header \
'Authorization: Bearer '"$jwt" --header 'Content-Type: application/json' --header 'Accept: application/json' --data-raw "$body")"; then
echo "$url curl error getting code: $response"
exit 2
fi
monitor.sh:个
api_url="foo"
oauth_token"bar"
body='{"address":"Test address"}'
checkAlive "api-check.sh" '"POST" "'$api_url'" "'$oauth_token'" '"'$body'"''
Execution Flow:个
- Sh将字符串json Body作为参数传递给check Alive函数(如上所定义).
- 在check Alive中,echo "$@"返回:
api-check.sh "POST" "foo" "bar" '{"address":"Test address"}'
- 由于再次出现转义问题,api-check.sh脚本永远不会正确执行
Question:个 我应该将什么json主体作为参数传递给check Alive才能正确转义OR我需要修改哪行代码以及在哪个脚本中才能正确地计算json主体?