Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 将grep作为if条件传递_Shell_If Statement_Grep - Fatal编程技术网

Shell 将grep作为if条件传递

Shell 将grep作为if条件传递,shell,if-statement,grep,Shell,If Statement,Grep,你好,我正在尝试通过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_exter

你好,我正在尝试通过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

但是当我这样做时,我的输出文件是空的。我能知道为什么吗/

grep
需要扩展正则表达式的
-E
选项,并且您不应该解析
ls
的输出:

for javaFile in *.java; do
  if  grep -Eq '^[^/]{2}.*https?' "$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
我还使用
grep-Eq'^[^/]{2}.*https?“$javaFile”
regex将您的2个
grep
命令组合成一个命令。

这就是我所做的

  for javaFile in `ls -f *.java` ; do
    if  `cat $javaFile | grep -rqE '^[^/]{2}.*http'` || `cat $javaFile | grep -rqE '^[^/]{2}.*https'` || `cat $javaFile | grep -rqE '^[^/]{2}.*ftp'` ;
    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

输出结果与我预期的一样:)

将`ls-f*.java`中的javaFile替换为*.java中的javaFile。没关系,我得到了答案:$