bash 运算符 != 在 shell 脚本中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20075811/
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
What does the operator != mean in a shell script?
提问by user3009489
In this i have an exclamation mark in my second if statement why is this used.
在此,我在第二个 if 语句中使用感叹号。
#!/bin/bash
name=
if [ "$name" = "" ]
then echo -n "Enter a name to search for: "
read name
fi
grep -i cheryl ~uli101/uli101/phonebook
grep -i $name ~uli101/uli101/phonebook
if [ "$?" != "0" ]
then echo -n "Name '$name' not in directory "
fi
回答by Mark Reed
The special shell parameter $?
contains the exit code from the last command run. Every command you run from the shell reports a numeric status back to the shell when it finishes running; in general, a value of 0 means the command succeeded, and a nonzero value means it failed.
特殊的 shell 参数$?
包含上次命令运行的退出代码。从 shell 运行的每个命令都会在 shell 运行结束时向 shell 报告一个数字状态;通常,值为 0 表示命令成功,非零值表示失败。
The grep
command searches a file for lines matching a pattern. If it finds any matching lines, it prints them out, but it also exits with status 0 if it found at least one match, and a nonzero status if it didn't find any.
该grep
命令在文件中搜索与模式匹配的行。如果找到任何匹配的行,它会将它们打印出来,但如果找到至少一个匹配项,它也会以状态 0 退出,如果没有找到任何匹配项,则以非零状态退出。
The syntax [
expression]
is a command that evaluates the given expression (usually a comparison of some sort) to see if it's true or not. Really, it's just another shell command, that exits with status 0 if the expression is true and 1 if it's false; the if
construct in the shell decides what to do based on the value of $?
.
语法[
表达式]
是一个命令,它评估给定的表达式(通常是某种比较)以查看它是否为真。真的,这只是另一个 shell 命令,如果表达式为真,则以状态 0 退出,如果为假,则以状态 1 退出;if
shell 中的构造根据 的值决定要做什么$?
。
And the !=
operator means 'is not equal to', so [ $? != 0 ]
is checking to see if $? is not equal to zero.
并且!=
运算符的意思是“不等于”,所以[ $? != 0 ]
要检查 $? 不等于零。
Putting all that together, the above code checks to see if the grep
found a match or not.
将所有这些放在一起,上面的代码会检查是否grep
找到了匹配项。
The origin of !=
is the C family of programming languages, in which the exclamation point generally means "not". In bash, a !
at the start of a command will invert the exit status of the command, turning nonzero values to zero and zeroes to one. So you could also "move the exclamation point" and rewrite the above expression like this:
起源!=
于C族编程语言,其中感叹号一般表示“不是”。在 bash 中,!
命令开头的 a 将反转命令的退出状态,将非零值变为零,将零变为一。所以你也可以“移动感叹号”并像这样重写上面的表达式:
if ! [ $? == 0 ]
However, since if
itself operates based on exit status, all of the above code is doing extra work. You can skip the middleman and just test grep
directly:
但是,由于if
其本身基于退出状态进行操作,因此上述所有代码都在做额外的工作。你可以跳过中间人grep
直接测试:
if ! grep -i "$name" ~uli101/uli101/phonebook; then
echo "Name '$name' not in directory."
fi
Note that I put double quotes around $name
, which prevents any spaces in the value from separating it into multiple arguments to grep
.
请注意,我在 周围放置了双引号$name
,这可以防止值中的任何空格将其分隔为grep
.