【问题标题】:git script works when shell = /bin/sh but not when shell = /bin/bash. How to fixgit 脚本在 shell = /bin/sh 时有效,但在 shell = /bin/bash 时无效。怎么修
【发布时间】:2016-01-18 10:59:09
【问题描述】:

我正在使用这个脚本(文件名 git-proj-setup):

#!/bin/sh

if [ ! -d "project1" ]; then
    echo Creating project1 folder
    mkdir "project1"
fi

git clone git@gitserver:CDE/project1
cd ./project1
git checkout master

我这样运行脚本:

git proj-setup

当我从我的 shell 运行这个脚本时,程序会按预期运行:

person1@comp1 /c/dev
$ git proj-setup
Creating project1 folder
Cloning into 'project1'...
remote: Counting objects: 3, done.
Receiving objects: 100
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Checking connectivity... done.
Already on 'master'
Your branch is up-to-date with 'origin/master'.

person1@comp1 /c/dev
$ echo $SHELL
/bin/sh

但是当我从另一台机器运行时,脚本失败了:

person2@comp2 MINGW64 /c/dev
$ git proj-setup
git: 'proj-setup' is not a git command. See 'git --help'.

person2@comp2 MINGW64 /c/dev
$ echo $SHELL
/usr/bin/bash

但是,如果我在第二台计算机上运行脚本中的命令,它会按预期工作。

这是在 Windows 7 上运行 git for Windows。

在安装了最新的 Windows 版 Git 的 PC 上运行略有不同:https://git-scm.com/download/win 版本 2.7.0 64 位版本。在它不起作用的 PC 上,它运行的是早期版本的 git。不确定这是否相关。

问题是由于不同的外壳造成的吗?如何让一个脚本同时在 /bin/sh 和 /bin/bash shell 上运行?

【问题讨论】:

  • @AngusComber 是你的 $PATH 中的 git-proj-setup 吗?
  • 您可能对 git 别名有不同的定义,具体取决于计算机($HOME/.gitconfig 或 .git/config)。
  • 那显然和shell无关!
  • @TNW 这个问题很关键。为 PC2 添加固定的 PC2。但该文件夹不在 PC1 的路径中。奇怪。
  • 请注意,$SHELL 是登录 shell,不一定是您当前正在运行的那个。

标签: git bash shell


【解决方案1】:

运行命令git xxx的唯一条件是:

  • 脚本名为git-xxx(无扩展名)
  • 说的脚本在PATH

这适用于旧的 git 1.9.5 msysgit-basedmore recent git for windows
在这两种情况下,您都不必使用 git bash:您可以直接从常规 CMD 会话中运行它。

仔细检查 git-proj-setup 是否在另一台机器的 PATH 引用的文件夹之一中。

【讨论】:

  • 您的答案是正确的,因为当我添加到 PC2 上的路径时,它起作用了。但奇怪的是,在我的 PC 上,我运行脚本的路径,即当前目录 (.),不在我的路径中,但它有效。无论如何,添加到路径修复。
  • @AngusComber 永远不要将. 添加到您的路径中!否则,获得用户权限的黑客可以“覆盖”系统命令。
猜你喜欢
  • 2012-05-22
  • 1970-01-01
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2011-11-14
  • 1970-01-01
  • 2020-10-05
相关资源
最近更新 更多