比较 wget 结果时出现此错误:
mpa=$(wget http://xxxxxxxx/api/pages/ZZZZ-YYYY -q -O -)
echo $mpa
if (($mpa = "{"'"'name'"'":"'"'ZZZZ-YYYY'"','"'active'"':true}"" )); then
echo "CRITICAL: Up."
else
echo "OK: Down."
fi
出现的错误:
./mpa: line 25: ((: {"name":"ZZZZ-YYYY","active":true} = {"name":"ZZZZ-YYYY","active":true} : syntax error: operand expected (error token is "{"name":"ZZZZ-YYYY","active":true} = {"name":"ZZZZ-YYYY","active":true} ")
OK: Down
不确定错误在哪里;
当我尝试回显时:
mpa="{"'"'name'"'":"'"'ZZZZ-YYYY'"','"'active'"':true}""
结果很好:
echo $mpa
{"name":"ZZZZ-YYYY","active":true}
但是当在 if 语句中比较它时,它显示了这个错误。
答案1
你的if
陈述是一个语法错误。执行字符串比较的正确方法是:
if [[ "testcase" == "$variable" ]]; then
do_stuff
else
do_other_stuff
fi