简体   繁体   English

编译没有特定子字符串的文件

[英]Compile files without certain substring

I'm new to bash, I was wondering how I would go about compiling files in a certain directory that don't contain certain strings in name. 我是bash的新手,我想知道如何在不包含名称中某些字符串的特定目录中编译文件。 So say I have gumbo.java jackson.java roosevelt.java roar.java as my files in directory. 假设我将gumbo.java jackson.java roosevelt.java roar.java作为目录中的文件。 How do I compile files that don't contain roar or gumbo in the name? 如何编译名称中不包含咆哮或浓汤的文件?

In bash, you can use an extended glob pattern like this: 在bash中,您可以使用如下扩展的glob模式:

shopt -s extglob nullglob
printf '%s\n' !(roar|gumbo).java

This matches anything other than roar or gumbo , that ends in .java . 这匹配以.java结尾的除了roargumbo以外的任何东西。 You can think of it as *.java , minus roar.java and gumbo.java . 您可以将其视为*.java ,减去roar.javagumbo.java

Substitute the printf for whatever you're using to compile, if you're happy with the output. 如果您对输出感到满意,则将printf替换为用于编译的任何内容。

If you want to negate anything that contains those substrings, you can add some asterisks: 如果要否定包含这些子字符串的任何内容,则可以添加一些星号:

printf '%s\n' !(*@(roar|gumbo)*).java

The @() matches any one of the pipe-separated options. @()匹配任何一个用管道分隔的选项。 As before, the whole thing is negated with !() . 和以前一样,整个事情都被!()否定了。

As suggested in the comments, I also enabled nullglob so that an empty match expands to nothing. 如评论中所建议,我还启用了nullglob以便空匹配扩展为空。

Try using the find command: 尝试使用find命令:

find . -type f -not -name '*roar*' -and -not -name '*gumbo*' -exec <command> {} \;

For example: 例如:

find . -type f -not -name '*roar*' -and -not -name '*gumbo*' -exec javac {} \;

I would use a standard loop: 我将使用标准循环:

for file in *.java; do
  case $file in
  *roar*|*gumbo*) continue;;
  *) javac "$file";;
  esac
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM