2

问题是,我有一个自定义 bash 脚本,可以对指定文件夹执行特定操作,语法为:

myscript $1 $2 $3

$1文件夹的名称和作业$2$3其他必要数字参数在哪里。

假设我有一个包含许多文件夹的目录,例如:

a
b
c
d
ref-1
ref-2
ref-3

我只需要在不以ref-. 因为手动执行每个文件的脚本会花费我很多精力,所以我想弄清楚如何使用正则表达式来限制到所需的文件夹,使用像“extglob”这样的反向匹配,以及每一个都使用给定的$2$3参数执行脚本。

4

4 回答 4

3

例如,您可以$(ls -d */|grep -v ^ref-)

于 2011-11-26T17:36:33.217 回答
1

这应该为你做 -

使用find -type d选项,您将查找搜索限制为目录。提供一个pattern with -not -name可以帮助您忽略匹配的目录。-exec option可以帮助您启动已找到的所有目录的 shell 脚本。{} \;只是意味着 exec 为每个目录一次执行一个 shell 脚本的缓冲区空间。-depth n将确保它下降到 n 个目录级别。

find . -not -name ref\* -not -name .\* -type d -depth 1 -exec ./script.sh {} \;

更新:

我的错,我忘了处理两个用户驱动的参数。我可能不是专家,但我想到的最简单的方法是将查找结果发送到文件,然后在该文件上运行 for 循环。

像这样的东西-

[jaypal~/Temp]$ find . -not -name ref\* -not -name .\* -type d -depth 1 > dirlist
[jaypal~/Temp]$ for i in dirlist; do ./script.sh $i param1 param2; done

传递用户驱动参数的替代方法(如@dma_k 所建议)

[jaypal~/Temp]$ find . -not -name ref\* -not -name .\* -type d -depth 1 | xargs script.sh param1 param2
于 2011-11-26T21:02:15.250 回答
1

这可能对您有用:

ls !(ref-?)

或者

ls !(ref*)

或者

ls +({a..d})

要不就

ls ?

编辑:

然后可以运行该脚本:

for first in !(ref-?); do script.sh $first $second $third; done

其中secondthird是数字参数。

于 2011-11-26T20:04:26.910 回答
0

怎么样 ls | grep yourRegexHere

于 2011-11-26T17:37:50.093 回答