我想在bash中检查文件是否包含特定字符串.我用过这个脚本,但它不起作用:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题?
Igo*_*bin 425
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
Run Code Online (Sandbox Code Playgroud)
你不需要[[ ]]这里.直接运行命令.-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
Run Code Online (Sandbox Code Playgroud)
您可以将命令指定为条件if.如果命令在其exitcode中返回0,则表示条件为真; 否则是假的.
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
Run Code Online (Sandbox Code Playgroud)
如你所见,你直接在这里运行程序.没有额外的[]或[[]].
Lah*_*ima 56
如果要检查文件是否不包含特定字符串,可以按如下方式进行。
if ! grep -q SomeString "$File"; then
Some Actions # SomeString was not found
fi
Run Code Online (Sandbox Code Playgroud)
Ben*_*oit 34
除了其他答案,告诉你如何做你想做的事情,我试着解释什么是错的(这是你想要的.
在Bash中,if将遵循命令.如果该命令的退出代码等于0,则then执行该else部分,否则执行该部分(如果有).
您可以使用任何命令执行此操作,如其他答案中所述: if /bin/true; then ...; fi
[[是一个内部bash命令,专门用于某些测试,如文件存在,变量比较.类似地[,外部命令(它通常位于其中/usr/bin/[)执行大致相同的测试,但需要]作为最终参数,这就是为什么]必须用左边的空格填充,而情况并非如此]].
在这里你不需要[[也不[.
另一件事是你引用事物的方式.在bash中,只有一种情况是成对的引号嵌套,它是"$(command "argument")".但是'grep 'SomeString' $File'你只有一个单词,因为它'grep '是一个引用单位,它与之连接在一起SomeString,然后再连接起来' $File'.$File由于使用单引号,变量甚至没有被其值替换.正确的方法是grep 'SomeString' "$File".
##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
Run Code Online (Sandbox Code Playgroud)
最短(正确)版本:
grep -q "something" file; [ $? -eq 0 ] && echo "yes" || echo "no"
Run Code Online (Sandbox Code Playgroud)
也可以写成
grep -q "something" file; test $? -eq 0 && echo "yes" || echo "no"
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,您无需显式测试它,因此与:
grep -q "something" file && echo "yes" || echo "no"
Run Code Online (Sandbox Code Playgroud)
小智 5
if grep -q [string] [filename]
then
[whatever action]
fi
Run Code Online (Sandbox Code Playgroud)
例子
if grep -q 'my cat is in a tree' /tmp/cat.txt
then
mkdir cat
fi
Run Code Online (Sandbox Code Playgroud)
grep -q [PATTERN] [FILE] && echo $?
Run Code Online (Sandbox Code Playgroud)
0如果找到模式,则退出状态为(true); 否则blankstring.
如果您想检查字符串是否与整行匹配,并且是否为固定字符串,则可以使用这种方法
grep -Fxq [String] [filePath]
Run Code Online (Sandbox Code Playgroud)
例
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
Run Code Online (Sandbox Code Playgroud)
从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.)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
370662 次 |
| 最近记录: |