假设我有一个运行服务器和安装Git Bash的test.sh脚本,如何创build一个Windows快捷方式,我可以双击运行在前台的Git Bash中的tesh.sh,并允许我看到服务器的输出?
Git bash已经是一个批处理文件,内容类似于:
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i"
如果要在shell的上下文中运行(并保持运行)shell脚本 ,请在命令行中指定它。 诀窍是当解释脚本文件名时,它使用Windows路径 ,而不是sh / Git环境中的等效路径。
换句话说,为了在Git shell中运行D:\temp\test.sh
文件并保持运行,创建这个批处理文件:
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"
另一方面,如果你想运行一个脚本,并返回你的shell ,你应该:
~/.profile
(尝试vi ~/.profile
) ~/test.sh
(如果需要的话,还需要路径) 所以看起来像这样的.profile
:
echo Executing .profile /bin/sh ~/test.sh
和test.sh
看起来像这样:
echo Hello, World!
你会得到这个提示:
Welcome to Git (version 1.7.11-preview20120710) Run 'git help git' to display the help index. Run 'git help <command>' to display help for specific commands. Executing .profile Hello, World! ixe013@PARALINT01 ~ $
其他答案的工作,但有一个较短的解决方案,完全回答这个问题 ,这是:
如何创建一个Windows快捷方式,我可以双击在Git Bash中运行
tesh.sh
答案是:将以下命令添加到快捷方式的Target:
字段中:
"C:\Git\bin\sh.exe" -l "D:\test.sh"
其中, -l
是--login
的简写。
为了更好地理解这个命令的作用,请参考有关调用Bash的官方GNU文档:
-l
(–--login
):使这个shell的行为就好像它已经被login直接调用一样。 当shell是交互式的,这相当于用exec -l bash
启动一个登录shell。 当shell不交互时,登录shell启动文件将被执行。exec bash -l
或exec bash --login
将用Bash登录shell替换当前的shell。
另请注意:
sh.exe
的完整路径,或者将其放在PATH
环境变量中(如其他人已经指出的那样)。 -i
选项 我建议使用环境变量%ComSpec%
,而不是绝对路径cmd
:
%ComSpec% /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
甚至只是cmd
命令,通常可以从%PATH% :
cmd /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
如果你的C:\Program Files (x86)\Git\bin
添加到PATH
(这也是常见的解决方案和TortoiseGit安装案例之一),你可以使用:
cmd /c "sh --login -i"
我认为最好的解决办法是:
调用正确的shell,没有不必要的窗口,然后调用bash脚本; 脚本退出后窗口将保持打开状态:
例如,在桌面上创建一个 mintty.exe
的快捷方式
编辑快捷方式的属性并更改目标(保持路径):
"C:\Program Files\Git\usr\bin\mintty.exe" -h always /bin/bash -l -e 'D:\folder\script.sh'