Using BATCH to call a BASH script on CygWin

I am trying to use a batch file to automatically execute a bash script with no luck this far.
The batch script looks like this:

 C:\Cygwin64\bin\bash test.sh
 

I have also tried this:

 C:\Cygwin64\bin\bash "C:\Cygwin64\bin\test.sh"

Needless to say that the windows box has Cygwin installed on it.
Any ideas/suggestions are very much appreciated!

The syntax looks correct.

Please check :
Is the test.sh in unix file format ?
Is your PATH on Windows configured properly to include CYGWIN/bin at the end ?
CYGWIN being path where you installed cygwin default being c:\cygwin\bin

Hope that helps
Regards
Peasant.

If CYGWIN is installed in c:\cygwin\bin and you're trying to run your script that is installed as c:\cygwin\bin\test.sh , is CYGWIN being confused by your test.sh and the CYGWIN test utility both being located in the same directory?

When CYGWIN is installed isn't bash set to run files with .sh as a file extension? If so, would trying to run test from Windows try to invoke c:\cygwin\bin\test or c:\cygwin\bin\test.sh ?

I actually came to realized that in CygWin I must use gawk instead
So I decided to run gawk using CMD. Everything is fine but when I tried to read a variable from a file using a gawk script inside other gawk script I get a warning message.
This is the script I am using:

gawk -v "est=gawk '{ print $1}' C:\minimum.txt" '{ calc=( est / $2)*15 } {print $2, calc}' C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

This is the warning I am getting:

gawk: warning: escape sequence `\m' treated as plain `m'

I tried to escape the path using quotes, double quotes or backslashes with no luck:

gawk -v "est=gawk '{ print $1}' "C:\minimum.txt"" '{ calc=( est / $2)*15 } {print $2, calc}' C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

I just cannot figure it out. Is there any other way to read the variable from the file within gawk ?

The code that you have shown us doesn't make any sense to me.

  1. Please explain in English what you are trying to do.
  2. Please show us the contents of the file C:\minimum.txt .
  3. Please show us the contents of the file C:\cygwin64\User\Output.txt .
  4. Please show us the output you hope to produce in the file C:\cygwin64\User\Output2.txt .

Don

I am calculating volumes based on concentrations. Here, I am "translating" a file that is being generated by one documentation system into a CSV file that will be understood by a robotic unit. The controllers are Windows based boxes (connected by a LAN cable). The idea is to have a BATCH file that will automatically translate and feed the CSV file into the computer controlling the robotic unit

The minimum file will contain a number that will change from run to run -the user will not have access to that file. Something like this:

2.0

I am outputting that number using the following script:

gawk '{ print $1}' C:\minimum.txt" 

I am using the above script to defince variable est

gawk -v "est=gawk '{ print $1}' C:\minimum.txt" 

The rest of my code divides the data generated by the documentation system and multiplies it for a constant ( 15 ), printing the original concentration value and the calculated volume

'{ calc=( est / $2)*15 } {print $2, calc}' C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

The Output file contains hundreds of entries and looks like this:

The Output2 file should look like this:

4.58 6.55022
2.57 11.6732
5.56 5.39568
3.58 8.37989
3.22 9.31677
3.09 9.70874
3.27 9.17431
2.46 12.1951
2.55 11.7647
2.31 12.987
3.85 7.79221
2.62 11.4504
2.76 10.8696
2.06 14.5631
2.69 11.1524

PS. I could do it with not many issues in CygWin but using BATCH will be much easier. Fetching the variable from the minimum file and inputting it in my script handling the calculations is the last step. My batch files works like a charm when I enter the actual number:

gawk -v "est=2" '{ calc=( est / $2)*15 } {print $2, calc}' C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

I am restricted to what CMD allows me to do. I cannot use bash or any other unix approach for this particular task
Thanks in advance for any help tackling this issue

It's been a while since I've run batch scripts, but I don't remember it having a command substitution construct like bash (and other POSIX shell conforming utilities) and there's no need to fire up two invocations of awk (or gawk ) when awk is perfectly capable of reading two input files in one invocation. If batch processes single quotes and double quotes the same way the POSIX shell command language does, try:

gawk 'FNR == NR{est = $1 * 15; next}{print $2, est / $2}' C:\minimum.txt C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

Or, ff your input files are DOS format text files and you want the output to be a DOS format text file, you might need to get rid of <carriage-return> characters from the input lines and add them to the output:

gawk '{sub(/\r/, "")}FNR == NR{est = $1 * 15; next}{printf("%.2f %.6f\r\n", $2, est / $2)}' C:\minimum.txt C:\cygwin64\User\Output.txt > C:\cygwin64\User\Output2.txt

Neither of these have been tested at all, but should come close to what you need.

1 Like

Don
Thanks a TON! Exactly what I was looking for.
I was so obsessed with the idea of using -v that I did not even though about your solution.