【问题标题】:Escaping the and (&) sign in Powershell在 Powershell 中转义和 (&) 符号
【发布时间】:2018-08-10 03:19:28
【问题描述】:

我需要在ffmpeg --header 参数中使用\r\n。这适用于 Unix,但不适用于 Windows 命令提示符。所以我想使用powershell

powershell c:\ffmpeg -headers 'User-Agent: user-agent'"`r`n"'Cookies: my-cookie' ...

我知道当使用特殊字符时我必须使用'string' 并且

"`r`n"

作为我的\r\n 分隔符。

我还测试了我可以将它们混合在一起,比如'this '"is "'text' 得到this is text

但是,如果我的字符串(cookie 或用户代理)包含 & 字符,则会失败。

示例:powershell c:\ffmpeg -headers 'Cookies: param=a=1&b=2; session=123'

如何在单行中“转义”& 字符?


这些示例(某些部分被屏蔽)被 CMD 接受,但它们不起作用

powershell -c "c:\ffplay -user_agent ""Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0"" -headers ""Cookie: nlqptid=h=676886edeea5bae904b0cf53daec8038.1519940538670&hdnea=exp=1519940598~acl=/*~hmac=C0E019502B060D23AB02BB157FCFFC72404500770A2CE5B00789A84AAEFBD77F&nltid=xxxxxxxxxx&nltdt=0&uid=744826&csid=xxxxxxxxxx; hdntl=exp=1520026938~acl=%2f*~hmac=952689e6de57a2a201ddc1d4c0794962fdc886ea48cf41494a42e787ef923bf9`r`n"" -i ""https://xxxxxxxxxx.net/nlds_vod/xxxxxxxxxx/vod/2018/02/28/cd264e10-dd54-3de2-df90-4c1cac104dcb/v1/stream/cd264e10-dd54-3de2-df90-4c1cac104dcb_1_4500_pc.mp4.m3u8"""

powershell -c "c:\ffplay -headers ""User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0`r`nCookie: nlqptid=h=676886edeea5bae904b0cf53daec8038.1519940538670&hdnea=exp=1519940598~acl=/*~hmac=C0E019502B060D23AB02BB157FCFFC72404500770A2CE5B00789A84AAEFBD77F&nltid=xxxxxxxxxx&nltdt=0&uid=744826&csid=xxxxxxxxxx; hdntl=exp=1520026938~acl=%2f*~hmac=952689e6de57a2a201ddc1d4c0794962fdc886ea48cf41494a42e787ef923bf9`r`n"" -i ""https://xxxxxxxxxx.net/nlds_vod/xxxxxxxxxx/vod/2018/02/28/cd264e10-dd54-3de2-df90-4c1cac104dcb/v1/stream/cd264e10-dd54-3de2-df90-4c1cac104dcb_1_4500_pc.mp4.m3u8"""

ffplay

必须指定输入文件

但是输入文件是在-i参数之后指定的。

这个命令有什么问题?


请注意,该实现是现有批处理脚本的一部分,因此需要使用批处理文件内部的语法。

【问题讨论】:

    标签: powershell cmd escaping ampersand quoting


    【解决方案1】:

    当从cmd.exe 拨打电话时,事情变得棘手;这是一个简化的例子:

    powershell -c "ffmpeg.exe -h "^""User-Agent: usra`r`nCookies: parm=a=1&b=2; session=1"^"""
    

    简而言之:cmd.exe / 批处理文件,传递PowerShell 的CLI 应该看到的双引号参数在传递给-Command (-c) 的整体"..." 字符串中

    • Windows PowerShell (powershell.exe) 中:使用 "^""..."^""(原文如此)

    • PowerShell (Core) v6+ (pwsh.exe) 中:使用 ""...""

    • 注意:

      • 虽然 \"...\"两个 PowerShell 版本中都可以使用,但在从cmd.exe 调用时却无法稳定 /strong>,特别是在手头的情况下,由于要引用的字符串包含cmd.exe 元字符,例如& - 见下文。

      • 但是,来自 无 shell 上下文,例如任务计划程序和 Windows Run 对话框 (WinKey-R),\"...\" 运行良好,并且由于与版本无关,并且与 大多数 CLI 期望 " 字符的方式保持一致,因此可能更可取。被逃脱。

    作为一般要求,文字 % 字符。必须将其转义为%%,以防止它们被解释为批处理文件中的cmd.exe 变量引用的一部分。在命令提示符中,things are unfortunately more complicated.


    • PowerShell命令行最好作为单个"..."-封闭字符串,通过参数-c(简称-Command,在 Windows PowerShell 中是默认值,但在 PowerShell (Core) v6+ 中已更改,现在默认为 -File

    • 由于 PowerShell CLI 在命令行解析期间剥离 未转义 " 字符将结果解释为 PowerShell 代码之前,任何 " 要保留的实例作为最终执行的命令的一部分必须转义(注意PowerShell-内部 `" 用于转义";或者,仅在"..." 字符串的上下文中,可以使用""

    • "^""..."^"" (Windows PowerShell) 和 ""..."" (PowerShell (Core) v6+) 在整体 "..." -c 参数中确保cmd.exe 本身将 ... 解释为在双引号字符串中,这就是使这些转义形式健壮的原因。

    • 如果在 "..." 内部使用 \"...\" cmd.exe(它只将 "" 识别为转义的 "),它实际上会将 ... 视为 外部双引号字符串,这将导致包含cmd.exe 元字符(例如&|)的值中断命令。比较来自cmd.exe 的以下调用:

      # OK - prints verbatim: The king of "Rock  &  Roll"
      C:\>powershell.exe -c " 'The king of "^""Rock  &  Roll"^""' "
      C:\>pwsh.exe -c " 'The king of ""Rock  &  Roll""' "
      
      # !! BROKEN - cmd.exe interprets "&" as a metachar.
      C:\>powershell.exe -c " 'The king of \"Rock  &  Roll\"' "
      C:\>pwsh.exe -c " 'The king of \"Rock & Roll\"' "
      

    顺便说一句:'this '"is "'text' 不会像在 Bash 中那样创建单个字符串;在 PowerShell 中:

    • 作为一个独立的表达式,它会导致语法错误(尝试自己执行);您必须使用 ('this ' + "is " + 'text') 或使用 引号字符串 ('this is text')。

    • 作为传递给命令(程序)的 参数,它被解释为 3 个不同的 参数 - 请参阅 this answer 以了解对此 - 令人惊讶的 - 行为的解释。

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多