[英]Run shell script globally in Git bash in Windows
[英]Windows shortcut to run a Git Bash script
假設我有一個運行服務器並安裝了Git Bash的test.sh腳本,如何創建一個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的行為就像登錄直接調用一樣。 當shell是交互式的時,這相當於使用exec -l bash
啟動登錄shell。 當shell不是交互式時,將執行登錄shell啟動文件。exec bash -l
或exec bash --login
將用Bash登錄shell替換當前shell。
另請注意:
sh.exe
的完整路徑或將其放在PATH
環境變量中(正如其他人已經指出的那樣)。 -i
選項 我認為最好的解決方案:
請執行下列操作:
例如,在桌面上創建 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'
參數說明:
-h always
在腳本完成時-h always
保持窗口打開,因此在您仍在讀取輸出時窗口不會消失(如果您不需要讀取輸出並希望窗口自動關閉,則移除)。
-l
使這個shell表現得像登錄直接調用一樣。
-e
如果管道返回非零狀態,則立即退出( 更多信息 )。
我建議使用環境變量%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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.