转义PowerShell中的和(&)符号 [英] Escaping the and (&) sign in Powershell

查看:14
本文介绍了转义PowerShell中的和(&)符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在ffmpeg --header参数内使用 。这在Unix中有效,但在Windows命令提示符中不起作用。所以我想用powershell

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

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

"`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参数之后指定的。

此命令有什么问题?


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

推荐答案

当从cmd.exe调用时,事情变得棘手;这里有一个简化的示例:

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

简而言之:fromcmd.exe/Batch Files,将PowerShell的CLI应视为双引号参数的传递到-Command(-c)

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

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

  • 注意:

    • "..."虽然"..."可以在两个版本中运行,但当从cmd.exe调用时,无法正常运行,尤其是在手边的情况下,这是因为要引用的字符串包含cmd.exe元字符,如&-请参见下文。

    • 但是,来自非外壳上下文,例如<任务计划程序和WindowsRun对话框(WinKey-R),"..."可以很好地工作,这可能是因为它与版本无关,并且与大多数CLI期望"字符的方式一致。要转义。

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


  • PowerShell命令行最好通过参数-c(-Command的缩写,在Windows PowerShell中默认为-Command,但在PowerShell(Core)V6+中默认为-File)作为单个"..."括起来的字符串传递)。

  • 由于PowerShell CLI在命令行解析过程中删除了未转义的"字符,将结果解释为PowerShell代码之前,任何"实例都必须转义(请注意,PowerShell-内部`"用于转义";或者,在"..."字符串的上下文中,""可以使用"")。

  • <[p>"^""..."^""(Windows PowerShell)和""...""(PowerShell(Core)v6+)"..."-c参数确保cmd.exe本身将...解释为位于双引号字符串中,这就是使这些转义表单健壮的原因。

  • 如果在"..."fromcmd.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"' "
    

作为备注:不是像在Bash中那样创建单个字符串;在PowerShell中:

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

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

这篇关于转义PowerShell中的和(&amp;)符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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