Execute commands on new terminal on cygwin - shell

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

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.

Keep terminal open at the end of bash script

I'm trying to run a bash script from cmd. When I execute the script a new terminal is opened an immediately closed since there is some problem with it. Because its happening so fast I can't read the problem. I'm looking for a way to keep the terminal open once the script exits.
Go horribly Windows-y with this:
read -p "Press any key to continue" x
You can also put a $SHELL in a new line at the end of your script. The window will stay open, no matter from where you open your shell script (e.g. cmd / powershell / etc).
Add bash at the end of the script
It works for me.
to test a .sh script in windows cmd (assuming you have bash installed and in your path environment variable):
cd into parent directory,
type "bash" to enter bash console,
type "./",
type "exit" to exit bash console

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

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.

Windows shortcut to run a Git Bash script

Assuming I have a test.sh script that runs a server and Git Bash installed, how do I create a Windows shortcut that I can double click to run tesh.sh in Git Bash in the foreground and allows me to see the output of the server?
Git bash is already a batch file with content similar to this :
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i"
If you want run (and leave running) a shell script in the context of the shell, specify it at the command line. The trick is that when the script file name is interpreted, it uses the Windows path, not the equivalent path in the sh/Git environment.
In other words, to run the file D:\temp\test.sh in the Git shell and leave it running, create this batch file :
C:\WINNT\system32\cmd.exe /c ""C:\Git\bin\sh.exe" --login -i -- D:\temp\test.sh"
On the other hand, if you want to run a script and get your shell back, you should :
Open the shell as is
Edit or create ~/.profile (try vi ~/.profile)
Add this line : ~/test.sh (ajdust the path if needed)
So with a .profile that looks like this :
echo Executing .profile
/bin/sh ~/test.sh
And test.sh that looks like this :
echo Hello, World!
You will get this prompt :
Welcome to Git (version 1.7.11-preview20120710)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
Executing .profile
Hello, World!
ixe013#PARALINT01 ~
$
Other answers work, but there is a shorter solution, that fully answers the question, which was:
How to create a Windows shortcut that I can double click to run
tesh.sh in Git Bash
The answer is: add the following command to the Target: field of the shortcut:
"C:\Git\bin\sh.exe" -l "D:\test.sh"
Where, -l is the short for --login.
To better understand what this command does, consult with official GNU docs about Invoking Bash:
-l (--login): Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent
to starting a login shell with exec -l bash. When the shell is
not interactive, the login shell startup files will be executed.
exec bash -l or exec bash --login will replace the current
shell with a Bash login shell.
Also note that:
You either need the full path to sh.exe or have it in your PATH environment variable (as others have already pointed out).
If you really need to force shell invocation in interactive mode, you can add the -i option
The last parameter is the path to the script that has to be executed. This path should be in Windows format.
Best solution in my opinion:
Invokes the right shell
No unnecessary windows
Invokes a bash script afterwards
Window will stay open after the script exits
Do the following:
Create a shortcut to mintty.exe on your desktop, for example. It is found under %installation dir%/Git/usr/bin/mintty.exe
Edit properties of the shortcut and change the target (keep the path):
"C:\Program Files\Git\usr\bin\mintty.exe" -h always /bin/bash -l -e 'D:\folder\script.sh'
Explanation of the parameters:
-h always keeps the window open when the script finished, so the window won’t disappear while you are still reading the output (remove if you don’t need to read the output and want the window to close automatically).
-l makes this shell act as if it had been directly invoked by login.
-e exits immediately if a pipeline returns a non-zero status (more info).
I'd recommend to use environment variable %ComSpec%, instead of absolute path to cmd:
%ComSpec% /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
or even just cmd command, which is usually available from %PATH%:
cmd /c ""C:\Program Files (x86)\Git\bin\sh.exe" --login -i"
if your C:\Program Files (x86)\Git\bin added to PATH (which is also common solution and one of cases on TortoiseGit installing) you can use just:
cmd /c "sh --login -i"

Resources