Author Topic: The shebang command to run the bash script under Windows CMD  (Read 3211 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5391
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
The shebang command to run the bash script under Windows CMD
« on: September 11, 2021, 10:35:09 am »
Hi, I just noticed that there is a nice project named:   https://github.com/matveyt/shebang

With this project, we can run bash script from normal Windows CMD.

Here is my test which can correctly run "wx-config-3.1"(this is a bash script) from Windows CMD.

Step 1, get and build the shebang project

You just clone/download this project.

Under your local folder, inside the msys2's mingw64 compiler command line(started by double click the "mingw64.exe" under msys2 folder)

Code
gcc -s shebang.c -o shebang.exe -lshlwapi -DUNICODE -D_UNICODE -municode

This will generate a "shebang.exe" in the same folder.


Step 2, rename this "shebang.exe" to the bash script executable name you want

I notice in my msys2's folder, there is a bash file named:  "F:\msys2\mingw64\bin\wx-config", this file is for wx 3.0, and since I have installed wx 3.1 from pacman, there is another file named "F:\msys2\mingw64\bin\wx-config-3.1".

Now, let me try with the later script file "wx-config-3.1".

I need to copy/rename the "shebang.exe" to "wx-config-3.1.exe"

Step 3, prepare the PATH

I just start the Windows CMD window, and type something like:

Code
set PATH=F:\msys2\usr\bin;F:\msys2\mingw64\bin;%PATH%

This will put the necessary tools in the PATH

Step 4, run the "wx-config-3.1.exe" from the CMD

Now, you can type such command inside your CMD

Code
D:\code\shebang-master>wx-config-3.1.exe --cflags
-I/mingw64/lib/wx/include/msw-unicode-3.1 -I/mingw64/include/wx-3.1 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__

D:\code\shebang-master>wx-config-3.1.exe --libs
-L/mingw64/lib   -pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--subsystem,windows -mwindows -lwx_mswu_xrc-3.1 -lwx_mswu_html-3.1 -lwx_mswu_qa-3.1 -lwx_mswu_core-3.1 -lwx_baseu_xml-3.1 -lwx_baseu_net-3.1 -lwx_baseu-3.1

You can see, when you run the wx-config-3.1.exe, it will internally call the msys' bash script engine.

One remaining bug:
If I run the the same command inside the msys2' mingw64 shell, I got this:


Code
# wx-config-3.1 --cflags
-IF:/msys2/mingw64/lib/wx/include/msw-unicode-3.1 -IF:/msys2/mingw64/include/wx-3.1 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__

So, it looks like the returned path is not the same in the two cases.

Any one knows how to fix this issue? Maybe, I need to add some options?




If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7187
    • My Best Post
Re: The shebang command to run the bash script under Windows CMD
« Reply #1 on: September 11, 2021, 12:38:56 pm »
Try running the below in both places.

Code
wx-config-3.1 --prefix

I used to use something like
Code
wx-config-3.1 --prefix=<COMPILERPATH> --libs

Where <COMPILERPATH> was an CB variable.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5391
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: The shebang command to run the bash script under Windows CMD
« Reply #2 on: September 11, 2021, 01:51:06 pm »
Hi, tim, thanks for the reply.

In-fact, I found the shebang project project on github because I see you star this project.  :)

I just try adding the "--prefix" option in Windows CMD, it gives nice result:

Code
D:\code\shebang-master>wx-config-3.1.exe --prefix=F:/msys2/mingw64/bin --cflags
-IF:/msys2/mingw64/bin/lib/wx/include/msw-unicode-3.1 -IF:/msys2/mingw64/bin/include/wx-3.1 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__

D:\code\shebang-master>wx-config-3.1.exe --prefix=F:/msys2/mingw64/bin --libs
-LF:/msys2/mingw64/bin/lib   -pipe -Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high -Wl,--subsystem,windows -mwindows -lwx_mswu_xrc-3.1 -lwx_mswu_html-3.1 -lwx_mswu_qa-3.1 -lwx_mswu_core-3.1 -lwx_baseu_xml-3.1 -lwx_baseu_net-3.1 -lwx_baseu-3.1
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5391
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: The shebang command to run the bash script under Windows CMD
« Reply #3 on: September 11, 2021, 01:58:01 pm »
The best way to use those results in C::B's option dialog is use some backtick option, such as:

Code

`wx-config-3.1 --cflags --prefix=$(TARGET_COMPILER_DIR)`


There are some similar method I tried before:

Code

`wx-config-msys2 --cflags --prefix=$(TARGET_COMPILER_DIR)`


Where the wx-config-msys2 comes from this eranif's project:  https://github.com/eranif/wx-config-msys2
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5391
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: The shebang command to run the bash script under Windows CMD
« Reply #4 on: September 12, 2021, 05:49:20 am »


One remaining bug:
If I run the the same command inside the msys2' mingw64 shell, I got this:

Code
# wx-config-3.1 --cflags
-IF:/msys2/mingw64/lib/wx/include/msw-unicode-3.1 -IF:/msys2/mingw64/include/wx-3.1 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__

So, it looks like the returned path is not the same in the two cases.

Any one knows how to fix this issue? Maybe, I need to add some options?


This issue is fixed, see this github ticket: How to control the returned file path from shebang tool matveyt/shebang

The author of the shebang give me the solution.

Code
set PATH=F:\msys2\mingw64\bin;F:\msys2\usr\bin;%PATH%

Just put the mingw64/bin path before msys2/usr/bin path.  ;)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.