且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MSYS2/MinGW中执行脚本

更新时间:2023-09-26 12:11:34

要在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).