String 检查是否可以在文件外壳中找到包含字符串的变量

String 检查是否可以在文件外壳中找到包含字符串的变量,string,file,shell,variables,String,File,Shell,Variables,这似乎不起作用。有什么建议吗?您可以这样做: if grep -Fxq $name1 test.txt 这就足够了: match=`grep $name1 test.txt` if [ -n "$match" ]; then echo found fi 你可以试试这个: grep $name1 test.txt $name1和test.txt中有什么内容? grep -Fxq $name1 test.txt if [ $? -eq 0 ]; then ... test.

这似乎不起作用。有什么建议吗?

您可以这样做:

if grep -Fxq $name1 test.txt
这就足够了:

match=`grep $name1 test.txt`
if [ -n "$match" ]; then
   echo found
fi
你可以试试这个:

 grep $name1 test.txt

$name1和test.txt中有什么内容?
grep -Fxq $name1 test.txt  
if [ $? -eq 0 ]; then
    ... test.txt contains $name1 ...
else
    ... test.txt does not contain $name1 ...
fi