bash 如何根据 curl 的 HTTP 状态代码分支 shell 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11135236/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:32:55  来源:igfitidea点击:

How can I branch a shell script based on an HTTP status code from curl?

bashcurl

提问by nobody

I am writing a bash script for a backup. The script is going to run a curl and this is going to return a certain code.

我正在编写一个用于备份的 bash 脚本。该脚本将运行 curl,这将返回特定代码。

Depending on the result of this code:

根据此代码的结果:

  • the script must continue running (if the return code is ok, like 200) or
  • the script must return a not ok status (if the return code is not ok, like 400) and end the script without doing anything
  • 脚本必须继续运行(如果返回码正常,如 200)或
  • 脚本必须返回一个不正常的状态(如果返回代码不正常,比如 400)并结束脚本而不做任何事情

How can the return be read out from the curl ?? Simple script for most of you but ... ;-)

如何从 curl 中读出返回值?对大多数人来说都是简单的脚本,但是...... ;-)

回答by Todd A. Jacobs

The Problem

问题

The curl program is shell-friendly, which means its exit status reflects the status of curl, not the HTTP status code.

curl 程序是 shell 友好的,这意味着它的退出状态反映的是 curl 的状态,而不是 HTTP 状态代码。

The Solution

解决方案

You can make a second call to the URL for a status code, use the write-out flag to append the status code to your output, or parse the headers. Here are some examples.

您可以再次调用 URL 以获取状态代码,使用写出标志将状态代码附加到您的输出,或解析标题。这里有些例子。

The first option is naive, in that you are making two separate calls, so the status code may not be the same between calls. Nevertheless, it could be useful in some cases.

第一个选项是幼稚的,因为您正在进行两个单独的调用,因此调用之间的状态代码可能不同。尽管如此,它在某些情况下还是有用的。

# Make a second call to get the status code.
curl --verbose http://www.google.com 2>&1 |
sed -rn 's!^< HTTP/.* ([[:digit:]]+).*!!p'

The better way to do this is to append the status code to standard output, and then strip it out after you capture it. For example:

更好的方法是将状态代码附加到标准输出,然后在捕获它后将其剥离。例如:

response=$(curl --silent --write-out "\n%{http_code}\n" http://google.com)
status_code=$(echo "$response" | sed -n '$p')
html=$(echo "$response" | sed '$d')

Sample Output

样本输出

Using the example above, you can use these results any way you like. As one example, to view the HTML and the status code separately, you can do something like this:

使用上面的示例,您可以按自己喜欢的任何方式使用这些结果。例如,要分别查看 HTML 和状态代码,您可以执行以下操作:

$ echo "$html"; echo; echo "HTTP Status Code: $status_code"
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

HTTP Status Code: 301

Branching

分枝

Now that you have the status code, you can branch based on the value using an if/then or case statement. For example:

现在您有了状态代码,您可以使用 if/then 或 case 语句根据值进行分支。例如:

case "$status_code" in
    200) echo 'Success!'
         ;;
      *) echo 'Fail!'
         exit 1
         ;;
esac

Note that you'll have to set your own exit status, and that you can't just re-use the HTTP status code. A shell exit status must be between 0-255, and many HTTP status codes are outside that range.

请注意,您必须设置自己的退出状态,并且不能仅重复使用 HTTP 状态代码。shell 退出状态必须在 0-255 之间,并且许多 HTTP 状态代码在该范围之外。

See Also

也可以看看

回答by anonymous

curl -i stackoverflow.com 2>/dev/null | head -n 1 | cut -d\  -f2

回答by xda1001

I wrote a demo, it can print status code. :)

我写了一个演示,它可以打印状态码。:)

#!/bin/bash

status_line=`curl -i stackoverflow.com 2>/dev/null | head -n 1`
status_code=`echo $status_line | awk '{print }'`
echo "STATUS_CODE: ${status_code}"