Aaron
Aaron

Reputation: 3325

Cygwin running script from one batch file?

I know when Cygwin installs there is a batch file that is used to launch it in Windows. But if I want to run a script without launching Cygwin and doing "./script"

How do I create a batch file to do that?

As in... just have one batch file that when clicked, opens Cygwin and runs the shell script.

Upvotes: 19

Views: 34416

Answers (6)

Orwellophile
Orwellophile

Reputation: 13963

If you want to open the script in a mintty windows aswell, this (based on @galmok's answer) will do the trick.

: <<TRAMPOLINE
@echo off
C:\Users\user\Downloads\cyg-packages\bin\bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
C:\Users\user\Downloads\cyg-packages\bin\mintty.exe -i /Cygwin-Terminal.ico C:\Users\user\Downloads\cyg-packages\bin\bash -l "%~f0" "%CD%" %_args%
goto :EOF 
TRAMPOLINE
#####################
#!/bin/bash  -- it's traditional!
cd "$1"
shift
echo Working from $PWD
vim "$@"

Note: my use-case was the need to open a terminal vim from Visual Studio, which is why there is a call to vim at the end. The batch file will exit afterwards.

Upvotes: 4

FatAn
FatAn

Reputation: 21

To run "your_bash_file" from any .bat file, write following line to .bat:

git-bash.exe (path_to_your_bash_file>)

P.S. Windows Git is necessary installed

Upvotes: 0

galmok
galmok

Reputation: 879

I'd like to expand on the answer given by Austin Hastings to allow it to work without Cygwin in the path and to work in the directory setup with the bat file (so even a shortcut with a different "Start in " path is used). The changes are to include full path to bash, use option -l to make bash setup properly with paths and environment and to pass the current directory to bash as the first argument which is popped of (using shift). Remember to use Unix newlines in the file or you will get lots of errors. Also use UTF8 instead of ANSI.

: <<TRAMPOLINE
@echo off
C:\cygwin\bin\bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
C:\cygwin\bin\bash -l "%~f0" "%CD%" %_args%
goto :EOF 
TRAMPOLINE
#####################
#!/bin/bash  -- it's traditional!
cd "$1"
shift
echo Working from $PWD

Edit: Fixed the trampoline code to pass arguments properly to bash. BAT files do not have a bash equivalent of "$@" and a loop processing each argument is made and then passed to bash.exe. Now properly handles arguments with spaces.

Upvotes: 9

Austin Hastings
Austin Hastings

Reputation: 627

You might try something like this to trampoline from CMD to BASH (or whatever sh-variant):

    : <<TRAMPOLINE
    @echo off
    bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
    bash "%~f0" "%*" 
    goto :EOF 
    TRAMPOLINE
    #####################
    #!/bin/bash  -- it's traditional!
    echo "Hello from $SHELL"

Upvotes: 9

Jakob Buron
Jakob Buron

Reputation: 1238

To run "foo.bsh", create a batch file with the following contents and run it:

    set PATH=C:\cygwin\bin;%PATH%
    c:\cygwin\bin\bash.exe c:\path\to\foo.bsh

The "C:\cygwin\bin" part can differ depending on your chosen install location of cygwin.

Upvotes: 29

Daniel Haley
Daniel Haley

Reputation: 52888

What kind of shell script? Bash? If you need to run a bash script, just run it with bash.exe via a shortcut. The target would be something like:

C:\cygwin\bin\bash.exe foo.bsh

The path to bash.exe will depend on where cygwin was installed. (You don't need the path at all if the bin directory is in your path environment variable.)

You can do the same thing in a batch script if you want. The command line would be the same as "target" above.


EDIT BASED ON COMMENTS (still assuming bash):

Ok, so in your batch script (.bat) just type:

bash.exe script.txt

You may need to add the path to "bash.exe". If you're not sure where "bash.exe" is, open the "Cygwin.bat" file you mentioned in your question. It should have the path in a chdir command. (Note: Cygwin.bat is just running bash.exe in interactive mode. It's not really running anything named "cygwin".)

You will also need to add the path to "script.txt" if it isn't in the same directory as your batch script.

Again this will also work in a shortcut instead of a batch script.

Upvotes: 3

Related Questions