Windows 批处理脚本开始行中的双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2254983/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:55:16  来源:igfitidea点击:

Double-Quotes in start line of Windows Batch script

windowsbatch-fileescaping

提问by Anthony

I have looked at the answers already provided, but I'm still stuck. Here is what I currently have:

我已经查看了已经提供的答案,但我仍然被卡住了。这是我目前拥有的:

 start "" "C:\Program Files (x86)\Spark\Spark.exe"
 echo Spark started

This works fine. But now I want to pass parameters to the client, which must be wrapped in quotes. I can do the following on the command line:

这工作正常。但是现在我想将参数传递给客户端,必须用引号括起来。我可以在命令行上执行以下操作:

 "C:\Program Files (x86)\Spark\Spark.exe" "user=%USERNAME%&server=example.org"

And it starts up with the user and server fields filled in.

它以填充的用户和服务器字段启动。

But when I try to edit the batch script to add those quote-wrapped parameters, I get a variety of errors depending on how I try to add double-quotes and where, etc.

但是当我尝试编辑批处理脚本以添加那些引号包裹的参数时,根据我尝试添加双引号的方式和位置等,我会收到各种错误。

So how would I add a quote-wrapped parameter to the start line?

那么我如何将引号包裹的参数添加到起始行?

Update:

更新:

I accidentally got it to work, but haven't been able to reproduce it. But it didn't work exactly right. The user name was still blank, but the server was filled in. I forgot to mention that I am using an environment variable for the user name: %USERNAME%

我不小心让它工作了,但一直无法重现它。但它并没有完全正确。用户名还是空的,但是服务器是填的。 忘了说我用的用户名是环境变量: %USERNAME%

So it might be my problem is that I can't escape quotes and use environment variables?

所以可能是我的问题是我无法转义引号并使用环境变量?

Final Answer:

最终答案:

It turns out that part of the problem was that I was using the wrong parameter, but originally I had been using the right one, so I didn't notice. From the command line, I should have:

原来问题的一部分是我使用了错误的参数,但最初我一直在使用正确的参数,所以我没有注意到。从命令行,我应该有:

 "C:\Program Files (x86)\Spark\Spark.exe" "username=%USERNAME%&server=example.org"

and so from the batch file, the following works:

因此从批处理文件中,以下工作:

 start "" "C:\Program Files (x86)\Spark\Spark.exe" "username=%USERNAME%&server=example.org"
 echo Spark started

Much thanks and points to dcp for getting me to the right answer.

非常感谢并指出 dcp 让我得到正确答案。

采纳答案by dcp

Did you try this?

你试过这个吗?

"C:\Program Files (x86)\Spark\Spark.exe" "\"user=foo&server=example.org\""

This worked when I tried a simple command line test and c++ program (I could see the quotes when I printed the argv[1] argument).

当我尝试一个简单的命令行测试和 c++ 程序时(当我打印 argv[1] 参数时我可以看到引号),这很有效。

UPDATE:If %USERNAME% has spaces in it, then you need to quote it like this (see below). I think you can remove the other quotes.

更新:如果 %USERNAME% 中有空格,那么您需要像这样引用它(见下文)。我认为您可以删除其他引号。

"C:\Program Files (x86)\Spark\Spark.exe" user="%USERNAME%"&server=example.org