shell code to check if a website up or down. but keep gives an error

huangapple go评论58阅读模式
英文:

shell code to check if a website up or down. but keep gives an error

问题

以下是代码的错误信息的翻译:

  1. 第5行: 遇到了预期之外的 EOF(文件结尾),在查找匹配的 '' 时。
  2. 第10行: 预期条件二进制操作符。
  3. 第10行: 语法错误,意外的文件结尾。
英文:

what's wrong with code, i keep getting an error running it.
this is the error:
updown.sh: line 5: unexpected EOF while looking for matching `''
updown.sh: line 10: conditional binary operator expected
updown.sh: line 10: syntax error: unexpected end of file

Code:

#!/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: line 5: unexpected EOF while looking for matching `''
updown.sh: line 10: conditional binary operator expected
updown.sh: line 10: syntax error: unexpected end of file

答案1

得分: 1

你可以使用以下的代码:

#!/bin/sh
echo "输入网站地址:"
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 "网站正常"
else
        echo "网站不可用"
fi
英文:

You can use the following code:

#!/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

答案2

得分: 1

以下是代码部分的翻译:

Seems overwrought to me. How about just

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

or maybe (as an example to elaborate as needed, assigning on the fly 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; }'

Which, using `site_url=https://google.com`, gives

    Redirect (301)

Manually passing a fail:

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 for some reason you just need it to still be part of an `if` test -

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


which gives

Redirect (301)
doing some other stuff


Or

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


for

Site error, no code
doing error stuff


<details>
<summary>英文:</summary>

Seems overwrought to me. How about just 

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

or maybe (as an example to elaborate as needed, assigning on the fly here...)

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

Which, using `site_url=https://google.com`, gives

    Redirect (301)

Manually passing a fail:

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 for some reason you just need it to still be part of an `if` test -

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

which gives

Redirect (301)
doing some other stuff


Or

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

for

Site error, no code
doing error stuff


</details>



# 答案3
**得分**: -1

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


<details>
<summary>英文:</summary>

Replace with this line

if [[ ping -W 100 -c 1 &quot;$site_url&quot;|tail -2|head -1|cut -d , -f2 | cut -d &#39; &#39; -f2 > 0 ]]; then

First issue; opening quote of in front of ping.
I think it should be a capital W option for ping

</details>



huangapple
  • 本文由 发表于 2023年2月10日 11:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定