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

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

问题描述

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

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:UsersMeDocumentsTestingSparseLibinWin32LinuxReleasehello.o" 
"c:UsersMeDocumentsTestingSparseLibSparseLibhello.cpp"

传入的第一个路径周围的第一个引号过早结束了传递给 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 最终想出了答案.批量替换字符串的语法是这样的:

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天全站免登陆