4

我正在编写一个 shell 脚本来监控网站是否实时并在下面发送电子邮件警报是我的代码

#!/bin/bash
if [[ curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]]
then echo "The HTTP server on opx.com and oss.com is up!" #> /dev/null
else
msg="The HTTP server  opx.com Or oss.com is down "
email="opx-noc@opx.com"

curl --data "body=$msg &to=$email &subject=$msg" https://opx.com/email/send
fi;

如果我运行这段代码,我得到了

./Monitoring_Opx_Oss: line 2: conditional binary operator expected
./Monitoring_Opx_Oss: line 2: syntax error near `-s'
./Monitoring_Opx_Oss: line 2: `if [[ curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]] '

请纠正我...

4

1 回答 1

7

改变它这样做:

if [ $(curl -s --head  --request GET http://opx.opera.com/opx/version | grep "200 OK" > /dev/null) ] && [ $(curl -s --head --request GET http://oss.opera.com/version | grep "200 OK" > /dev/null) ]

要检查 中的命令的状态if,您必须这样做

if [ $(command) ]

当你使用

if [ command]

还要注意周围需要空格[ ]if [_space_ command _space_ ]

更新

根据Ansgar Wiechers 的评论,您还可以使用以下内容:

if curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null && curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null;

那是,

if command && command
于 2013-07-08T08:48:02.330 回答