'find' using regex with variables

情到浓时终转凉″ 提交于 2021-02-07 04:16:23

问题


Please, help me with the following:

Let it be three files:

file-aaa.sh 
file-bbb.sh
file-xxx.sh

In a bash script I have variables

$a=aaa 
$b=bbb

Now, I want to execute something like:

find . -name "file-[$a|$b].sh"

and expect to get two files in output. What am I doing wrong?


回答1:


You can use this find:

find . -name "file-$a.sh" -o -name "file-$b.sh"

To combine it into one using -regex option:

On OSX:

find -E . -regex ".*file-($a|$b)\.txt"

On Linux:

find . -regextype posix-extended -regex ".*file-($a|$b)\.txt"


来源:https://stackoverflow.com/questions/24552144/find-using-regex-with-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!