英文:
shell code to check if a website up or down. but keep gives an error
问题
以下是代码的错误信息的翻译:
- 第5行: 遇到了预期之外的 EOF(文件结尾),在查找匹配的
''
时。 - 第10行: 预期条件二进制操作符。
- 第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 "$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>
# 答案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 "$site_url"|tail -2|head -1|cut -d , -f2 | cut -d ' ' -f2
> 0 ]]; then
First issue; opening quote of in front of ping.
I think it should be a capital W option for ping
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论