通过grep匹配动态排除文件

dynamic exclusion of files through grep matching

我有一个文件 source-push.sh,其中 returns 我想从 find 命令的结果中排除的文件列表。

看起来像这样:

#!/usr/bin/env bash
find . -not \( -path './node_modules' -prune \) -name '*.js' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 standard --fix
find . -not \( -path './node_modules' -prune \) -name '*.css' | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev) | xargs -L1 stylelint --config stylelint.json

应该有比这更好的方法。有什么建议吗?

而不是:

... | grep -vE $(echo $(./source-push.sh | xargs -I{} echo -n "{}|") | rev | cut -b2- | rev ) | ...

您可以使用 POSIX 选项 -F-f:

... | grep -v -F -f <( ./source-push.sh ) | ...
  • -F 告诉 grep 模式是固定字符串
    • (避免如果模式包含 grep -E 特有的字符,您的原始代码将被破坏的问题)
  • -f file 告诉 grep 使用来自 file
  • 的模式列表
  • <( ... ) 是一种 bash 将程序输出呈现为文件(命名管道)
  • 的方法