【发布时间】:2014-07-31 07:14:37
【问题描述】:
您好,我正在尝试将 grep 作为 if 条件传递。也就是说,如果 grep 匹配表达式,那么我将打印文件名和文件的行号。到目前为止我有这个:
for javaFile in `ls -f *.java` ; do
if grep -q '^[^/]{2}.*https' $javaFile || grep -q '^[^/]{2}.*http' $javaFile ;
then
echo "The file $javaFile has : " >> ~/Desktop/$1_externalServers.txt
grep -nrE '^[^/]{2}.*http' $javaFile >> ~/Desktop/$1_externalServers.txt
grep -nrE '^[^/]{2}.*https' $javaFile >> ~/Desktop/$1_externalServers.txt
grep -nrE '^[^/]{2}.*ftp' $javaFile >> ~/Desktop/$1_externalServers.txt
fi
done
但是当我这样做时,我的输出文件是空白的。我能知道为什么吗? :/
【问题讨论】:
-
将
for javaFile in `ls -f *.java`替换为for javaFile in *.java。 -
没关系,得到我的答案:$
标签: shell if-statement grep