linux shell 代码来检查是否一个网站向上或向下,但保持给出一个错误

fslejnso  于 2023-02-11  发布在  Linux
关注(0)|答案(3)|浏览(137)

什么是错误的代码,我一直得到一个错误运行它.这是错误:* * www.example.com:**第5行:查找匹配'' updown.sh时出现意外的EOF:updown.sh第10行:语法错误:意外的文件结尾line 10: syntax error: unexpected end of file
代码:

#!/bin/sh
echo "Enter the site:"
read site_url
if [[ 'ping -w 100 -c 1 "$site_url"|tail -2|head
-1|cut -d , -f2 | cut -d ' ' -f2` > 0 ]]; then
        echo "site is up"
else
        echo "site is down"
fi

updown.sh第10行:条件二元运算符预期为updown.sh:第10行:语法错误:意外的文件结束 unexpected end of file

llew8vvj

llew8vvj1#

您可以使用以下代码:

#!/bin/sh
echo "Enter the site:"
read -r site_url
if [ "$(ping -w 100 -c 1 "$site_url" | tail -2| head -1 |cut -d , -f2 | cut -d ' ' -f2)" -gt 0 ]
then
        echo "site is up"
else
        echo "site is down"
fi
aemubtdh

aemubtdh2#

我觉得有点太紧张了。不如

if ping -w 100 -c 1 "$site_url" # is ping really what you want here?

或者可能(作为一个例子,根据需要详细说明,在这里动态分配...)

curl -sI "$site_url" |
  awk '/^HTTP/{
       switch(int($2/100)) {
         case 1: msg="Informational"             ; break;
         case 2: msg="Site is up"                ; break;
         case 3: msg="Redirect"                  ; break;
         case 4: print "Client Error " $2        ; exit 4;
         case 5: print "Server Error " $2        ; exit 5;
         default: print "Unexpected error - " $0 ; exit 6;
       }
       print msg " ("$2")"; exit;
   } END { print "Site error, no code"           ; exit 7; }'

其中,使用site_url=https://google.com可得到

Redirect (301)

手动传递失败:

echo "HTTP/2 444" | awk '/^HTTP/{ switch(int($2/100)) {
  case 1: msg="Informational" ; break;
  case 2: msg="Site is up"    ; break;
  case 3: msg="Redirect"      ; break;
  case 4: print "Client Error " $2 ; exit 4;
  case 5: print "Server Error " $2 ; exit 5;
  default: print "Unexpected error - " $0; exit 6;
  } print msg " ("$2")"; exit; } END { print "Site error, no code"; exit 7; }'
Client Error 444

如果出于某种原因,您仍然需要它作为if测试的一部分-

if curl -sI ${site_url:=https://google.com} |
     awk '/^HTTP/{
       switch(int($2/100)) {
         case 1: msg="Informational" ; break;
         case 2: msg="Site is up"    ; break;
         case 3: msg="Redirect"      ; break;
         case 4: print "Client Error " $2 ; exit 4;
         case 5: print "Server Error " $2 ; exit 5;
         default: print "Unexpected error - " $0; exit 6;
       }
       print msg " ("$2")"; exit;
     } END { print "Site error, no code"; exit 7; }'
then echo doing some other stuff
else echo doing error stuff
fi

它给出了

Redirect (301)
doing some other stuff

或者

if curl -sI some.broken.site | awk '/^HTTP/{ switch(int($2/100)) {
   case 1: msg="Informational" ; break;
   case 2: msg="Site is up"    ; break;
   case 3: msg="Redirect"      ; break;
   case 4: print "Client Error " $2 ; exit 4;
   case 5: print "Server Error " $2 ; exit 5;
   default: print "Unexpected error - " $0; exit 6;
   } print msg " ("$2")"; exit; } END { print "Site error, no code"; exit 7; }';
then echo doing some other stuff; else echo doing error stuff; fi

Site error, no code
doing error stuff
huwehgph

huwehgph3#

替换为此行

if [[ `ping -W 100 -c 1 "$site_url"|tail -2|head -1|cut -d , -f2 | cut -d ' ' -f2` > 0 ]]; then

第一期;在ping前面的开始引号。我认为它应该是ping的大写W选项

相关问题