[英]Windows shortcut to run a Git Bash script
假设我有一个运行服务器并安装了Git Bash的test.sh脚本,如何创建一个Windows快捷方式,我可以双击在前台的Git Bash中运行tesh.sh,并允许我查看服务器的输出?
Git bash is already a batch file with content similar to this : Git bash已经是一个批处理文件,其内容与此类似:
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i"
If you want run (and leave running) a shell script in the context of the shell, specify it at the command line. 如果要在shell的上下文中运行(并保持运行)shell脚本 ,请在命令行中指定它。 The trick is that when the script file name is interpreted, it uses the Windows path , not the equivalent path in the sh/Git environment.
诀窍是,当解释脚本文件名时,它使用Windows路径 ,而不是sh / Git环境中的等效路径。
In other words, to run the file D:\\temp\\test.sh
in the Git shell and leave it running, create this batch file : 换句话说,要在Git shell中运行文件
D:\\temp\\test.sh
并使其保持运行,请创建此批处理文件:
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"
On the other hand, if you want to run a script and get your shell back , you should : 另一方面,如果你想运行一个脚本并让你的shell回来 ,你应该:
~/.profile
(try vi ~/.profile
) ~/.profile
(试试vi ~/.profile
) ~/test.sh
(ajdust the path if needed) ~/test.sh
(如果需要,请调整路径) So with a .profile
that looks like this : 所以使用
.profile
看起来像这样:
echo Executing .profile
/bin/sh ~/test.sh
And test.sh
that looks like this : 和
test.sh
看起来像这样:
echo Hello, World!
You will get this prompt : 你会得到这个提示:
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 ~
$
Other answers work, but there is a shorter solution, that fully answers the question , which was: 其他答案有效,但有一个较短的解决方案,完全回答了这个问题 :
How to create a Windows shortcut that I can double click to run
tesh.sh
in Git Bash如何创建一个Windows快捷方式,我可以双击以在Git Bash中运行
tesh.sh
The answer is: add the following command to the Target:
field of the shortcut: 答案是:将以下命令添加到快捷方式的
Target:
字段:
"C:\Git\bin\sh.exe" -l "D:\test.sh"
Where, -l
is the short for --login
. 其中,
-l
是--login
的缩写。
To better understand what this command does, consult with official GNU docs about Invoking Bash : 要更好地理解此命令的作用,请参阅有关调用Bash的官方GNU文档:
-l
(--login
): Make this shell act as if it had been directly invoked by login.-l
( ---login
):使此shell的行为就像登录直接调用一样。 When the shell is interactive, this is equivalent to starting a login shell withexec -l bash
.当shell是交互式的时,这相当于使用
exec -l bash
启动登录shell。 When the shell is not interactive, the login shell startup files will be executed.当shell不是交互式时,将执行登录shell启动文件。
exec bash -l
orexec bash --login
will replace the current shell with a Bash login shell.exec bash -l
或exec bash --login
将用Bash登录shell替换当前shell。
Also note that: 另请注意:
sh.exe
or have it in your PATH
environment variable (as others have already pointed out). sh.exe
的完整路径或将其放在PATH
环境变量中(正如其他人已经指出的那样)。 -i
option -i
选项 Best solution in my opinion: 我认为最好的解决方案:
Do the following: 请执行下列操作:
Create a shortcut to mintty.exe
on your desktop, for example. 例如,在桌面上创建
mintty.exe
的快捷方式 。 It is found under %installation dir%/Git/usr/bin/mintty.exe
它位于
%installation dir%/Git/usr/bin/mintty.exe
Edit properties of the shortcut and change the target (keep the path): 编辑快捷方式的属性并更改目标(保留路径):
"C:\\Program Files\\Git\\usr\\bin\\mintty.exe" -h always /bin/bash -l -e 'D:\\folder\\script.sh'
Explanation of the parameters: 参数说明:
-h always
keeps the window open when the script finished, so the window won't disappear while you are still reading the output (remove if you don't need to read the output and want the window to close automatically). -h always
在脚本完成时-h always
保持窗口打开,因此在您仍在读取输出时窗口不会消失(如果您不需要读取输出并希望窗口自动关闭,则移除)。
-l
makes this shell act as if it had been directly invoked by login. -l
使这个shell表现得像登录直接调用一样。
-e
exits immediately if a pipeline returns a non-zero status ( more info ). -e
如果管道返回非零状态,则立即退出( 更多信息 )。
I'd recommend to use environment variable %ComSpec%
, instead of absolute path to cmd
: 我建议使用环境变量
%ComSpec%
,而不是cmd
的绝对路径:
%ComSpec% /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
or even just cmd
command, which is usually available from %PATH% : 甚至只是
cmd
命令,通常可以从%PATH%获得 :
cmd /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
if your C:\\Program Files (x86)\\Git\\bin
added to PATH
(which is also common solution and one of cases on TortoiseGit installing) you can use just: 如果你的
C:\\Program Files (x86)\\Git\\bin
添加到PATH
(这也是常见的解决方案和TortoiseGit安装的一个案例)你可以使用:
cmd /c "sh --login -i"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.