这个 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
主脚本每天定时运行,每次都打印成功信息。如果状态代码不是 200、$Status_code_1 或 $Status_code_2,则打印错误代码。
代码运行良好,但我想知道如何让它更短。前两行的 curl 命令是否可以合并,因为它们具有相同的权限和所有内容,只是最后的 url 不同。稍后如果语句几乎相同,只是我 运行 它们分别用于不同的 urls.
是否可以将前两行写在一行中,并且两个 if 语句都相同?我知道 AND 和 OR 可用于 if 语句,但假设我们有 5 个 url 和 2 个已关闭,在那种情况下它将如何打印这 2 个 url 的名称?
Is there a way to combine multiple curl statements and if statements in bash?
curl
可以在一个 运行 中检索多个 URL,但是您需要将其输出解析为 per-URL 部分。由于您想报告每个 URL 的响应,因此对每个单独 运行 curl
可能对您有利。
但是您可以通过编写一个 shell 函数来减少脚本的重复性,该函数执行一个 curl
运行 并报告结果:
test_url() {
local status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "")
if [ "$status" = 200 ]; then
echo " is running successfully"
else
echo "Error at . Status code: $status"
fi
}
然后 运行对于给定的 URL 宁它是 one-liner:
test_url https://url_1.com url1_1
test_url https://url_2.com url1_2
总的来说,这也与原始长度大致相同,但这是长度上的 break-even 点。每个要测试的特定 URL 只需要一行,而不是您的版本中的六行。此外,如果您想更改查找或状态报告的任何详细信息,那么您可以在一个地方为所有更改。
为避免重复,您可以将需要重用的代码封装在一个函数中。
httpresult () {
curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "$@"
}
check200 () {
local status=$(httpresult "")
if [ "$status" = "200" ]; then
echo "[=10=]: ${2-} is running successfully" >&2
else
echo "[=10=]: error at ${2-}. Status code: $status" >&2
fi
}
check200 "https://url_1.com/" "url_1"
check200 "https://url_2.com/" "url_2"
将 httpresult
拆分为一个单独的函数并不是真正必要的,但作为更模块化设计的演示以及您也可以在其他脚本中重用的东西可能很有用。
我更改了状态消息的格式,以在消息中包含脚本的名称,并根据常见的最佳做法将诊断信息打印到标准错误而不是标准输出。
check200
函数接受 URL 和可选的 human-readable 标签以在诊断消息中使用;如果省略它,它们也将只包含 URL。从你的问题中不清楚这些标签是否重要和有用。
请注意 [ ... ]
中的标准比较运算符是 =
,而不是 ==
(尽管 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
主脚本每天定时运行,每次都打印成功信息。如果状态代码不是 200、$Status_code_1 或 $Status_code_2,则打印错误代码。
代码运行良好,但我想知道如何让它更短。前两行的 curl 命令是否可以合并,因为它们具有相同的权限和所有内容,只是最后的 url 不同。稍后如果语句几乎相同,只是我 运行 它们分别用于不同的 urls.
是否可以将前两行写在一行中,并且两个 if 语句都相同?我知道 AND 和 OR 可用于 if 语句,但假设我们有 5 个 url 和 2 个已关闭,在那种情况下它将如何打印这 2 个 url 的名称?
Is there a way to combine multiple curl statements and if statements in bash?
curl
可以在一个 运行 中检索多个 URL,但是您需要将其输出解析为 per-URL 部分。由于您想报告每个 URL 的响应,因此对每个单独 运行 curl
可能对您有利。
但是您可以通过编写一个 shell 函数来减少脚本的重复性,该函数执行一个 curl
运行 并报告结果:
test_url() {
local status=$(curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "")
if [ "$status" = 200 ]; then
echo " is running successfully"
else
echo "Error at . Status code: $status"
fi
}
然后 运行对于给定的 URL 宁它是 one-liner:
test_url https://url_1.com url1_1
test_url https://url_2.com url1_2
总的来说,这也与原始长度大致相同,但这是长度上的 break-even 点。每个要测试的特定 URL 只需要一行,而不是您的版本中的六行。此外,如果您想更改查找或状态报告的任何详细信息,那么您可以在一个地方为所有更改。
为避免重复,您可以将需要重用的代码封装在一个函数中。
httpresult () {
curl -o /dev/null -s -w "%{http_code}\n" -H "Authorization: token abc123" "$@"
}
check200 () {
local status=$(httpresult "")
if [ "$status" = "200" ]; then
echo "[=10=]: ${2-} is running successfully" >&2
else
echo "[=10=]: error at ${2-}. Status code: $status" >&2
fi
}
check200 "https://url_1.com/" "url_1"
check200 "https://url_2.com/" "url_2"
将 httpresult
拆分为一个单独的函数并不是真正必要的,但作为更模块化设计的演示以及您也可以在其他脚本中重用的东西可能很有用。
我更改了状态消息的格式,以在消息中包含脚本的名称,并根据常见的最佳做法将诊断信息打印到标准错误而不是标准输出。
check200
函数接受 URL 和可选的 human-readable 标签以在诊断消息中使用;如果省略它,它们也将只包含 URL。从你的问题中不清楚这些标签是否重要和有用。
请注意 [ ... ]
中的标准比较运算符是 =
,而不是 ==
(尽管 Bash 将接受两者)。