首页 > 解决方案 > 我正在尝试 grep 文件夹并将结果变量作为进一步需要

问题描述

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

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

标签: linuxbash

解决方案


您可以避免使用管道|并在 内grep使用nameor iname(不区分大小写)find,例如:

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 (``\'').

推荐阅读