Search code examples
msys2

How do I run an MSys2 shell to block until command finishes


How do I run MSys from the Windows command line to run a bash command such that the Windows MSys command doesn't return until the bash command does?

I don't want to use an interactive shell that requires the user to dismiss the window, if that would even work, as it will break stuff.

---Context---

I have a need to run a shell script as part of a build system on Windows. To do this I am using MSys2. The shell script is currently invoked from the IDE, that my boss and others insist on using, to generate a version number from the repository information and push it into an auto-generated header file.

The problem is that when the MSys executable is run, the process forks and returns, and the build process continues before the script executes and the header file is generated. When running on a clean checkout, the build fails because the header file is missing. When re-running the build, the version information in the compiled program is from the previous build.

Is there a way to invoke the command so that the process doesn't fork at any stage, or if it does, blocks until the shell script has completed?

The command I am using is:

"c:\msys64\mingw32.exe" "../EXTERNALS/Scripts/GitBuildVersion.sh" "../src" "../BUILDS"

An interactive shell would break our automated build process in our CI system.


Solution

  • If I remember correctly:

    C:\msys64\msys2_shell.cmd -mingw32 -defterm -no-start -here -c "your_script.sh param1 param2"
    

    Where:

    • -mingw32 is your environment of choice (see --help for a list).
    • -defterm makes it execute in the current terminal, instead of opening a mintty instance.
    • -no-start makes the call blocking, which is what you want.
    • -here preserves the working directory.