how to write a batch (windows) to start Cygwin (mintty.exe) and then execute a python script? - windows

Pretty much as titled. If I were to manually do this, I would first open a Cygwin Terminal (which should be /cygwin/bin/mintty.exe), and then in that terminal, cd to the directory that has the python script, and then execute the python script by doing "python myPython.py". I'm wondering if I can write a batch script or a bash script to do this: start a Cygwin Terminal, cd to a directory, execute a python script in the directory.
Thanks.
Edited:
So I have a python script that generates csv files for activities through mongodb.This script won't function if I run it through windows cmd. I have to run it in cygwin terminal (mintty.exe). So any alternatives to execute the python script won't work. I have to somehow start a Cygwin Terminal and execute the python script through there. Any ideas please? Thanks.

Depending of your needs it could be better to start mintty (creating a new window) instead of starting bash inside the cmd.exe window.
When you want to use ansi escape sequences then it works better with a real mintty window, as the cmd window ignores the escape sequences for window resizing and positioning.
start "" C:\cygwin\bin\mintty --exec ./myProgramToExecute.sh

You may start a bash from the Windows terminal and start your script from there (without starting mintty.exe). Just execute
bash -c "cd /your/directory && python myPython.py"
from the Windows cmd prompt or a batch file.

Related

WSL - Launch bash from Powershell in new terminal and pass a command to it

I am trying to make a simple Powershell script to quickly setup my dev environment. For that I need a few instances of WSL programs running on bash terminals.
From Powershell, I am trying to:
Open a new terminal window
Start bash
Run a command with bash - In my use case I just want to run a simple npm start within bash.
A plus is if I can do all of this in one script line.
I think I am close. If I use start powershell I can start a new terminal. That inmediately opens a new PowerShell terminal.
Then, I can pass PowersShell commands to it like so:
start powershell{bash}
This opens a new terminal window and immediately opens bash.
A way to pass commands to bash in PowerShell is like this:
bash -c "npm start"
This works well. It opens bash in the same terminal and then runs the command I am passing to it. npm start works just as if I was calling it directly from bash. The problem comes when I want to pass the npm start to the new terminal. This is what I am trying:
start powershell{ bash -c "npm start"; Read-host}
This opens the new powershell terminal and it seems to be opening bash. ; Read-host is added so that the terminal doesn't close immediately. However, instead of running the npm command, it reacts by showing me information about the npm command instead of actually running it.
Is there a workaround so that I can actually get the command to run in the new terminal window after opening bash?
I believe that the recommended way to start commands in the WSL environment is to use wsl.exe now. (https://learn.microsoft.com/en-us/windows/wsl/reference#wslexe)
Try start powershell{wsl -- npm start; Read-Host}

When program uses Command to open Bash to run a script, Bash closes immediately without running the script

I have a simple, already-working bash script set up to launch specific files with specific programs in the gaming frontend EmulationStation on Windows.
But the frontend routes its actions through a Command Prompt. And when Command is used to run the script through Bash, the Bash shell just opens and then closes immediately.
Here's an image of what shows for the instant before Bash closes.
This is only happening when going through a separate Command Prompt first, such as Windows Command Prompt or Git Command Prompt. Running the script with an appropriate argument directly through the git-bash shell works just fine.
In case you want to see the script for any reason, here it is:
#!/bin/bash
defaultemulaunch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/bsnes_mercury_accuracy_libretro.dll" \"$1\""
emu1names=(\
"(1999) Fire Emblem - Thracia 776.smc")
emu1launch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/snes9x_libretro.dll" \"$1\""
gamename=`basename "$1"`
for index in ${!emu1names[*]}
do
game=${emu1names[index]}
if [ "$game" == "$gamename" ]; then
eval "$emu1launch"
fi
done
eval "$defaultemulaunch"
But it's worth pointing out that this is happening when trying to run any bash script when starting the process from a separate Command Prompt.
Note: Git is installed on the hard drive that houses the emulation frontend (V:)---not in the user directory or programs directory of the system's OS/boot drive (C:). I mention this because git-bash's failure at an apparent "login" step except when launched directly feels like it could be a default filepath issue.
Check if that program would still open/close a Windows when executed from the CMD with:
bash -c '/v/path/to/bash/script'
In your case:
set PATH=V:\Emulation\
set GIT_HOME=V:\Emulation\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%GIT_HOME%\mingw64\bin;%PATH
Then:
cd V:/Emulation/.emulationstation/roms/snes/
bash -c './gamelaunch.sh "./(1990) F-Zero.sfc"'
I usually make a run.bat script which would:
set the correct PATH
launch the correct script
That way, for any of my project, I just type run.
And it runs.

Execute commands on new terminal on cygwin

I am trying to execute a script on cygwin terminal.I want to start a new terminal, close the existing one and run the rest of commands present in the script on new terminal.
I have included the command :
cygstart /bin/bash -li
at the end of my file to open a new terminal of cygwin.
But the rest of commands included after :
cygstart /bin/bash -li
are being executed at the previous cygwin prompt only, not the new one.Even i could not figure out how to close the previous terminal.
Cygstart just executes specified program in the new terminal window. Your script just opens new bash shell with cygstart and continues to work. I think you probably must to divide the script by two parts and call second of them via cygstart if it is really necessary. (Although as far as I know, there is a problem with passing of additional arguments to cygstart command.)

sbt: Can't run interactively from mintty on cygwin

When I run sbt interactively from a DOS shell or from a cygwin bash terminal, it functions just fine. However, when I try to run from my preferred mintty terminal, sbt doesn't respond to my commands until I send it an end of file (control-) and sbt exits.
TERM is set to cygwin when I'm using the bash terminal, and it's xterm when I'm using mintty.
Does anyone know how to fix this?
It ends up that by doing the opposite of this answer, I added the -Djline.terminal=jline.UnixTerminal java option to my sbt startup script to fix the problem.

New Application Process from Bash Shell

I'm relearning UNIX commands to use git on windows using MINGW32.
When I launch a program, for example "$ notepad hello.txt" I can't use the shell again until I close the notepad file or CTRL-C in the shell.
How do I essentially fork a new process so I can use both programs?
Add a & to the end of the command:
notepad hello.txt &
Put an ampersand (&) at the end of the command line. That tells the shell to run the program in the background.
In UNIX, you can hit CTRL-z to suspend the currently running program (Instead of CTRL-c to kill it). Once it's suspended, you can use the 'bg' command to put it in the background. I don't think that will work on Windows, but you can try.
You can also create an alias in your .rc file so you don't have to add the ampersands each time.
I had some trouble doing this in bash on Cygwin though.
I ended up having to create a separate script file and add an alias to point to it.
Script file contents (filename is "dtextpad"):
#!/bin/bash.exe
C:/Program\ Files/TextPad\ 5/TextPad.exe $# &
Alias in my .bashrc:
alias tp='~/include/bin/dtextpad'
Now if I want to open a file in textpad, I can type tp filename

Resources