运行 Git Bash 脚本的 Windows 快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21564275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows shortcut to run a Git Bash script
提问by konyak
Assuming I have a test.sh script that runs a server and Git Bash installed, how do I create a Windows shortcut that I can double click to run tesh.sh in Git Bash in the foreground and allows me to see the output of the server?
假设我有一个运行服务器并安装了 Git Bash 的 test.sh 脚本,我如何创建一个 Windows 快捷方式,我可以双击它在前台运行 Git Bash 中的 tesh.sh 并允许我查看服务器的输出?
回答by ixe013
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 scriptin the context of the shell, specify it at the command line. 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.
如果您想在 shell的上下文中运行(并保持运行)一个 shell 脚本,请在命令行中指定它。诀窍在于,当解释脚本文件名时,它使用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 :
换句话说,要D:\temp\test.sh
在 Git shell 中运行该文件并让它继续运行,请创建此批处理文件:
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 回来,你应该:
- Open the shell as is
- Edit or create
~/.profile
(tryvi ~/.profile
) - Add this line :
~/test.sh
(ajdust the path if needed)
- 按原样打开外壳
- 编辑或创建
~/.profile
(尝试vi ~/.profile
) - 添加这一行:(
~/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 ~
$
回答by quasoft
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
如何创建我可以双击以
tesh.sh
在 Git Bash 中运行的 Windows 快捷方式
The answeris: 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:
为了更好地理解此命令的作用,请查阅有关Invoking Bash 的官方 GNU 文档:
-l
(--login
): Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell withexec -l bash
. When the shell is not interactive, the login shell startup files will be executed.exec bash -l
orexec bash --login
will replace the current shell with a Bash login shell.
-l
(--login
): 使这个 shell 就像是直接被 login 调用一样。当 shell 是交互式的时,这等效于使用exec -l bash
. 当 shell 不交互时,将执行登录 shell 启动文件。exec bash -l
或者exec bash --login
将使用 Bash 登录 shell 替换当前 shell。
Also note that:
另请注意:
- You either need the full path to
sh.exe
or have it in yourPATH
environment variable (as others have already pointed out). - If you really need to force shell invocation in interactive mode, you can add the
-i
option - The last parameter is the path to the script that has to be executed. This path should be in Windows format.
- 您要么需要完整路径,
sh.exe
要么将其包含在PATH
环境变量中(正如其他人已经指出的那样)。 - 如果你真的需要在交互模式下强制shell调用,你可以添加
-i
选项 - 最后一个参数是必须执行的脚本的路径。此路径应为 Windows 格式。
回答by Hannes Schneidermayer
Best solution in my opinion:
我认为最好的解决方案:
- Invokes the right shell
- No unnecessary windows
- Invokes a bash script afterwards
- Window will stay open after the script exits
- 调用正确的外壳
- 没有不必要的窗户
- 之后调用 bash 脚本
- 脚本退出后窗口将保持打开状态
Do the following:
请执行下列操作:
Create a shortcutto
mintty.exe
on your desktop, for example. It is found under%installation dir%/Git/usr/bin/mintty.exe
Edit propertiesof the shortcut and change the target (keep the path):
mintty.exe
例如,在桌面上创建一个快捷方式。它被发现在%installation dir%/Git/usr/bin/mintty.exe
编辑快捷方式的属性并更改目标(保留路径):
"C:\Program Files\Git\usr\bin\mintty.exe" -h always /bin/bash -l -e 'D:\folder\script.sh'
"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
在脚本完成时保持窗口打开,因此在您仍在读取输出时窗口不会消失(如果您不需要读取输出并希望窗口自动关闭,请删除)。
-l
makes this shell act as if it had been directly invoked by login.
-l
使这个 shell 就像是直接被 login 调用一样。
-e
exits immediately if a pipeline returns a non-zero status (more info).
-e
如果管道返回非零状态(更多信息),则立即退出。
回答by radistao
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 TortoiseGitinstalling) you can use just:
如果您C:\Program Files (x86)\Git\bin
添加到PATH
(这也是常见的解决方案和TortoiseGit安装案例之一),您可以只使用:
cmd /c "sh --login -i"