简体   繁体   English

如何检查curl的http_code是否在200和299之间

[英]How to check if curl's http_code is between 200 and 299

I would like to test a website until it returns with http_code between 200 and 299: 我想测试一个网站,直到它返回200到299之间的http_code为止:

  until [[ $(curl --silent -o /dev/null -w %{http_code} -u admin:admin http://mywww:8080/api/v1) = "2[0-9][0-9]" ]] ;do
    printf '.'
    sleep 5
done

But it just keeps on looping even though curl returns 202 for example. 但是,即使例如curl返回202,它也会继续循环播放。 I've tried substituting = with -eq when comparing to regex but then it throws an error 与正则表达式比较时,我尝试用-eq替换=,但是会引发错误

[[: 2[0-9][0-9]: syntax error: invalid arithmetic operator (error token is "[0-9][0-9]") [[:2 [0-9] [0-9]:语法错误:无效的算术运算符(错误标记为“ [0-9] [0-9]”)

Try this: 尝试这个:

until [[ $(curl -I --silent -o /dev/null -w %{http_code} -u admin:admin http://mywww:8080/api/v1) =~ 2[0-9][0-9]  ]] ;do
    printf '.'
    sleep 5
done

-I : Do a head instead of get. -I :做个头而不是弄个头。 Much more efficent, if not blocked by the webserver. 如果没有被网络服务器阻止,效率会更高。

=~ 2[0-9][0-9] : Equal Tilde operator that allows the use of regex in an if statement. =~ 2[0-9][0-9] 〜2 =~ 2[0-9][0-9] :相等的Tilde运算符,允许在if语句中使用正则表达式。

Side note: If you dont want to use equal tilde you should lose the qoutes on the pattern. 旁注:如果您不想使用等号,则应该在模式上丢失qoutes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM