通过grep匹配动态排除文件

问题描述 投票:0回答:1

我有一个文件source-push.sh,它返回我要从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 find echo xargs
1个回答
0
投票

代替:

... | 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方式
© www.soinside.com 2019 - 2024. All rights reserved.