如何编写用于检查网站实时的shell脚本

Akk*_*kki 4 bash shell if-statement

我正在编写一个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;
Run Code Online (Sandbox Code Playgroud)

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

./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 ]] '
Run Code Online (Sandbox Code Playgroud)

请纠正我......

fed*_*qui 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) ]
Run Code Online (Sandbox Code Playgroud)

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

if [ $(command) ]
Run Code Online (Sandbox Code Playgroud)

你正在使用的时候

if [ command]
Run Code Online (Sandbox Code Playgroud)

还要注意周围空间的需要[ ]: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;
Run Code Online (Sandbox Code Playgroud)

那是,

if command && command
Run Code Online (Sandbox Code Playgroud)