Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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
Regex 如何使用文件动态指定模式,并根据该目录中的模式获取文件?_Regex_Linux_Bash_Shell - Fatal编程技术网

Regex 如何使用文件动态指定模式,并根据该目录中的模式获取文件?

Regex 如何使用文件动态指定模式,并根据该目录中的模式获取文件?,regex,linux,bash,shell,Regex,Linux,Bash,Shell,我是shell脚本的新手。如果我错了,请纠正 我有一个名为ignorefileslist的文件,其中包含文件名和模式,如/trunk/*表示忽略trunk中的所有文件 我能够获得保存此模式的文件行,但无法找到如何执行这些模式以获得与该模式匹配的文件。例如/trunk/*模式的/trunk文件夹中的文件 ignorefileslist.txt: /.settings /.project /branches/staging/test.php /trunk/index.php /trunk/*.php

我是shell脚本的新手。如果我错了,请纠正

我有一个名为
ignorefileslist
的文件,其中包含文件名和模式,如/trunk/*表示忽略trunk中的所有文件

我能够获得保存此模式的文件行,但无法找到如何执行这些模式以获得与该模式匹配的文件。例如/trunk/*模式的/trunk文件夹中的文件

ignorefileslist.txt:

/.settings
/.project
/branches/staging/test.php
/trunk/index.php
/trunk/*.php
/trunk/*
这是我的密码:

if grep -q "*" "$HOME/$REPONAME/ignorablefileslist.txt";then

   filewithpattern=`cat $HOME/$REPONAME/ignorablefileslist.txt | grep "*" `

   echo "It contains pattern $filewithpattern"    
fi
输出:

It contains pattern /trunk/*.php /trunk/*

如何同时使用/trunk/*和/trunk/*.php正则表达式来获取相应的文件

如下更改grep命令以打印包含
/trunk/*
/trunk/*.php
字符串的文件名

grep -lP '/trunk/\*(\.php)?' *.*

如果出于某种原因需要保持条件语句的完整性,那么可以使用循环和进程替换来避免子shell

if grep -q "*" ignorefileslist.txt; then while read filewithpattern; do echo "It contains pattern $filewithpattern"; done < <( grep "*" ignorefileslist.txt ); fi
如果grep-q“*”ignorefileslist.txt;然后在读取文件时使用模式;执行echo“它包含模式$filewithpattern”;完成<