windows 为什么 Cygwin 可以运行 .bat 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787522/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:17:20  来源:igfitidea点击:

Why is it that Cygwin can run .bat scripts?

windowscygwinbatch-file

提问by skiphoppy

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.

当我在 Cygwin 中从 bash 执行 .bat 脚本时,它是通过什么机制运行的?我知道,如果我运行 .EXE,它将启动,无论 .EXE 是来自 Cygwin 还是来自更传统的环境。我知道当我用 #! 一开始,Cygwin 为其提供了运行的魔力。

But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?

但是为什么 .bat 脚本可以工作?Cygwin 内部是否有一些组件知道 Windows .bat 脚本是什么以及如何处理它?或者在 Windows 下执行调用以启动 .EXE 文件是不可能的,该文件不会自动也适用于 .bat 脚本?

回答by Tunzi

Running

跑步

./test.bat params

from bash seems to be equivalent to

from bash 似乎相当于

cmd /c test.bat params

回答by BnWasteland

I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).

我相信 cygwin 中的 bash 将 bat 扩展视为被标记为可执行文件(cygwin 对 Windows 约定的提示)。因此,它使用关联的解释器(cmd.exe,每个操作系统配置)加载并执行文件,就像它创建一个新的 bash 实例来运行你的 #! 脚本(根据 posix 标准)。

回答by Mr. Anderson

And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:

如果你想像 ShellScript 进程一样 fork 一个 *.cmd 文件执行并将他的日志附加到一个文件中:

cmd /c test.bat > nohup.out &

Enjoy!

享受!