Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run/invoke windows batch script from sh or bash script

I have a sh/bash script that needs to call a batch file with parameters (parameters are file names and are given in DOS/Windows format).

Basically I have: script.sh

#!/bin/sh
declare var1=$1
declare var2=$2
dosomething var1 var2
...
<invoke batch script> var1 var2
...
dosomethingelse

I'm using GNU bash, version 3.1.0(3)-release (i686-pc-msys) as the shell, on msysgit

The problem is that when i run from script: $COMSPEC /c batchfile param1 param2 either I get an "empty prompt" which looks like bash, but no command result is displayed on the console, either cmd.exe start, but doesn't execute the script.

I've tried quoting the params to bash like this:

$COMSPEC /c \"batchfile param1 param2\"
$COMSPEC /c \"\"batchfile param1 param2\"\"
$COMSPEC /c \"\"batchfile \"param1\" \"param2\"\"\"

But I didn't get any result.

like image 551
Bogdan Maxim Avatar asked Aug 03 '10 09:08

Bogdan Maxim


People also ask

How do I run a batch file from a shell script?

Batch files can be run by typing "start FILENAME. bat". Alternately, type "wine cmd" to run the Windows-Console in the Linux terminal. When in the native Linux shell, the batch files can be executed by typing "wine cmd.exe /c FILENAME.

Can same shell script file run on both Windows and Linux?

Or may be you write programs in Java and for each operating system you have a script to set up an environment (sh-script or cmd-script). If you need to do basically the same in Windows and in Linux, you can write only one script, that will work in both operating systems.

How do I run a shell script from the command line in Windows?

Execute Shell Script Files Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output.


1 Answers

It seems that I needed to escape the space from the cmd param:

$COMSPEC \/c batch-file\ \"$var1\"\ \"$var2\"

or

$COMSPEC /c batch-file\ \"$var1\"\ \"$var2\"

I'm not sure whether the / from /c needs to be escaped, but it works fine both ways escaped.

like image 191
Bogdan Maxim Avatar answered Sep 20 '22 19:09

Bogdan Maxim