在 '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

而 read 不适用于 bin/sh

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

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

如何排除 secrets.yaml?

解决方法

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

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
,

您不需要find

for file in config/*.yaml; do 
  case "$file" in
  config/secrets.yml) continue ;;
                   *)  esphome "$file" config ;;
  esac
done

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

如果是 bash -

$: touch a.yaml b.yaml c.yaml secrets.yaml
$: shopt -s extglob
$: echo !(secrets).yaml
a.yaml b.yaml c.yaml

所以 -

shopt -s extglob
for file in config/!(secrets).yaml; do esphome "$file" config ; done