首页 > 解决方案 > 在'for file in 中排除一个文件;做 ; 完成'命令

问题描述

几周以来,我一直在使用 Drone.io 一个 CI/CD 工具,发现只能在其中执行 bin/sh 命令,而不是 bin/bash。现在我正在寻找一个单行命令来查找 '*.yaml' 上的文件,除了 'secrets.yaml' 并使用找到的 *.yaml 文件运行命令。

我试过的是:

find /config -maxdepth 1 -name "*.yaml" -print0 | while read -d $'\0' file ; do esphome "$file" config ; done

虽然读取不适用于 bin/sh

此命令有效,但无法找到排除 secrets.yaml 的方法

for file in config/*.yaml ; do esphome "$file" config ; done

如何排除 secrets.yaml?

标签: shellfor-loopfindshdrone.io

解决方案


您快到了。只需使用-not or!来排除您不想要的文件。

find config -type f -name '*.yaml' -not -name 'secrets.yaml' -exec esphome '{}' config \;

或者

for file in config/*.yaml; do if [ "${file##*/}" != 'secrets.yaml' ]; then esphome "$file" config; fi; done

推荐阅读