Cygwin: Running bash script directly inside dos batch file doesn't work - windows

I've searched over the web and still can't figure it out.
I apologise if this sounds like a lazy whinging cry for help -- I really am at wit's end with this one.
I have a bash script located at:
/cygdrive/k/Linux Scripts/Scripts/filter.sh
I've copied the Cygwin.bat to filter.bat, and changed it as follows:
#echo off
L:
chdir L:\Cygwin\bin
bash --login "/cygdrive/k/Linux Scripts/Scripts/filter.sh amc.txt bmo.txt"
When I run filter.bat by double-clicking on it in Windows Explorer, the console flashes open momentarily and then closes. The script is OK, because it runs from the command line in the Cygwin console.
Is there a way to debug this problem?

Try running the batch file from an already-existing Command Prompt window so you can see any error messages bash might send. I'm guessing it has a problem with "/cygdrive/k/Linux Scripts/Scripts/filter.sh amc.txt bmo.txt" -- as far as it's concerned, that's one argument rather than three. Therefore I would change it to
bash --login "/cygdrive/k/Linux Scripts/Scripts/filter.sh" amc.txt bmo.txt

Related

Git Bash .sh Script on windows to change directory does not work. Basic change directory in .sh script [duplicate]

This question already has answers here:
How to run 'cd' in shell script and stay there after script finishes?
(7 answers)
Closed last year.
Trying to make a script to automate activating a virtual environment for my Django project. I am running 'BIT BASH' on the windows 10 operating system. I have a feeling Windows 10 may be where my issues lie. Could anyone confirm this?
Here is my script which I save as activatevirt.sh.
I run it by starting up the 'bit bash' command line program. Then to run the script,
I enter the line:
sh activatevirt.sh
Commands such as echo work, but for some reason file system navigation commands are what seem to fall apart. This is why I suspect the issue to be the fact I'm doing this in windows 10 whereas others trying to offer help may be running on a Unix system. Please consider this if offering a solution.
If someone could simply give a simple brief bit of code that does nothing more than to change the directory in Bit Bash using a .sh script, I would be very grateful as I need to automate much more than just this, but this is where I am currently stuck. Below here is the code which doesn't work atm!
cd c:
cd virt
cd scripts
source activate
whoami
Errors which show up: line 1 no such file or directory c:
I think you mean "git" bash? Try /C, or possibly /mnt/c, instead of c:.
You can use a single path too:
cd /C/virt/scripts
Or just source it without changing directory:
source /C/virt/scripts/activate
Note unix style (forward) slashes to separate directories, as opposed to Microsoft style backslashes (\).
There's also sh /C/virt/scripts/activatevirt.sh for example.

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.

Suppress command window when running console application on Windows

Is there a way to suppress showing the command window when running a console application on Windows XP?
Details: I am calling a (console-based) program from Vim. I would like to avoid the command window being shown every time I do this.
Try start /B <program name> to start the program without a new window.
Did you try shell.vim?
The xolox#shell#execute() function
This function enables other Vim
plug-ins to execute external commands
in the background (i.e.
asynchronously) without opening a
command prompt window on Windows.
i can't believe no one has suggested simply using :silent
for example, I have the following in my .vimrc (gvim on Win7)
"open windows explorer to the directory of the current file
:map <leader>ex :silent !Explorer %:p:h<CR>
When I didn’t want to see the output of external commands called from the Vim command line, I would prepend them with :silent. However, this results in a command window momentarily flashing on screen when running GVim under MS Windows. After a short while, I got annoyed by this behaviour so I researched alternative solutions (which is how I came across this question).
The best solution I came up with was to use Vim’s inbuilt system function which runs shell commands without opening an external command window. While the output of the shell command is not printed, its exit status is conveniently available via v:shell_error. It also has the advantage that it’s portable across (all) platforms.
Example (the echo statement should print 0 if C:\Windows exists):
call system("dir c:\windows")
echo v:shell_error
You could maybe use some autohotkey script of this kind:
Loop {
WinWait, my command window title
WinHide
}
I was trying to use git-bash as my shell from vim on Windows but having the command prompt open whenever a command was run, as you described. My eventual solution was to install the plugin xolox/vim-shell and add the following snippet to .vimrc:
if has('win32')
set shell=bash\ -c
set shellcmdflag=
set shellxquote='
set shellquote=
set shellredir=>
set noshelltemp "This prevents an external window from opening.
endif
This utility will also do the job:
http://www.ntwind.com/software/utilities/hstart.html

bash script on cygwin - seems to get stuck between consecutive commands.

I am using a bash script to run a number of application (some repeatedly) on a Windows machine through cygwin. The script contains commands to launch those applications, line by line. Most of these applications run for many minutes and many times I have observed that the i+1 th application is not being started even after i th application is completed. In such cases, if I press enter in the cygwin console on which the bash script is running, the next application starts running. Is it because of any issue with bash on cygwin? Or is it an issue with the Windows OS itself? Have any of you observed such an issue with bash + cygwin + Windows?
Thanks.
I think I have seen this before.
Instead of
somecommand
try
somecommand </dev/null
If that doesn't work, try
cmd /c somecommand
Or experiment with other redirections, e.g.
somecommand >/dev/null
Sounds like you may have a problem with your shell script encoding; DOS (and Windows) uses CR+LF line endings, whereas Linux uses LF endings. Try saving the file as LF.
What might also be going on:
When I was running Cygwin on a school laptop, I encountered a dramatic slowing of shell scripts vs. when they were running in a native Linux environment. This was especially apparent when running a configure script from GNU Autotools.
Check your path for slow drives. (From the Cygwin FAQ):
Why is Cygwin suddenly so slow?
If suddenly every command takes a very long time, then something is probably attempting to access a network share. You may have the obsolete //c notation in your PATH or startup files. Using //c means to contact the network server c, which will slow things down tremendously if it does not exist.
You might also want to check whether you have an antivirus program running. Antivirus programs tend to scan every single executable file as it is executed; this can cause problems for even simple shell scripts that run hundreds or even thousands of individual programs before they run their course.
This mailing list post outlines what is needed to pseudo-mount the main /usr/bin directory as cygexec. I'm not sure what that does, but I found it helped.
If you're running a configure script, try the -C option.
Hope this helps!
Occasionally, I'll get this behaviour because I have accidentally deleted the 'she-bang' at the top of the script, that is, deleted the #!/bin/bash on the first line of the script.
It's even more likely for this to happen when a parent shell script calls a child script that has the she-bang missing!
Hope this helps.
A bit of a long shot, but I have seen some similar behaviour previously.
In Windows 2000, if any program running in a command prompt window had some of it's text highlighted by the cursor, it would pause the command running, and you had to press enter or clear the highlighting to get the command prompt to continue executing.
As I said, bit of a long shot, but accidental mouse clicks could be your issue...
Install cygwin with unix style line breaks and forget weird problems like that.
Try saving your script as "the-properly-line-broken-style" for your cygwin. That is, use the style you specified under installation.
Here is some relevant information:
https://stackoverflow.com/a/7048200/657703

Cygwin automatic script launch

Im trying to automatically run a script using Cygwin via CMD. I basically created a BAT file that goes to the directory and executes an .SH file. SH files are accosiated with Cygwin, and I tried something like "cygwin update.sh" in the command line. But all it really does is open Cygwin. I want Cygwin to automatically run the script file. Is there any easy way to do this, I've been trying to find but can't. Thank you!
You'll want to call the shell script with a particular shell, e.g. bash.
When having Cygwin open, call which bash to figure out where the binary is located. Cygwin also comes with tools that can convert paths between Cygwin and Win32 form, which is pretty helpful in cases like yours.
There is one other thing that may work, depending on your setup. There is an environment variable named PATHEXT which declares file extensions that are deemed "executable" by CMD. This can be used to your advantage, if Windows is configured so that the shell's "open" verb executes the correct shell for the file extension .sh (in your case).
Good luck.
From Cygwin Terminal, read man mintty. Try something like the following from a Windows Command Prompt:
c:\cygwin\bin\mintty --hold always --exec /cygdrive/c/path/to/bash/script.sh
I also found this!
http://rothmanshore.com/2011/01/26/kick-off-a-cygwin-script-from-a-windows-bat-file-with-a-different-working-directory/
I didn't quite understand it at first, but then it worked as I wanted it. Just if anyone knows, is there a way to make the script run without the CMD window open?? Thanks

Resources