【问题标题】:Bash script to find a specific file in a directory and its subdirectories and execute 2 commands on that specific file用于在目录及其子目录中查找特定文件并在该特定文件上执行 2 个命令的 Bash 脚本
【发布时间】:2021-01-02 21:23:12
【问题描述】:

大家好,我是 bash 的新手,需要一些非常简单但不知道的东西。我需要一个在管道中调用的 bash 脚本来查找特定文件“terragrunt.hcl”。 bash 应该从目录(A)及其子目录(B、C、D、E 等)开始查找,然后在每个“terragrunt.hcl”文件上执行 2 个命令,但是从目录内部执行这 2 个命令文件所在的位置。

如果我要“手工”做,它看起来像这样:

cd A
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)
cd ../B
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)
cd ../C
(if terragrunt.hcl exist execute **command 1 && command 2** on terragrunt.hcl)

到目前为止,我并没有真正找到任何真正有用的东西。我在网上找到了这个(https://unix.stackexchange.com/questions/295965/how-to-use-find-exec-to-execute-command-in-directory-of-found-file-not-curre),但它并没有执行我正在寻找的东西。 任何帮助将不胜感激。

【问题讨论】:

    标签: bash


    【解决方案1】:

    如果您的平台的 find 支持它,请尝试使用 -exedir

    find . -name 'terragrunt.hcl' -execdir pwd \;
    

    pwd 替换为您要从该目录运行的命令。

    如果你想运行多个命令,你可以这样做:

    find . -name 'terragrunt.hcl' -execdir sh -c "command 1 && command 2" \;
    

    【讨论】:

    • 当您发现一个有用的答案时,请随时点赞并接受它。这样社区可以得到它的反馈;)
    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 2021-09-11
    • 2015-10-31
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    相关资源
    最近更新 更多