shellfor-loopfindshdrone.io

exclude one file in 'for file in ; do ; done' command


since a few weeks I've been using Drone.io a CI/CD tool and found out that only bin/sh commands can be executed in it and not bin/bash. Now I'm looking for a one line command to find files on '*.yaml' except 'secrets.yaml' and run a command with the found *.yaml files.

What i have tried is:

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

while read doest work with bin/sh

This command works but cant find a way to exlude secrets.yaml

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

How do I exclude secrets.yaml?


Solution

  • You're almost there. Just use a -not or ! to exclude files that you don't want to.

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

    Or

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