【发布时间】:2022-10-25 00:08:05
【问题描述】:
在 shell 中比较 2 个响应代码时遇到问题。运行 curl 并需要验证响应是否在 200 到 400 之间。此外,当服务器关闭时,响应可能为“000”。
#!/bin/sh
response1="200" #curl first url
response2="000" #curl second url
if (( $response1 -ge 400 || $response1 -lt 200 || $response2 -ge 400 || $response2 -lt 200 )) ; then
echo "Something went wrong, response code is not in success range"
exit 1
else
echo "Success"
exit 0
fi
((: 200 -ge 400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200 :表达式中的语法错误(错误标记是“400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200")
如果我将括号更改为 [[...]],它总是返回 true。 如果我将 -lt 更改为 < 并将 -ge 更改为 >= ,则会出现以下错误:
((: 200 = 400 || 200 < 200 || 000 = 400 || 000 < 200 :尝试分配给非变量(错误标记是“= 400 || 200 < 200 || 000 = 400 || 000 < 200 ")
【问题讨论】:
-
将
((更改为[[(和]])。 -
((...))中使用的运算符记录在 Shell Arithmetic。[[...]]中使用的运算符记录在 Bash Conditional Expressions -
你如何运行你的脚本?根据#!线,它不是重击脚本。