bash 我如何编写用于检查网站是否在线的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17522360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i write a shell script for checking website live or not
提问by Akki
I am writing a shell script to monitor website live or not and send an email alert below is my code
我正在编写一个 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="[email protected]"
curl --data "body=$msg &to=$email &subject=$msg" https://opx.com/email/send
fi;
if i run this code i got
如果我运行此代码,我会得到
./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 ]] '
Please correct me...
请纠正我...
回答by fedorqui 'SO stop harming'
Change it do this:
改变它这样做:
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) ]
To check the status of a command inside an if, you have to do it like
要检查 内命令的状态if,您必须这样做
if [ $(command) ]
while you were using
当你使用
if [ command]
note also the need of spaces around [ ]: if [_space_ command _space_ ]
还要注意周围需要空格[ ]:if [_space_ command _space_ ]
Update
更新
Based on Ansgar Wiechers's comment, you can also use the following:
根据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;
That is,
那是,
if command && command

