问题描述
最近我一直在使用GnuWin32从PowerShell使用GnuWin32每当双引号涉及。
Recently I have been having some trouble using GnuWin32 from PowerShell whenever double quotes are involved.
进一步调查,似乎PowerShell剥离双引号从命令行参数,即使正确转义。
Upon further investigation, it appears PowerShell is stripping double quotes from command line arguments, even when properly escaped.
PS C:\Documents and Settings\Nick> echo '"hello"'
"hello"
PS C:\Documents and Settings\Nick> echo.exe '"hello"'
hello
PS C:\Documents and Settings\Nick> echo.exe '\"hello\"'
"hello"
双引号传递到PowerShell的 echo cmdlet,但是作为参数传递给 echo.exe 时,双引号被剥离,除非用反斜杠转义PowerShell的转义字符是反引号,而不是反斜杠。)
Notice that the double quotes are there when passed to PowerShell's echo cmdlet, but when passed as an argument to echo.exe, the double quotes are stripped unless escaped with a backslash (even though PowerShell's escape character is a backtick, not a backslash).
这看起来像是一个bug。如果我将正确的转义字符串传递给PowerShell,则PowerShell应该处理任何转义可能是必要的,因为它调用该命令。
This seems like a bug to me. If I am passing the correct escaped strings to PowerShell, then PowerShell should take care of whatever escaping may be necessary for however it invokes the command.
这里发生了什么?
现在,修复是根据这些规则转义命令行参数(似乎被 CreateProcess
PowerShell用于调用.exe文件的API调用):
For now, the fix is to escape command line arguments in accordance with these rules (which seem to be used by the CreateProcess
API call which PowerShell uses to invoke .exe files):
- 要传递双引号,使用反斜杠转义:
\
- > - 传递一个或多个反斜杠双引号,将每个反斜杠转换为另一个反斜杠并转义引号:
\\\\\
- >\ \\
- 如果后面没有双引号,则不必对反斜杠进行转义:
\\
- >\\
- To pass a double quote, escape with a backslash:
\"
->"
- To pass a one or more backslashes followed by a double quote, escape each backslash with another backslash and escape the quote:
\\\\\"
->\\"
- If not followed by a double quote, no escaping is necessary for backslashes:
\\
->\\
请注意,
以下是一些示例,其中 echo.exe 可以用来将Windows API转义字符串中的双引号转换为PowerShell。 GnuWin32:
Here are some examples, with echo.exe from GnuWin32:
PS C:\Documents and Settings\Nick> echo.exe "\`""
"
PS C:\Documents and Settings\Nick> echo.exe "\\\\\`""
\\"
PS C:\Documents and Settings\Nick> echo.exe "\\"
\\
如果你需要传递一个复杂的命令行参数,可以迅速变得地狱。当然,在 CreateProcess()
或PowerShell文档中没有记录。
I imagine that this can quickly become hell if you need to pass a complicated command line parameter. Of course, none of this documented in the CreateProcess()
or PowerShell documentation.
必须将带有双引号的参数传递给.NET函数或PowerShell cmdlet。为此,您只需要将双引号转义到PowerShell。
Also note that this is not necessary to pass arguments with double quotes to .NET functions or PowerShell cmdlets. For that, you need only escape your double quotes to PowerShell.
推荐答案
这是一个:
...没有单一的方法来编写命令行来正确地调用这个应用程序,所以即使你掌握了所有4或5种不同方式的引用和转义的东西,你仍然猜测什么将工作,当...或者,你可以只是shell out的cmd,并完成它。
...there is no single way to write a command line to invoke this application correctly, so even after you master all 4 or 5 different ways of quoting and escaping things, you're still guessing as to which will work when ... or, you can just shell out to cmd, and be done with it.
这篇关于PowerShell从命令行参数中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!