我试图grep文件夹并为进一步的需要创建结果的变量

时间:2018-05-23 11:54:36

标签: linux bash

GET_DIR=$ (find ${FIND_ROOT} -type -d 2>/dev/null | grep -Eiv ${EX_PATTERN| grep -Eio ${FIND_PATTERN}) 

但不知怎的,当我尝试打印结果时,它是空的。 但是当我在没有脚本的情况下使用我的grep时,我在命令行上得到了结果。

3 个答案:

答案 0 :(得分:1)

您可以在|中使用grepname(不区分大小写)来避免管道inamefind,例如:

find /tmp -type d -iname "*foo*"

这将找到与-type d

中忽略案例*foo*的模式-iname匹配的目录/tmp

要将输出保存在变量中,您可以使用:

FOO=$(find /tmp -type d -iname "*foo*") 

来自find男人:

 -name pattern
         True if the last component of the pathname being examined matches pattern.  Special shell pattern matching
         characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern.  These characters may be matched
         explicitly by escaping them with a backslash (``\'').

答案 1 :(得分:0)

考虑使用xargs:

GET_DIR = $(找$ {FIND_ROOT} -type -d 2> / dev / null | xargs grep -Eiv $ {EX_PATTERN | grep -Eio $ {FIND_PATTERN})

答案 2 :(得分:-1)

GET_DIR=$(find ${FIND_ROOT} -type -d 2>/dev/null | grep -Eiv ${EX_PATTERN| grep -Eio ${FIND_PATTERN})