Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a script in MSYS2/MinGW

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.

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

I tried:

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

A window opens and closes, it does not work.

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 ...

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

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

like image 422
rom1v Avatar asked Nov 22 '17 15:11

rom1v


People also ask

How do I run Msys shell?

1. Starting the Command Shell: This part of the lab will teach you how to start the MSYS command shell in Windows. Click on Start, drag to All Programs, drag to MinGW, drag to MSYS, and click on msys. (Or, double-click on the MSYS icon on the desktop, if tere is one.)

Is MSYS2 a bash?

It consists of a command line terminal called mintty, bash, version control systems like git and subversion, tools like tar and awk and even build systems like autotools, all based on a modified version of Cygwin.

What is msys2_shell CMD?

msys2_shell. cmd, the improved multi-purpose batch file from the filesystem package. Run msys2_shell. cmd --help for usage. msys2.exe, mingw64.exe, mingw32.exe, the new pinnable launchers from the msys2-launcher package from @Elieux.

Where can I find MSYS2 and MinGW packages?

Packages for msys2 are built from recipes in the msys2-packages Git repository, packages for mingw are in mingw-packages. Official repositories are on GitHub under user the msys2 organization.

How to run a bash shell script in MSYS2 without showing window?

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:msys64usrbin[&mintty&].exe -w hide /bin/env MSYSTEM=MINGW64 /bin/bash -l /c/Users/rom1v/project/release.sh

How do I get Started with MSYS2?

Assuming you use GitHub this is the easiest way to get going. We provide a GitHub Action which handles everything from installing the latest MSYS2, updating it and installing all the packages you need. All you have to do is to provide a BASH script that runs your code in the MSYS2 environment.

Is MSYS2 32-bit or 64-bit?

Both 32-bit and 64-bit variants exist and receive mostly the same level of support. Here is a list of packages we provide. MSYS2 consists of three subsystems and their corresponding package repositories, msys2, mingw32, and mingw64.


2 Answers

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

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.

Explanation

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.

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.

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.

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.

Error handling

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

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

like image 102
David Grayson Avatar answered Oct 02 '22 04:10

David Grayson


Thanks to the answers from @David Grayson, I managed to call my release script with msys2/mingw from a Windows console (cmd), with additional directories (for Java and Meson) in $PATH:

c:\msys64\usr\bin\env MSYSTEM=MINGW64 c:\msys64\usr\bin\bash -l -c "PATH=\"/c/Program Files/Java/jdk1.8.X_XXX/bin:/c/Program Files/Meson:$PATH\" /c/Users/rom1v/project/release"
like image 33
rom1v Avatar answered Oct 02 '22 04:10

rom1v