如何使用 Bash 检查文件是否包含特定字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11287861/
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 to check if a file contains a specific string using Bash
提问by Hakim
I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:
我想在 bash 中检查文件是否包含特定字符串。我使用了这个脚本,但它不起作用:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What's wrong in my code?
我的代码有什么问题?
回答by Igor Chubin
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
You don't need [[ ]]
here. Just run the command directly. Add -q
option when you don't need the string displayed when it was found.
你不需要[[ ]]
这里。直接运行命令即可。-q
当您不需要在找到时显示的字符串时添加选项。
The grep
command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.
该grep
命令根据搜索结果在退出代码中返回 0 或 1。0 如果找到了东西;1 否则。
$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0
You can specify commands as an condition of if
. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.
您可以将命令指定为 的条件if
。如果该命令在其退出代码中返回 0,则表示条件为真;否则为假。
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
As you can see you run here the programs directly. No additional []
or [[]]
.
如您所见,您可以直接在此处运行程序。没有额外的[]
或[[]]
。
回答by Benoit
In addition to other answers, which told you how to do what you wanted, I try to explain what was wrong (which is what you wanted.
除了告诉您如何做您想做的其他答案之外,我还尝试解释出了什么问题(这就是您想要的。
In Bash, if
is to be followed with a command. If the exit code of this command is equal to 0, then the then
part is executed, else the else
part if any is executed.
在 Bash 中,if
后面是一个命令。如果此命令的退出代码等于 0,则then
执行该else
部分,否则执行该部分(如果有)。
You can do that with any command as explained in other answers: if /bin/true; then ...; fi
您可以使用其他答案中所述的任何命令执行此操作: if /bin/true; then ...; fi
[[
is an internal bash command dedicated to some tests, like file existence, variable comparisons. Similarly [
is an external command (it is located typically in /usr/bin/[
) that performs roughly the same tests but needs ]
as a final argument, which is why ]
must be padded with a space on the left, which is not the case with ]]
.
[[
是专用于某些测试的内部 bash 命令,例如文件存在、变量比较。类似的[
还有一个外部命令(它通常位于/usr/bin/[
),它执行大致相同的测试,但需要]
作为最后一个参数,这就是为什么]
必须在左侧填充一个空格,而]]
.
Here you needn't [[
nor [
.
在这里你不需要[[
也不需要[
。
Another thing is the way you quote things. In bash, there is only one case where pairs of quotes do nest, it is "$(command "argument")"
. But in 'grep 'SomeString' $File'
you have only one word, because 'grep '
is a quoted unit, which is concatenated with SomeString
and then again concatenated with ' $File'
. The variable $File
is not even replaced with its value because of the use of single quotes. The proper way to do that is grep 'SomeString' "$File"
.
另一件事是你引用事物的方式。在 bash 中,只有一种情况会嵌套引号对,它是"$(command "argument")"
. 但是 in'grep 'SomeString' $File'
你只有一个词,因为'grep '
是一个带引号的单元,它与 连接,SomeString
然后再与 连接' $File'
。$File
由于使用了单引号,该变量甚至不会被其值替换。这样做的正确方法是grep 'SomeString' "$File"
。
回答by Lahiru Chandima
In case if you want to check whether file does notcontain a specific string, you can do it as follows.
如果要检查文件是否不包含特定字符串,可以按如下方式进行。
if ! grep -q SomeString "$File"; then
Some Actions # SomeString was not found
fi
回答by Nevin Raj Victor
##To check for a particular string in a file
cd PATH_TO_YOUR_DIRECTORY #Changing directory to your working directory
File=YOUR_FILENAME
if grep -q STRING_YOU_ARE_CHECKING_FOR "$File"; ##note the space after the string you are searching for
then
echo "Hooray!!It's available"
else
echo "Oops!!Not available"
fi
回答by lzap
Shortest (correct) version:
最短(正确)版本:
grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"
can be also written as
也可以写成
grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"
but you dont need to explicitly test it in this case, so the same with:
但是在这种情况下您不需要显式测试它,因此与以下内容相同:
grep -q "something" file && echo "yes" || echo "no"
回答by SupermanKelly
grep -q [PATTERN] [FILE] && echo $?
The exit status is 0
(true) if the pattern was found; otherwise blankstring.
0
如果找到模式,则退出状态为(true);否则为空字符串。
回答by Shubham Khatri
In case you want to checkif the string matches the whole line and if it is a fixed string, You can do it this way
如果您想检查字符串是否与整行匹配并且它是固定字符串,您可以这样做
grep -Fxq [String] [filePath]
example
例子
searchString="Hello World"
file="./test.log"
if grep -Fxq "$searchString" $file
then
echo "String found in $file"
else
echo "String not found in $file"
fi
From the man file:
从人文件:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of
which is to be matched.
(-F is specified by POSIX.)
-x, --line-regexp
Select only those matches that exactly match the whole line. (-x is specified by
POSIX.)
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately with zero
status if any match is
found, even if an error was detected. Also see the -s or --no-messages
option. (-q is specified by
POSIX.)
回答by user5854766
if grep -q [string] [filename]
then
[whatever action]
fi
Example
例子
if grep -q 'my cat is in a tree' /tmp/cat.txt
then
mkdir cat
fi
回答by vgokul129
Try this:
尝试这个:
if [[ $(grep "SomeString" $File) ]] ; then
echo "Found"
else
echo "Not Found"
fi
回答by madarekrap
I done this, seems to work fine
我这样做了,似乎工作正常
if grep $SearchTerm $FileToSearch; then
echo "$SearchTerm found OK"
else
echo "$SearchTerm not found"
fi