Cygwin 从一个批处理文件运行脚本?
我知道 Cygwin 安装时有一个批处理文件用于在 Windows 中启动它。但是,如果我想在不启动 Cygwin 并执行“./script”的情况下运行脚本,
如何创建批处理文件来执行此操作?
就像...只需一个批处理文件,单击该批处理文件即可打开 Cygwin 并运行 shell 脚本。
I know when Cygwin installs there is a batch file that is used to launch it in Windows. But if I want to run a script without launching Cygwin and doing "./script"
How do I create a batch file to do that?
As in... just have one batch file that when clicked, opens Cygwin and runs the shell script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要运行“foo.bsh”,请创建一个包含以下内容的批处理文件并运行它:
“C:\cygwin\bin”部分可能会有所不同,具体取决于您选择的 cygwin 安装位置。
To run "foo.bsh", create a batch file with the following contents and run it:
The "C:\cygwin\bin" part can differ depending on your chosen install location of cygwin.
你可以尝试像这样从 CMD 蹦床到 BASH (或任何 sh 变体):
You might try something like this to trampoline from CMD to BASH (or whatever sh-variant):
我想扩展 Austin Hastings 给出的答案,以允许它在路径中没有 Cygwin 的情况下工作,并在使用 bat 文件设置的目录中工作(因此,甚至使用具有不同“开始于”路径的快捷方式) 。更改是包含 bash 的完整路径,使用选项
-l
使 bash 设置正确的路径和环境,并将当前目录传递给 bash 作为弹出的第一个参数(使用>转移)。记住在文件中使用 Unix 换行符,否则你会得到很多错误。也使用 UTF8 而不是 ANSI。
编辑:修复了蹦床代码以将参数正确传递给 bash。 BAT 文件没有相当于“$@”的 bash,并且会创建一个处理每个参数的循环,然后将其传递给 bash.exe。现在可以正确处理带空格的参数。
I'd like to expand on the answer given by Austin Hastings to allow it to work without Cygwin in the path and to work in the directory setup with the bat file (so even a shortcut with a different "Start in " path is used). The changes are to include full path to bash, use option
-l
to make bash setup properly with paths and environment and to pass the current directory to bash as the first argument which is popped of (usingshift
). Remember to use Unix newlines in the file or you will get lots of errors. Also use UTF8 instead of ANSI.Edit: Fixed the trampoline code to pass arguments properly to bash. BAT files do not have a bash equivalent of "$@" and a loop processing each argument is made and then passed to bash.exe. Now properly handles arguments with spaces.
如果您还想在薄荷窗口中打开脚本,这(基于@galmok的答案)就可以了。
注意:我的用例是需要从 Visual Studio 打开终端 vim,这就是最后调用
vim
的原因。批处理文件随后将退出。If you want to open the script in a mintty windows aswell, this (based on @galmok's answer) will do the trick.
Note: my use-case was the need to open a terminal vim from Visual Studio, which is why there is a call to
vim
at the end. The batch file will exit afterwards.什么样的 shell 脚本?重击?如果您需要运行 bash 脚本,只需通过快捷方式使用 bash.exe 运行它即可。目标类似于:
bash.exe 的路径将取决于 cygwin 的安装位置。 (如果 bin 目录位于路径环境变量中,则根本不需要路径。)
如果需要,您可以在批处理脚本中执行相同的操作。命令行与上面的“目标”相同。
根据注释进行编辑(仍然假设 bash):
好的,所以在批处理脚本 (.bat) 中只需键入:
您可能需要添加“bash.exe”的路径。如果您不确定“bash.exe”在哪里,请打开您在问题中提到的“Cygwin.bat”文件。它应该在
chdir
命令中包含路径。 (注意:Cygwin.bat 只是以交互模式运行 bash.exe。它并没有真正运行名为“cygwin”的任何内容。)如果“script.txt”不在同一目录中,您还需要添加“script.txt”的路径作为您的批处理脚本。
同样,这也可以在快捷方式而不是批处理脚本中工作。
What kind of shell script? Bash? If you need to run a bash script, just run it with bash.exe via a shortcut. The target would be something like:
The path to bash.exe will depend on where cygwin was installed. (You don't need the path at all if the bin directory is in your path environment variable.)
You can do the same thing in a batch script if you want. The command line would be the same as "target" above.
EDIT BASED ON COMMENTS (still assuming bash):
Ok, so in your batch script (.bat) just type:
You may need to add the path to "bash.exe". If you're not sure where "bash.exe" is, open the "Cygwin.bat" file you mentioned in your question. It should have the path in a
chdir
command. (Note: Cygwin.bat is just running bash.exe in interactive mode. It's not really running anything named "cygwin".)You will also need to add the path to "script.txt" if it isn't in the same directory as your batch script.
Again this will also work in a shortcut instead of a batch script.
要从任何 .bat 文件运行“your_bash_file”,请将以下行写入 .bat:
git-bash.exe (path_to_your_bash_file>)
PS Windows Git 必须安装
To run "your_bash_file" from any .bat file, write following line to .bat:
git-bash.exe (path_to_your_bash_file>)
P.S. Windows Git is necessary installed