从 sh 或 bash 脚本运行/调用 Windows 批处理脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3395374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run/invoke windows batch script from sh or bash script
提问by Bogdan Maxim
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).
我有一个 sh/bash 脚本,需要调用带参数的批处理文件(参数是文件名,以 DOS/Windows 格式给出)。
Basically I have: script.sh
基本上我有: script.sh
#!/bin/sh
declare var1=
declare var2=
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
我正在使用GNU bash, version 3.1.0(3)-release (i686-pc-msys)作为外壳,在msysgit
The problem is that when i run from script:
$COMSPEC /c batchfile param1 param2either I get an "empty prompt" which looks like bash, but no command result is displayed on the console, either cmd.exestart, but doesn't execute the script.
问题是,当我从脚本运行时:
$COMSPEC /c batchfile param1 param2要么我得到一个看起来像 bash 的“空提示”,但控制台上没有显示命令结果,要么cmd.exe启动,但不执行脚本。
I've tried quoting the params to bash like this:
我试过像这样引用参数来 bash:
$COMSPEC /c \"batchfile param1 param2\"
$COMSPEC /c \"\"batchfile param1 param2\"\"
$COMSPEC /c \"\"batchfile \"param1\" \"param2\"\"\"
But I didn't get any result.
但我没有得到任何结果。
回答by Bogdan Maxim
It seems that I needed to escape the space from the cmd param:
看来我需要从 cmd 参数中转义空间:
$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.
我不确定 / 来自 /c 是否需要转义,但两种转义方式都可以正常工作。

