bash If/Else curl 命令不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35074066/
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
If/Else curl command not working
提问by rubberfishstudios
I am using a grep -c to count the occurrence of a phrase on a curl. Currently the code below return a number 12.
我正在使用 grep -c 来计算 curl 上短语的出现次数。目前,下面的代码返回一个数字 12。
curl WEBSITEURL | grep -c "incident-title"
I want to use this in a single line if/else bash command that says that if the occurrence is greater than 0 then printf or else printf.
我想在单行 if/else bash 命令中使用它,该命令表示如果出现次数大于 0,则使用 printf 或其他 printf。
if curl WEBSITEURL | grep -c "incident-title" > 0; then printf "Investigating Issue"; else printf "Fully Operational"; fi
It keeps returning "Fully Operational" even though that it should be true as 12 is greater than 0.
它一直返回“完全可操作”,即使它应该是真的,因为 12 大于 0。
Thank you in advance for your assistance.
预先感谢您的帮助。
采纳答案by shellter
As you seem in need of a "one-liner", here is the same idea, but embedded inside of an if/else
block.
正如您似乎需要“单行”一样,这是相同的想法,但嵌入在if/else
块内。
if (( $( curl $WEBSITEURL | grep -c "incident-title" ) > 0 )) ; then printf "Investigating Issue"; else printf "Fully Operational"; fi
IHTH
IHTH
回答by chepner
Don't count the number of lines output by grep
; just check that its exit status is 0, which indicates at least one successful match.
不要计算grep
;输出的行数;只需检查其退出状态是否为 0,这表示至少匹配成功。
if curl WEBSITEURL | grep -q "incident-title"; then
printf "Investigating Issue"
else
printf "Fully Operational"
fi
The -q
suppresses standard output, because you don't care what the match is, only that there is a match.
在-q
抑制标准输出,因为你不关心对手是什么,只是有一个匹配。
回答by choroba
>
means output redirection in the shell. If you want to compare numbers in bash, use the arithmetic expansion (and command substitution to capture the output):
>
表示 shell 中的输出重定向。如果要在 bash 中比较数字,请使用算术扩展(和命令替换来捕获输出):
if (( $(curl WEBSITEURL | grep -c "incident-title") > 0 )) ; then
I'd use a variable for readability if it's not a one-off thing
如果它不是一次性的,我会使用一个变量来提高可读性
n=$(curl WEBSITEURL | grep -c "incident-title")
if (( n > 0 )) ; then
printf %s 'Investigating Issue'
else
printf %s 'Fully Operational'
fi
回答by alvits
In addition to what the other answers provided, here's a suggestion. Try using short-circuit logical operators &&
and ||
. You may shorten your code to:
除了其他答案提供的内容之外,这里还有一个建议。尝试使用短路逻辑运算符&&
和||
。您可以将代码缩短为:
(( $(curl WEBSITEURL | grep -c "incident-title") > 0 )) && printf 'Investigating Issue' || printf 'Fully Operational'
This however is harder to read compared to multiple lines of if...else...fi
.
然而,与多行if...else...fi
.
回答by rubberfishstudios
Thank you everyone for assisting. I truly appreciate it. I was looking for a one-liner and shellter provide the one-liner that resolved my issue.
谢谢大家的协助。我真的很感激。我一直在寻找一种单衬纸,而 shellter 提供的单衬纸可以解决我的问题。