简体   繁体   English

用于运行Git Bash脚本的Windows快捷方式

[英]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回来 ,你应该:

  1. Open the shell as is 按原样打开shell
  2. Edit or create ~/.profile (try vi ~/.profile ) 编辑或创建~/.profile (试试vi ~/.profile
  3. Add this line : ~/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 with exec -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 or exec bash --login will replace the current shell with a Bash login shell. exec bash -lexec bash --login将用Bash登录shell替换当前shell。

Also note that: 另请注意:

  • You either need the full path to sh.exe or have it in your PATH environment variable (as others have already pointed out). 您需要sh.exe的完整路径或将其放在PATH环境变量中(正如其他人已经指出的那样)。
  • If you really need to force shell invocation in interactive mode, you can add the -i option 如果确实需要在交互模式下强制进行shell调用,则可以添加-i选项
  • The last parameter is the path to the script that has to be executed. 最后一个参数是必须执行的脚本的路径。 This path should be in Windows format. 此路径应为Windows格式。

Best solution in my opinion: 我认为最好的解决方案:

  • Invokes the right shell 调用正确的shell
  • No unnecessary windows 没有多余的窗户
  • Invokes a bash script afterwards 之后调用bash脚本
  • Window will stay open after the script exits 脚本退出后窗口将保持打开状态

Do the following: 请执行下列操作:

  1. 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

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM