简体   繁体   English

Bash Shell脚本处理主目录中的每个目录

[英]Bash Shell Script Process Each Directory in Home

I am using Git Bash, and I would like to write a script that processes the same set of commands for each directory (local repo) in my home directory. 我正在使用Git Bash,我想编写一个脚本来处理主目录中每个目录(本地存储库)的相同命令集。 This would be easy enough in DOS, which most consider as handicapped at best, so I'm sure there's a way to do it in bash. 在DOS中这很容易,因为DOS在大多数情况下最多都认为是残障人士,因此我敢肯定有一种方法可以在bash中完成。

For example, some pseudo-code: 例如,一些伪代码:

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

Does anyone know how to do this in Bash? 有人知道如何在Bash中执行此操作吗?

You can do that in bash (even on Windows, if you name your script git-xxx anywhere in your %PATH% ) 您可以在bash中执行此操作(即使在Windows中,如果您将脚本git-xxx命名为%PATH%任何位置)

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

As mentioned in " Git Status Across Multiple Repositories on a Mac ", you don't even have to cd into the git repo folder in order to execute a git command: 如“ 在Mac上跨多个存储库的Git状态 ”中所述,您甚至不必进入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

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

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