繁体   English   中英

在'for file in;中排除一个文件; 做; 完成'命令

[英]exclude one file in 'for file in ; do ; done' command

几周以来,我一直在使用 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

此命令有效,但找不到排除机密的方法。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

如果您正在运行自己的无人机实例,您可能对我们编写的转换扩展感兴趣https://github.com/meltwater/drone-convert-pathschanged

该扩展允许根据更改的文件包含或排除管道或管道步骤。

  - name: example
    image: busybox
    commands:
    - echo "a file other than secrets.yaml was changed"
    when:
      paths:
        include:
        - '*.yaml'
        exclude:
        - secrets.yaml

暂无
暂无

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

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