2012-08-31 46 views
1

请考虑我在名为test的文件夹中有许多shell脚本。我想执行除了一个特定文件以外的所有文件。我该怎么办?重新定位文件或手动执行文件不是一种选择。有没有什么办法可以在单线上做到这一点。或者,可以添加一些sh path/to/test/*.sh,它执行所有文件?命令行:使用通配符时忽略特定文件

+0

你的意思是我想包括除一人外的所有文件? –

回答

4
for file in test/*; do 
    [ "$file" != "test/do-not-run.sh" ] && sh "$file" 
done 

如果您正在使用bash,您可以使用扩展模式,以跳过不需要的脚本:

shopt -s extglob 
for file in test/!(do-not-run).sh; do 
    sh "$file" 
done 
+0

+1用于extglob-usage :-) – plundra

1
for FILE in `ls "$YOURPATH"` ; do 
    test "$FILE" != "do-not-run.sh" && sh "$YOURPATH/$FILE"; 
done 
+0

如果有任何包含空格的路径,解析'ls'的输出将会失败。 – chepner

1

find path/to/test -name "*.sh" \! -name $pattern_for_unwanted_scripts -exec {} \;

查找将递归执行中到底该目录中的所有条目在.sh(-name“* .sh”)并且不匹配不需要的模式(\!-name $ pattern_for_unwanted_scripts)。

0

bash,只要你做shopt -s extglob您可以使用“扩展通配符”,允许使用哪些匹配除了给定的模式之一什么!(pattern-list)

你的情况:

shopt -s extglob 
for f in !(do-not-run.sh); do if [ "${f##*.}" == "sh" ]; then sh $f; fi; done 
+0

这将匹配不是“do-not-run.sh”的每个文件。 – chepner

+0

是的你是对的,在'.sh后缀'上加了测试 –

+0

或者,把扩展名移出负模式:'!(不要运行).sh'匹配所有的“* .sh”文件,除了“do -not-run.sh” –