New Application Process from Bash Shell - bash

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

Related

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

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.

How do I open a shell but still have access to vim

I am running gvim on Windows 7.
I use this mapping to execute the current file with powershell:
nnoremap <C-q> :! & '%:p'<cr>
It works great except I can't access vim until I close the powershell window. Sometimes I want the shell to remain open so I can run additional commands or I want to access vim with the shell still open so I can check the lines where errors were generated.
Ideally (don't know if this is possible) I want to have an already open shell execute the command. So I always have vim and a shell open (on separate monitors) and I can execute the script in that same shell.
How can I achieve this?
GVIM on Windows has a special :!start command to execute the external command asynchronously; i.e. Vim doesn't wait for its return. Just replace the :! with it. See :help :!start for more information.
On Unix, such special isn't necessary; you can just append & (a shell feature) to execute the command asynchronously.

Run a bash script in cygwin using ./

I have a simple bash script that throw errors on a Windows machine in the Cygwin xterm terminal when I call it like so: ./myscript.bat. It runs fine when I call it like this: /cygdrive/c/cygwin/bin/bash.exe myscript.bat. I am thinking that my shell is not using bash by default. How can I set it to bash so that the next time I open the shell, I can execute my script using ./myscript.bat?
When you execute a file, Windows (or some component within Windows) decides how to execute it based on the extension part of the file name.
Cygwin inherits this functionality, letting you run Windows commands from within Cygwin. Cygwin also implements most of the usual UNIX functionality (running commands based on their content), but the combination of UNIX and Windows semantics can't always be perfectly clean.
The .bat suffix refers to a Windows batch file, so when you try to execute myscript.bat, the system treats it that way rather than as a bash script.
Change the file name from myscript.bat to myscript.bash or myscript.sh -- or just drop the extension altogether (since someone running your script shouldn't need to care how it's written).
There are several other filename extensions you should avoid (like .cmd), depending on how Windows is configured. A few quick experiments show that a .sh extension is safe, but really you don't need to use an extension at all for a shell script.
And, as R Sahu's answer says, you also need to make sure the script has execute permission if you haven't already done so:
mv myscript.bat myscript
chmod +x myscript
You'll probably need to change permissions of the file to make it an executable.
Try
chmod +x myscript.bat
./myscript.bat

Resources