在MSYS2/MinGW中执行脚本 [英] Executing a script in MSYS2/MinGW

查看:1319
本文介绍了在MSYS2/MinGW中执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上,如果我启动c:\msys64\mingw64.exe,它将打开一个外壳程序,可在其中构建我的项目,例如,调用release bash脚本(为简化起见).一切正常.

On Windows, if I start c:\msys64\mingw64.exe, it opens a shell, where I can build my project, let's say by calling a release bash script (to simplify). Everything works fine.

现在,我想直接在mingw64上执行我的release脚本,而无需进行交互.

Now, I would like to execute my release script on mingw64 directly, without interaction.

我尝试过:

c:\msys64\mingw64.exe /c/the/full/path/release

一个窗口打开和关闭,它不起作用.

A window opens and closes, it does not work.

我尝试直接使用bash,但是似乎环境设置不正确:

I attempted to use bash directly, but it seems the environment is not correctly set:

> c:\msys64\usr\bin\bash -c ls
/usr/bin/bash: ls: command not found

> c:\msys64\usr\bin\bash -c /bin/ls
... it works ...

因此很明显,环境与执行c:\msys64\mingw64.exe然后调用ls时的环境不同.

So it is obvious that the environment is not the same as when execute c:\msys64\mingw64.exe then call ls.

如何像在mingw64.exe所启动的shell中一样执行我的release脚本?

How to execute my release script as if I were in the shell started by mingw64.exe?

推荐答案

要在MSYS2中运行Bash shell脚本而不显示窗口,应右键单击桌面或Windows资源管理器中的其他位置,选择新建",选择快捷方式",然后为快捷方式目标输入以下内容:

To run a Bash shell script in MSYS2 without showing a window, you should right-click on your Desktop or somewhere else in Windows Explorer, select "New", select "Shortcut", and then enter something like this for the shortcut target:

C:\msys64\usr\bin\mintty.exe -w hide /bin/env MSYSTEM=MINGW64 /bin/bash -l /c/Users/rom1v/project/release.sh

请注意,这里有4条路径. minttyrelease.sh的路径是您需要调整的绝对路径. envbash的路径是相对于MSYS2安装目录的.还要注意,第一个路径必须是标准的Windows路径,因为Windows希望在运行快捷方式时使用该路径.

Note that there are 4 paths in here. The path to mintty and release.sh are absolute paths that you will need to adjust. The paths to env and bash are relative to your MSYS2 installation directory. Note also that the first path must be a standard Windows path, since Windows expects that when it is running a shortcut.

对于非交互式脚本使用MinTTY似乎很奇怪,但是我们需要使用为Windows子系统编译的 some 程序(GCC的-mwindows选项),或者否则,当我们运行该程序时,Windows会自动启动一个新的控制台.我们将-w hide选项传递给MinTTY,以告知它不实际显示窗口. MinTTY将该选项之后的所有内容都解释为要运行的命令.

It might seem odd to use MinTTY for a non-interactive script, but we need to use some program that was compiled for the Windows subsystem (-mwindows option to GCC), or else Windows will automatically start a new console when we run the program. We pass the -w hide option to MinTTY to tell it not to actually show a window. Everything after that option is interpreted by MinTTY as a command to run.

因此MinTTY将在MSYS2发行版中运行/bin/env,并将其余参数传递给它.这是一个方便的实用程序,实际上是Linux和MSYS2的标准部分.它将MSYSTEM环境变量设置为MINGW64(这在后面很重要),然后使用其余的命令行参数运行/bin/bash.

So MinTTY will run /bin/env from the MSYS2 distribution and pass the remainder of the arguments on to it. This is a handy utility that is actually a standard part of Linux as well as MSYS2. It sets the MSYSTEM environment variable to MINGW64 (which is important later) and then it runs /bin/bash with the remainder of the command-line arguments.

我们将-l传递给Bash,以便它充当登录脚本并运行某些启动脚本.特别是,MSYS2中的/etc/profile脚本至关重要,因为它查看了MSYSTEM环境变量,发现它是MINGW64,然后设置了许多其他环境变量(例如PATH)来为您提供MinGW 64位Shell环境.

We pass -l to Bash so that it acts as a login script, and runs certain startup scripts. In particular, the /etc/profile script from MSYS2 is essential because it looks at the MSYSTEM environment variable, sees that it is MINGW64, and then sets a bunch of other environment variables (e.g. PATH) to give you the MinGW 64-bit shell environment.

最后,我们将脚本名称作为主要参数传递给bash,因此它将在运行初始化脚本后运行该脚本.

Finally, we pass the name of your script as the main argument to bash, so it will run that script after running the initialization scripts.

请注意,如果您的Bash脚本有错误,您将不会收到任何通知,因为上面的快捷方式不会打开任何控制台窗口.我个人会觉得很烦.我可能会删除-w hide选项,然后制作一个包装的bash脚本,其功能类似于:

Note that if your Bash script has an error, you won't get any notification, because the shortcut above doesn't open any console windows. I personally would find that pretty annoying. I'd probably remove the -w hide option, then make a wrapper bash script that just does something like:

run_my_main_script || sleep 10000

因此,如果主脚本成功执行,请立即退出,否则将窗口保持打开状态10000秒.您甚至不必将该包装程序脚本放在其自己的文件中,只需将其作为Bash -c选项的参数放在快捷方式中即可(不要忘记将其包装在双引号中).

So if the main script is successful, exit right away, otherwise keep the window open for 10000 seconds. You don't have to even put that wrapper script in its own file, you can just put it in the shortcut as the argument to Bash's -c option (don't forget to wrap it in double quotes).

这篇关于在MSYS2/MinGW中执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆