我想在Bash中检查文件是否包含特定的字符串。我使用了这个脚本,但它不起作用:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
我的代码有什么问题?
我想在Bash中检查文件是否包含特定的字符串。我使用了这个脚本,但它不起作用:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
我的代码有什么问题?
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
在这里你不需要使用 [[ ]]
。直接运行命令即可。当你不需要显示找到的字符串时,添加 -q
选项。
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
你可以将命令作为 if
语句的条件。如果该命令在退出代码中返回0,则表示条件为真;否则为假。$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
正如你所看到的,你可以在这里直接运行程序。无需额外使用[]
或[[]]
。
-Fxq
参数,例如 https://dev59.com/eW445IYBdhLWcg3wq8FY#4749368。 - Grzegorz Wierzowiecki-m 1
以提高扫描性能。返回第一次出现的结果。 - AlikElzin-kilakaSomeString
包含正则表达式特殊字符(如.
),则可能会得到意外的结果。最安全的方法是始终使用fgrep
(或grep -F
)(除非您真的需要正则表达式,在这种情况下,egrep
(或grep -E
)可能是最好的选择)。 - Walter Trossif
的例子是不正确的,因为它只检查退出代码是否为非0。如果发生任何错误,比如文件无法读取,退出代码也是非0的。所以你必须做类似于 ec=$?
这样的事情,并检查它是否为 0
(找到),然后如果它是 1
(未找到),然后再检查它是否为其他值(失败)。 - ddekany!
和一个空格:if ! grep -q SomeString ...
- Cameron Tacklind如果您想检查文件是否不包含特定字符串,可以按照以下步骤进行。
if ! grep -q SomeString "$File"; then
Some Actions # SomeString was not found
fi
cat
(和xargs
)结合起来?或者你需要一个for
循环? - qräbnögrep -v
也可以否定条件。 - Tom Harrison最短(正确)版本:
grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"
可以写成以下形式
grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"
但在这种情况下您不需要显式测试它,所以可以使用相同的方法:
grep -q "something" file && echo "yes" || echo "no"
$?
进行任何更改。 - Charles Duffy##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
grep -q [PATTERN] [FILE] && echo $?
如果找到了该模式,则退出状态为 0
(真); 否则为空字符串。
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
如果您想检查字符串是否与整行匹配并且它是一个固定的字符串,可以这样做:
grep -Fxq [String] [filePath]
例子
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
来自 man 手册:
-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.)
试试这个:
if [[ $(grep "SomeString" $File) ]] ; then
echo "Found"
else
echo "Not Found"
fi
grep
之所以返回一个退出状态,是有非常好的理由的;将输出捕获到一个字符串中可能会在缓冲区中存储大量文本,只是为了说明它不为空。 - tripleee我完成了这个,看起来运行良好。
if grep $SearchTerm $FileToSearch; then
echo "$SearchTerm found OK"
else
echo "$SearchTerm not found"
fi
grep
的输出。[/questions/10067266/when-to-wrap-quotes-around-a-shell-variable] - tripleee