Folder_name = "D:\newfolder\xxx"
echo "enter keyword"
read string
if grep $string $Folder_name;
then
echo "yes"
else
echi "no"
fi
问问题
146 次
5 回答
1
用这个:
Folder_name=/newfolder/xxx
echo "enter keyword"
read string
if grep -q -F "$string" "$Folder_name"/*
then echo yes
else echo no
fi
- shell 变量赋值中没有空格
=
。 grep
需要文件名参数,而不是目录,除非您使用-r
递归搜索目录的选项。- 该
-q
选项告诉grep
不要打印匹配的行。 - 该
-F
选项告诉它将其$string
视为逐字字符串而不是正则表达式。 - 您应该引用变量以防它们包含空格或通配符。
于 2013-07-05T10:11:18.463 回答
1
如果您正在寻找是/否响应,您可以使用这一行命令:
grep -q $string $Folder_name/* && echo 'Yes'|| echo 'No'
于 2013-07-05T10:33:51.370 回答
1
我会说
found=false
for file in *; do
if grep -q "$string" "$file"; then
found=true
break
fi
done
if $found; then
echo "at least one file contains $string"
else
echo "no files contain $string"
fi
于 2013-07-05T10:34:01.130 回答
1
您是在寻找包含特定字符串的文件,还是在寻找包含特定字符串的文件名?
包含字符串的文件:
find . -type f -exec grep -l "string" {} \;
包含特定字符串的文件名:
find . -type f | grep "string"
于 2013-07-05T11:52:53.537 回答
1
您可以使用它来搜索
cd "$Folder_name" && count=$(grep -R -c "$string")
if [ $count -gt 0 ]; then
echo "Yes"
else
echo "No"
fi
这将递归搜索文件夹中的所有文件。
于 2013-07-08T12:37:33.953 回答