shell - 在'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?
解决方案
您快到了。只需使用-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
推荐阅读
- postgresql - 如何在 postgres 中创建条件事务?
- node.js - Twilio 消息 API 不允许在其消息正文中使用变量或连接字符串
- azure - 使用 Azure 表单识别器 API 获取模型无效消息
- go - 我可以使用 for c := range 遍历字符串吗
但只使用字符而不使用索引? - angular - Angular 项目的 URL 在 Apache 服务器中运行的问题
- smartsheet-api - 尝试删除我创建的工作区时从 smartsheet API 收到错误
- c# - 一个数字范围内的素数
- mongodb - 有没有办法为此查询使用聚合管道方法
- asp.net-web-api2 - Web API 2:如果控制器方法同时被 [HttpPost] 和 [HttpGet] 重载,[FromBody] 控制器方法参数是否可以是可选的?
- python - 字符串清理