linux - 如何在 git 或 shell 别名中使用 `find -exec sh -c`

标签 linux git bash shell

我正在使用以下作为 git shell 脚本。

    #!/bin/sh

    [ -z "$1" ] && dir="$PWD" || dir="$1"

    find "$dir" -mindepth 1 -maxdepth 1 -type d -exec sh -c '
        for i; do
            printf "\n%s\n" "$i"
            cd "$i" \
            && git remote update
        done' sh {} +

但想使用 git 别名。

    rmup=!sh -c "find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '\
        for i; do \
            printf "\n%s\n" "$i" && cd "$i" && git remote update; \
        done' sh {} +"

或者至少是一个 shell 别名。

    alias rmup="find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '\
        for i; do \
            printf "\n%s\n" "$i" && cd "$i" && git remote update; \
        done' sh {} +"

我尝试了各种转义、单引号和双引号组合,但都没有成功。

想要使用“嵌入式”别名的原因是为了简化机器之间的维护和可移植性,因为我已经同步了我的 .gitconfig.bashrc

忘了说我也试过 shell 函数,当 printf 打印目录时,cd 提示目录不存在。

    rmup() {
        find . -mindepth 1 -maxdepth 1 -type d -exec sh -c '
            for i; do
               printf "\n%s\n" "$i" && cd "$i" && git remote update;
            done' sh {} +
    }

最佳答案

如果你已经有了 shell 脚本,那么将它命名为 git-rmup,把它放在你的 $PATH 中,你就可以调用 git rmup 就好像它是一个 Git 别名。

关于linux - 如何在 git 或 shell 别名中使用 `find -exec sh -c`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408006/

相关文章:

git - Git 中的内部/外部 merge 意味着什么?

linux - 我是否需要创建一个单独的用户来托管具有 SSH 连接的 git 存储库

python - 无法在 MacOS : 'zsh: command not found: nvm' 上使用 nvm install

linux - 如何在相同的环境下生成新的 xterm

检查用户输入的字符串在 C 中的格式是否为 "%d/%d/%d/%d/%d"

linux - 缺少 time.h 的 lib 文件

Linux:环境变量存储在哪里?

git - ProGit 书中的箭头方向

linux - Bash - 如何正确列出文件夹中的文件并管理排除

regex - 单引号内的单引号问题 - Bash 命令行 GNU 并行