Bash 中“if”语句的“and”运算符

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

An "and" operator for an "if" statement in Bash

bashif-statement

提问by HTF

I'm trying to create a simple Bash script to check if the website is down and for some reason the "and" operator doesn't work:

我正在尝试创建一个简单的 Bash 脚本来检查网站是否关闭以及由于某种原因“and”运算符不起作用:

#!/usr/bin/env bash

WEBSITE=domain.com
SUBJECT="$WEBSITE DOWN!"
EMAILID="[email protected]"
STATUS=$(curl -sI $WEBSITE | awk '/HTTP\/1.1/ { print  }')
STRING=$(curl -s $WEBSITE | grep -o "string_to_search")
VALUE="string_to_search"

if [ $STATUS -ne 200 ] && [[ "$STRING" != "$VALUE" ]]; then
    echo "Website: $WEBSITE is down, status code: '$STATUS' - $(date)" | mail -s "$SUBJECT" $EMAILID
fi

The "-a" operator also doesn't work:

“-a”运算符也不起作用:

if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]

Could you also please advise when to use:

您能否还请建议何时使用:

  • single and double square brackets
  • parenthesis
  • 单方括号和双方括号
  • 插入语

?

?

回答by William Pursell

What you have should work, unless ${STATUS}is empty. It would probably be better to do:

你所拥有的应该工作,除非${STATUS}是空的。这样做可能会更好:

if ! [ "${STATUS}" -eq 200 ] 2> /dev/null && [ "${STRING}" != "${VALUE}" ]; then

or

或者

if [ "${STATUS}" != 200 ] && [ "${STRING}" != "${VALUE}" ]; then

It's hard to say, since you haven't shown us exactly what is going wrong with your script.

很难说,因为您还没有向我们展示您的脚本到底出了什么问题。

Personal opinion: never use [[. It suppresses important error messages and is not portable to different shells.

个人意见:从不使用[[. 它会抑制重要的错误消息,并且不能移植到不同的 shell。

回答by BrenanK

Try this:

尝试这个:

if [ $STATUS -ne 200 -a "$STRING" != "$VALUE" ]; then

回答by AnshBikram

Try this:

尝试这个:

if [ ${STATUS} -ne 100 -a "${STRING}" = "${VALUE}" ]

or

或者

if [ ${STATUS} -ne 100 ] && [ "${STRING}" = "${VALUE}" ]

回答by Jo So

Quote:

引用:

The "-a" operator also doesn't work:

if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]

“-a”运算符也不起作用:

if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]

For a more elaborate explanation: [and ]are not Bash reserved words. The ifkeyword introduces a conditional to be evaluated by a job (the conditional is true if the job's return value is 0or false otherwise).

更详细的解释:[并且]不是 Bash 保留字。该if关键字引入了要由作业评估的条件(如果作业的返回值为真,则条件为真,0否则为假)。

For trivial tests, there is the testprogram (man test).

对于琐碎的测试,有test程序 ( man test)。

As some find lines like if test -f filename; then foo bar; fi, etc. annoying, on most systems you find a program called [which is in fact only a symlink to the testprogram. When testis called as [, you have to add ]as the last positional argument.

由于有些人会发现诸如if test -f filename; then foo bar; fi等之类的行很烦人,因此在大多数系统上,您会发现一个名为的程序[,它实际上只是该test程序的符号链接。当test被称为 as 时[,您必须添加]作为最后一个位置参数。

So if test -f filenameis basically the same (in terms of processes spawned) as if [ -f filename ]. In both cases the testprogram will be started, and both processes should behave identically.

所以if test -f filename基本上与if [ -f filename ]. 在这两种情况下,test程序都将启动,并且两个进程的行为应该相同。

Here's your mistake: if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]will parse to if+ some job, the job being everything except the ifitself. The job is only a simple command(Bash speak for something which results in a single process), which means the first word ([) is the command and the rest its positional arguments. There are remaining arguments after the first ].

这是你的错误:if [ $STATUS -ne 200 ] -a [[ "$STRING" != "$VALUE" ]]将解析为if+ 一些工作,工作是除if本身之外的一切。作业只是一个简单的命令(Bash 表示导致单个进程的东西),这意味着第一个单词 ( [) 是命令,其余是位置参数。在第一个之后还有剩余的参数]

Also not, [[is indeed a Bash keyword, but in this case it's only parsed as a normal command argument, because it's not at the front of the command.

也不[[是,确实是 Bash 关键字,但在这种情况下,它仅被解析为普通命令参数,因为它不在命令的前面。