Keep terminal open at the end of bash script - windows

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

Related

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.

Is there a way to launch Terminal with a prefilled statement?

I want to open a terminal window with a statement. Is there a process I can run that will do this?
For example, I want to run a script that 1. Opens Terminal and 2. has the following statement:
java -version
The user then can press the Enter key to run the statement
One way to do this is by wrapping the command in a .command file that contains a command to read something, such as:
#!/bin/bash
command="java -version"
echo $command
read input
exec $command
...then, make the script file executable by running chmod +x filename.command on the file that you put this in, and run it (e.g. from the Finder or by using open filename.command). This should launch the terminal, print the command, and wait for the user to press Enter before running it.
You will note that since this is a script, you can customize any of the steps above to do whatever you want, e.g. printing more stuff or running other commands.

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

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.

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