在 Git Bash 终端上执行 .bat 文件中的命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22397521/
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
Execute commands from .bat file on Git Bash Terminal
提问by Shabbir Panjesha
I am newbie to Git bash.
我是 Git bash 的新手。
Just out of curiosity trying to make a .bat file which contains commands(Dont know if Git Bash supports .bat file)
只是出于好奇尝试制作一个包含命令的 .bat 文件(不知道 Git Bash 是否支持 .bat 文件)
What I want to achieve is simply drag and drop this .bat file to Git Bash terminal and commands in the file get executed(Is it possible?).
我想要实现的只是将此 .bat 文件拖放到 Git Bash 终端,然后执行文件中的命令(是否可能?)。
My commands in .bat file
我在 .bat 文件中的命令
cd "C:\Users\USER\abc\xyz"
cd "C:\Users\USER\abc\xyz\pqr"
export HOME="C:\Users\USER\some_directory"
export HOME2="C:\Program Files\directoy"
采纳答案by janos
What I want to achieve is simply drag and drop this .bat file to Git Bash terminal and commands in the file get executed (Is it possible?).
我想要实现的只是将此 .bat 文件拖放到 Git Bash 终端,然后执行文件中的命令(是否可能?)。
This is not possible, and probably will never be, because it's not a natural UX. Dragging a file from a file explorer to a Git Bash terminal should produce the absolute path to the file in the terminal. Then you can press enter to execute it. The natural way to execute a file in a file explorer is to double-click on it. (The file explorer may need configuration to allow the execution of .bat
and .sh
files on double-click.)
这是不可能的,而且可能永远不会,因为这不是自然的用户体验。将文件从文件资源管理器拖到 Git Bash 终端应该会在终端中生成文件的绝对路径。然后你可以按回车键来执行它。在文件资源管理器中执行文件的自然方法是双击它。(文件浏览器可能需要配置以允许双击执行.bat
和.sh
文件。)
My commands in .bat file
cd "C:\Users\USER\abc\xyz" cd "C:\Users\USER\abc\xyz\pqr" export HOME="C:\Users\USER\some_directory" export HOME2="C:\Program Files\directoy"
我在 .bat 文件中的命令
cd "C:\Users\USER\abc\xyz" cd "C:\Users\USER\abc\xyz\pqr" export HOME="C:\Users\USER\some_directory" export HOME2="C:\Program Files\directoy"
For one thing, this script looks artificial: cd /some/abs/path
followed by cd /some/other/abs/path
is a pointless statement.
一方面,这个脚本看起来很假:cd /some/abs/path
后面cd /some/other/abs/path
是一个毫无意义的陈述。
For another thing, the .bat
extension should be used for DOS shell scripts, but the export
command doesn't exist in DOS (it exists in Bash). So your example should be a .sh
script, not a .bat
script.
另一方面,.bat
扩展应该用于 DOS shell 脚本,但该export
命令在 DOS 中不存在(它在 Bash 中存在)。所以你的例子应该是一个.sh
脚本,而不是一个.bat
脚本。
Lastly, it's important to understand the distinction between executinga script and sourcinga script:
最后,了解执行脚本和获取脚本之间的区别很重要:
When you executea script, for example with
path/to/script.sh
, the commands in it are executed in a child process. As such, commands that change the execution environment, such as changing the directory or variables, will apply only to the child process. In other words, the effect ofcd
andexport
commands will not be visible when the script exits.When you sourcea script, for example with
source path/to/script.sh
(or. path/to/script.sh
), the commands in it are executed in the current process. As such, commands that change the execution environment, such as changing the directory or variables, will apply to the current process.
当您执行脚本时,例如使用
path/to/script.sh
,其中的命令在子进程中执行。因此,更改执行环境的命令(例如更改目录或变量)将仅适用于子进程。换句话说,脚本退出时,cd
和export
命令的效果将不可见。当源的脚本,例如与
source path/to/script.sh
(或. path/to/script.sh
),在它的命令在当前的处理中执行。因此,更改执行环境的命令,例如更改目录或变量,将应用于当前进程。
In other words, if you want cd
and export
commands to have an effect in the current shell, then you want to source the script, instead of executing.
换句话说,如果您希望cd
和export
命令在当前 shell 中起作用,那么您希望获取脚本,而不是执行。
回答by Michael Freidgeim
You can run batch files just from git bash e.g.
您可以仅从 git bash 例如运行批处理文件
./clear.bat
From Out of a git console: how do I execute a batch file and then return to git console?