如何在 bash if/else 语句中使用文件 grep 比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2480584/
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
How do I use a file grep comparison inside a bash if/else statement?
提问by user77413
When our server comes up we need to check a file to see how the server is configured.
当我们的服务器启动时,我们需要检查一个文件以查看服务器是如何配置的。
We want to search for the following string inside our /etc/aws/hosts.conf file:
我们想在我们的 /etc/aws/hosts.conf 文件中搜索以下字符串:
MYSQL_ROLE=master
Then, we want to test whether that string exists and use an if/else statement to run one of two options depending on whether the string exists or not.
然后,我们要测试该字符串是否存在并使用 if/else 语句根据字符串是否存在运行两个选项之一。
What is the BASH syntax for the if statement?
if 语句的 BASH 语法是什么?
if [ ????? ]; then
#do one thing
else
#do another thing
fi
回答by
From grep --help
, but also see man grep:
从grep --help
,但也看到man grep:
Exit status is 0 if any line was selected, 1 otherwise; if any error occurs and -q was not given, the exit status is 2.
如果选择了任何行,退出状态为 0,否则为 1;如果发生任何错误并且未给出 -q,则退出状态为 2。
if grep --quiet MYSQL_ROLE=master /etc/aws/hosts.conf; then
echo exists
else
echo not found
fi
You may want to use a more specific regex, such as ^MYSQL_ROLE=master$
, to avoid that string in comments, names that merely start with "master", etc.
您可能希望使用更具体的正则表达式,例如^MYSQL_ROLE=master$
, 以避免在注释中使用该字符串,名称仅以“master”开头,等等。
This works because the iftakes a command and runs it, and uses the return value of that command to decide how to proceed, with zero meaning true and non-zero meaning false—the same as how other return codes are interpreted by the shell, and the opposite of a language like C.
这是有效的,因为if接受一个命令并运行它,并使用该命令的返回值来决定如何继续,零表示真,非零表示假 - 与 shell 解释其他返回码的方式相同,和像 C 这样的语言相反。
回答by Ignacio Vazquez-Abrams
if
takes a command and checks its return value. [
is just a command.
if
接受一个命令并检查它的返回值。[
只是一个命令。
if grep -q ...
then
....
else
....
fi
回答by vladr
Note that, for PIPE
being any command or sequence of commands, then:
请注意,对于PIPE
任何命令或命令序列,则:
if PIPE ; then
# do one thing if PIPE returned with zero status ($?=0)
else
# do another thing if PIPE returned with non-zero status ($?!=0), e.g. error
fi
For the record, [ expr ]
is a shell builtin?shorthand for test expr
.
作为记录,[ expr ]
是否内置了外壳?的简写test expr
。
Since grep
returns with status 0 in case of a match, and non-zero status in case of no matches, you can use:
由于grep
匹配时返回状态 0,不匹配时返回非零状态,因此您可以使用:
if grep -lq '^MYSQL_ROLE=master' ; then
# do one thing
else
# do another thing
fi
Note the use of -l
which only cares about the file having at least one match (so that grep
returns as soon as it finds one match, without needlessly continuing to parse the input file.)
请注意,使用-l
which 只关心至少有一个匹配项的文件(以便grep
在找到匹配项后立即返回,而无需继续解析输入文件。)
?on some platforms [ expr ]
is not a builtin, but an actual executable /bin/[
(whose last argument will be ]
), which is why [ expr ]
should contain blanks around the square brackets, and why it must be followed by one of the command list separators(;
, &&
, ||
, |
, &
, newline)
? 在某些平台上[ expr ]
是不是内置的,但实际的可执行文件/bin/[
(其最后一个参数会]
),这就是为什么[ expr ]
应该包含空格前后的方括号,以及为什么它必须遵循的命令列表分隔符的一个(;
,&&
,||
,|
,&
,新队)
回答by ghostdog74
just use bash
只需使用 bash
while read -r line
do
case "$line" in
*MYSQL_ROLE=master*)
echo "do your stuff";;
*) echo "doesn't exist";;
esac
done <"/etc/aws/hosts.conf"