How to open multiple ConEmu/CygWin Bash terminals at different directories - bash

I have a Windows environment set up using ConEmu and CygWin64 and I frequently have to open up four different Windows directories and run a script there to launch a server.
In total I have these four things running in order to have a local version of our environment operating.
Like so;
C:\code\project1\private-api
C:\code\project2\public-api
C:\code\project2\management-agent
C:\code\project3\back-office
So far I have just been manually opening new ConEmu/CygWin Bash tabs and navigating via command line to each directory I need then running the scripts. This is tedious though and I figure there must be a way to have ConEmu launch multiple CygWin Bash tabs in different, specific directories (and maybe even run a script in each directory) but I am at a loss as to how to accomplish this.
I considered trying to do this via a Windows batch script too but I couldn't figure out how to launch ConEmu/CygWin bash windows from a batch file. I'm thinking it's not possible.
I have followed the documentation for ConEmu Tasks but the docs for Task parameters and commands are still confusing me. I tried following the answer in this question as well with little success;
ConEmu: Open multiple Git Bash tabs on different locations
Anyway, here is how I have my current ConEmu CygWin Bash task set up, which works great when opening single tabs in my home directory (had to provide a link, I keep getting errors when trying to upload an image here);
ConEmu CygWin Bash Task config
Name
Bash::CygWin bash
Task parameters
None
Commands
set CHERE_INVOKING=1 & %ConEmuDrive%\cygwin64\bin\sh.exe --
login -i -new_console:C:"%ConEmuDrive%\cygwin64\Cygwin.ico"
Is it actually possible with a ConEmu/CygWin64/CygWin Bash set up to accomplish what I want?

Tasks allow to run as many tabs/panes as you wish.
Docs explain how to run your shell in specific directory.
Ways to execute some command in bash shell on startup.
Example for two tabs with directories
set CHERE_INVOKING=1 & %ConEmuDrive%\cygwin64\bin\sh.exe -l -i -new_console:d:"C:\code\project1\private-api" -new_console:C:"%ConEmuDrive%\cygwin64\Cygwin.ico"
set CHERE_INVOKING=1 & %ConEmuDrive%\cygwin64\bin\sh.exe -l -i -new_console:d:"C:\code\project2\public-api" -new_console:C:"%ConEmuDrive%\cygwin64\Cygwin.ico"

I know nothing about ConEmu, but you should be able to run a shell script like:
#!/bin/sh
mintty --dir /c/code/project1 --exec ./task1 &
mintty --dir /c/code/project2 --exec ./task2 &
...
or
#!/bin/sh
mintty --exec /c/code/project1/task1 &
mintty --exec /c/code/project2/task2 &
...

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.

How to pipe bash script output to a file on windows

I am running on Windows 7.
I have a bash script which runs fine from a Windows command prompt using "C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1. I can see it running in a new bash window, the output looks fine and it closes. What I want to do is capture the output in a file for further processing.
I tried
"C:\Program Files\Git\git-cmd.exe" "C:\path\myBashScript.sh" 17.1 > C:\out\myBashScript.out.txt
but all I get in the output is the working folder, ie,
C:\path>
Is this possible?
Thanks
"C:\Program Files\Git\git-cmd.exe" is a frontend windows launcher. I'm surprised it takes any argument at all. You should run bash scripts using bash interpreter:
"C:\Program Files\Git\bin\bash.exe" "C:\path\myBashScript.sh"
PS: I'd advise to apply redirection inside the bash script rather than in windows commandline, they are better controlled there.
You need to discriminate between the outputs generated within the shell script, and those generated by the command line execution.
To easily capture the output of the shell script, you may find it easier to do the redirection > to the capture file within the script.
You didn't say what style of system you were on, and how it was set up (Cygwin, GfW; command line in cmd.exe, or bash; integrated or separated git commands (in the cmd.exe), etc). I suspect that the inability to do a simple redirection of the command line is because of that system setup issue.

"source: command not found" running script with git sh via cmder

I'm using cmder on windows https://github.com/bliker/cmder
I created my custom cmder task with the following commands
-new_console:d:C:\project > "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -cur_console:d:C:\project
I need to add another command when this tab opens
source script.sh
But when I add it above in the commands I get
'source' is not recognized as an internal or external command
You can use -c to pass a command to sh to have it run that but I don't believe you can do that and get an interactive session.
Which means if you need a command to be run at the start of an interactive session you want to use the --init-file or --rcfile to specify your startup file (instead of the default file). Though those might both be bash specific. I'm not sure.
If they are then you could try setting the ENV variable to the (absolute or variable/etc. expansion-able) path to your script before running the shell.

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

Can I use cygwin to script a hudson build step?

I've tried executing the following:
#!C:\cygwin\bin\bash.exe
ls ${WORKSPACE}
But that doesn't find ls (even if it's on the windows path). Is there any way to set this up?
UPDATE: In other words, I want to be able to set up a build step that uses cygwin bash instead of windows cmd like this page shows you how to do with Python.
So put your cygwin's bin directory in your PATH.
In case you don't know how to do it (Control Panel -> System -> Advanced -> Environment Variables), see: http://support.microsoft.com/kb/310519
That shell-script has two errors: the hash-bang line should be "#!/bin/bash", and ${WORKSPACE} is not a shell-variable. Hudson has a bunch of variables of its own which are expanded in the commands you specify to be run (i.e. when you add commands in the web gui).
If you want to run Hudson build step on the Cygwin command line, you need to figure out what command Hudson runs and in which directory.
To give a more specific answer, you need to show us how your project is configured and what steps you want to run separately.
Provided cygwin's bin folder is in your path, the following works for me:
#!/bin/sh
ls ${WORKSPACE}
I find Hudson does not pick up environment variable changes unless you restart the server.
you might want to try to give a full path to ls
/cygdrive/c/cygwin/bin/ls
One other thing that seems to work is to use this:
#!C:\cygwin\bin\bash.exe
export PATH=$PATH:/usr/bin
ls
But it would be nice not to have to modify the path for every script.
Have you thought about power shell? as much as I like cygwin, it's always been a little flaky, powershell is a solid fully functional shell on windows, another option is Windows Services for UNIX it gives you korn shell or c shell not quite as nice as bash but it gets the job done
You will need to pass the --login (aka -l) option to bash so that it will source Cygwin's /etc/profile and set up the PATH variable correctly. This will cause the current directory to get changed to the default "home" but you can set the environment variable CHERE_INVOKING to 1 before running bash -l and it will stay in the current directory if you need to preserve that.

Resources