【问题标题】:Bash Shell Script Process Each Directory in HomeBash Shell 脚本处理 Home 中的每个目录
【发布时间】:2015-04-16 22:16:04
【问题描述】:

我正在使用 Git Bash,我想编写一个脚本,为我的主目录中的每个目录(本地存储库)处理相同的命令集。这在 DOS 中很容易,大多数人认为它充其量是有缺陷的,所以我确信在 bash 中有一种方法。

比如一些伪代码:

ls --directories-in-this-folder -> $repo_list
for each $folder in $repo_list do {
  ...my commmand set for each repo...
}

有人知道如何在 Bash 中执行此操作吗?

【问题讨论】:

    标签: bash directory git-bash


    【解决方案1】:

    您可以在 bash 中执行此操作(即使在 Windows 上,如果您在 %PATH% 中的任何位置将脚本命名为 git-xxx)

    #! /bin/bash
    cd /your/git/repos/dir
    for folder in $(ls -1); do
      cd /your/git/repos/dir/$folder
      # your git command
    done
    

    如“Git Status Across Multiple Repositories on a Mac”中所述,您甚至不必 cd 进入 git repo 文件夹即可执行 git 命令:

    #! /bin/bash
    cd /your/git/repos/dir
    for folder in $(ls -1); do
      worktree=/your/git/repos/dir/$folder
      gitdir=$worktree/.git # for non-bare repos
      # your git command
      git --git-dir=$gitdir --work-tree=$worktree ...
    done
    

    【讨论】:

    • 谢谢,但我收到一个错误:sh.exe": repo_dir=$(ls -1)': not a valid identifier. If I run ls -1` 在脚本中它工作正常, 但是当 for 语句的一部分发生错误。
    • @JimFell 抱歉:cyberciti.biz/faq/bash-for-loop。我将编辑答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2018-08-24
    • 2015-05-17
    • 2017-09-27
    相关资源
    最近更新 更多