我正在编写一个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 ]] '
请纠正我......
答案 0 :(得分:6)
更改它:
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's comment,您还可以使用以下内容:
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