简体   繁体   English

如何使这个 bash 脚本更短更好? 有没有办法在 bash 中组合多个 curl 语句和 if 语句?

[英]How can this bash script be made shorter and better? Is there a way to combine multiple curl statements and if statements in bash?

Status_code_1=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" https://url_1.com)
Status_code_2=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" https://url_2.com)

if [ $Status_code_1  == "200" ]; then
    echo "url_1 is running successfully"
else
    echo "Error at url_1. Status code:" $Status_code_1
fi

if [ $Status_code_2 == "200" ]; then
    echo "url_2 is running successfully"
else
    echo "Error Error at url_2. Status code:" $Status_code_2
fi

The main script is scheduled and runs everyday and prints the success message everytime.主脚本每天安排并运行,每次都打印成功消息。 If the status code is anything other than 200, $Status_code_1 or $Status_code_2, whichever is down prints the error code.如果状态码不是 200、$Status_code_1 或 $Status_code_2,以向下的为准打印错误代码。

The code is working fine but I want to know how can it be made shorter.代码运行良好,但我想知道如何使它更短。 Can the curl command from first 2 lines be combined because they have same authorization and everything, it's just the url at the end is different.是否可以合并前两行中的 curl 命令,因为它们具有相同的授权和所有内容,只是最后的 url 不同。 Also later if statements are pretty much same, only that I'm running them separately for different urls.稍后,如果语句几乎相同,只是我为不同的 url 分别运行它们。

Is it possible to write first 2 lines in one line and same for both if statements?是否可以将前 2 行写在一行中,并且两个 if 语句都相同? I know AND and OR can be used for if statements but say we have 5 urls and 2 are down, how it will print the name of those 2 urls in that case?我知道 AND 和 OR 可用于 if 语句,但说我们有 5 个 url 和 2 个关闭,在这种情况下它将如何打印这 2 个 url 的名称?

Is there a way to combine multiple curl statements and if statements in bash?有没有办法在 bash 中组合多个 curl 语句和 if 语句?

curl can retrieve multiple URLs in one run, but then you're left to parse its output into per-URL pieces. curl可以在一次运行中检索多个 URL,但是您需要将其 output 解析为每个 URL 片段。 Since you want to report on the response for each URL, it is probably to your advantage to run curl separately for each.由于您要报告每个 URL 的响应,因此为每个单独运行curl可能对您有利。

But you can make the script less repetitive by writing a shell function that performs one curl run and reports on the results:但是您可以通过编写 shell function 来减少脚本的重复性,该脚本执行一次curl运行并报告结果:

test_url() {
  local status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "$1")

  if [ "$status" = 200 ]; then
    echo "$2 is running successfully"
  else
    echo "Error at $2. Status code: $status"
  fi
}

Then running it for a given URL is a one-liner:然后为给定的 URL 运行它是单行的:

test_url https://url_1.com url1_1
test_url https://url_2.com url1_2

Altogether, that's also about the same length as the original, but this is the break-even point on length.总而言之,这也与原始长度大致相同,但这是长度的收支平衡点。 Each specific URL to test requires only one line, as opposed to six in your version.每个要测试的特定 URL 只需要一行,而不是您的版本中的六行。 Also, if you want to change any of the details of the lookup or status reporting then you can do it in one place for all.此外,如果您想更改查找或状态报告的任何详细信息,那么您可以在一个地方为所有人完成。

To avoid repetition, you can encapsulate code you need to reuse in a function.为避免重复,您可以将需要重用的代码封装在 function 中。

httpresult () {
    curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "$@"
}

check200 () {
    local status=$(httpresult "$1")
    if [ "$status" = "200" ]; then
        echo "$0: ${2-$1} is running successfully" >&2
    else
        echo "$0: error at ${2-$1}. Status code: $status" >&2
    fi
}

check200 "https://url_1.com/" "url_1"
check200 "https://url_2.com/" "url_2"

Splitting httpresult to a separate function isn't really necessary, but perhaps useful both as a demonstration of a more modular design, and as something you might reuse in other scripts too.httpresult拆分为单独的 function 并不是真正必要的,但作为更模块化设计的演示以及您也可以在其他脚本中重用的东西可能很有用。

I changed the formatting of the status message to include the name of the script in the message, and to print diagnostics to standard error instead of standard output, in accordance with common best practices.根据常见的最佳实践,我更改了状态消息的格式以在消息中包含脚本的名称,并将诊断打印到标准错误而不是标准 output。

The check200 function accepts a URL and optionally a human-readable label to use in the diagnostic messages; check200 function 接受 URL 和可选的人类可读 label 以用于诊断消息; if you omit it, they will simply contain the URL, too.如果您省略它,它们也将仅包含 URL。 It wasn't clear from your question whether the labels are important and useful.从您的问题中不清楚标签是否重要和有用。

Notice that the standard comparison operator in [... ] is = , not == (though Bash will accept both).请注意[... ]中的标准比较运算符是= ,而不是== (尽管 Bash 将接受两者)。

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

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