转义批处理脚本双引号 [英] Escaping Double Quotes in Batch Script

查看:1660
本文介绍了转义批处理脚本双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何去使用转义双引号替换我的批处理文件的参数,所有的双引号?这是我目前的批处理文件,它扩展所有的字符串中的命令行参数:

How would I go about replacing all of the double quotes in my batch file's parameters with escaped double quotes? This is my current batch file, which expands all of its command line parameters inside the string:

@echo off
call bash --verbose -c "g++-linux-4.1 %*"

它然后使用该字符串作出Cygwin的bash的一个电话,执行一个Linux交叉编译器。不幸的是,我越来越喜欢这些传递到我的批处理文件中的参数:

It then uses that string to make a call to Cygwin's bash, executing a Linux cross-compiler. Unfortunately, I'm getting parameters like these passed in to my batch file:

"launch-linux-g++.bat" -ftemplate-depth-128 -O3 -finline-functions 
-Wno-inline -Wall  -DNDEBUG   -c 
-o "C:\Users\Me\Documents\Testing\SparseLib\bin\Win32\LinuxRelease\hello.o" 
"c:\Users\Me\Documents\Testing\SparseLib\SparseLib\hello.cpp"

在哪里各地传递的第一条路径的第一次报价是prematurely结束的字符串传递给GCC,并直接传递参数的其余部分的bash(这壮观的失败。)

Where the first quote around the first path passed in is prematurely ending the string being passed to GCC, and passing the rest of the parameters directly to bash (which fails spectacularly.)

我想,如果我可以连接的参数成一个字符串,然后逃逸,应该做工精细的报价,但我难以决定如何做到这一点。有谁知道?

I imagine if I can concatenate the parameters into a single string then escape the quotes it should work fine, but I'm having difficulty determining how to do this. Does anyone know?

推荐答案

谷歌最终想出了答案。对于字符串替换批量的语法是这样的:

Google eventually came up with the answer. The syntax for string replacement in batch is this:

set v_myvar=replace me
set v_myvar=%v_myvar:ace=icate%

将会产生复制我。我的脚本现在看起来是这样的:

Which produces "replicate me". My script now looks like this:

@echo off
set v_params=%*
set v_params=%v_params:"=\"%
call bash -c "g++-linux-4.1 %v_params%"

这会替换 \\,正确转义对于bash所有实例。

Which replaces all instances of " with \", properly escaped for bash.

这篇关于转义批处理脚本双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆