如何将带有空格和引号的命令作为单个参数传递给 CScript? [英] How to pass a command with spaces and quotes as a single parameter to CScript?

查看:30
本文介绍了如何将带有空格和引号的命令作为单个参数传递给 CScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CScript 来运行 VBScript 文件,我需要将命令行传递给脚本,其中参数包含空格和引号.整个命令需要作为一个参数传递给脚本.例如:

I'm using CScript to run a VBScript file, and I need to pass a command line to the script where the parameter includes both spaces and quotes. The entire command needs to be passed as one parameter to the script. For example:

C:\> CScript myscript.vbs //Nologo "cmd.exe /c "dir && del /q *.txt""

请注意,上面的特定命令行只是一个示例,但它确实涉及我要执行的操作(即传递涉及运行 cmd.exe 的命令行).但是,我正在尝试解决一般情况.

Note the specific command line above is just an example, but it does touch on what I'm trying to do (i.e. pass a command line that involves running cmd.exe). I'm trying to solve for the general case, however.

问题是 CScript 似乎在试图确定引号的开始和结束位置时感到困惑.至少有时是这样.

The issue is that CScript appears to be getting confused trying to determine where quotes are opening and closing. At least sometimes.

以下脚本可能有助于解释.它只是输出传入的参数,因为它看到它们:

The following script may help explain. It just outputs the passed in arguments as it sees them:

Dim count
For count = 0 To WScript.Arguments.Count-1
    WScript.Echo CStr(count) & " = " & WScript.Arguments.Item(count)
Next

通常情况下,它会按预期工作:

Often times, it works as expected:

C:\> cscript myscript.vbs //Nologo "1 2 3"
0 = 1 2 3

但是当引用的参数中有引号时,事情就会改变:

But when there are quotes inside the quoted parameter, things change:

C:\> cscript myscript.vbs //Nologo "1 "2 3""
0 = 1 2
1 = 3

我知道在上面的例子中每个引号是打开还是关闭参数存在引入歧义,所以接下来我尝试转义内部引号,以便命令处理器将它们视为文字字符串:

I understand there is an introduced ambiguity about whether each quote is opening or closing a parameter in the above example, so next I tried escaping the internal quotes so the command processor will treat them as literal strings:

C:\> cscript myscript.vbs //Nologo "1 ^"2 3^""
0 = 1 ^2
1 = 3

嗯,看起来这只有在 shell 为您进行通配等操作时才有效.我怀疑 cscript 将 ^ 理解为像 cmd.exe 那样的转义字符.

Hmmm, looks like that only works when the shell does your globbing and such for you. I doubt cscript understands ^ as an escape character like cmd.exe does.

在双引号命令中使用单引号(例如cmd.exe/c 'dir && cd \'")可以正确解析为单个参数,但在调用 cmd 的情况下不起作用.exe 与/c 参数,单引号是不够的.例如:

Using single quotes inside the double-quoted command (e.g. "cmd.exe /c 'dir && cd \'") properly parses as a single parameter but won't work because for the case of invoking cmd.exe with the /c parameter, single quotes are not sufficient. For example:

C:\> cmd /c 'dir && cd \'
''dir' is not recognized as an internal or external command,
operable program or batch file.

我想我可以遍历脚本中的所有参数并将它们连接回一个长字符串,但是我会丢失它们提供的内部引号和上下文.或者也许我确实在双引号参数中使用了单引号,并将它们映射回脚本中的双引号.但是,如果我不考虑的未来案例的意图单引号怎么办?由于我正在尝试解决一般情况,因此我无法做出决定.

I suppose I could just iterate over all the arguments in the script and join them back to one long string, but I'll lose the internal quotes and context that they provide doing that. Or maybe I do use single quotes inside the the double-quoted param and map those back to double quotes inside the script. But what if the intention was single quotes for some future case I'm not considering? Since I'm attempting to solve for the general case, I can't make that determination.

有什么想法吗?

推荐答案

您似乎正在尝试描述参数中的值.我建议使用引号之外的其他内容并重新添加引号.例如使用波浪线:

It appears like you are trying to delineate values in your parameters. I suggest using something besides quotes and adding back the quotes. For example using tildes:

Dim count
For count = 0 To WScript.Arguments.Count-1
    WScript.Echo CStr(count) & " = " & replace(WScript.Arguments.Item(count),"~",chr(34))
Next

通过:

cscript myscript.vbs //Nologo "1 ~2 3~"
0 = 1 "2 3"

这篇关于如何将带有空格和引号的命令作为单个参数传递给 CScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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