在查看这篇文章之后,我试图逃避引号,但做反斜杠不会逃避这样的html引号:”< - 注意它是一个正确的双引号不同于正常".
例如:
工作良好:
powershell -noexit -command $str = \"hello '123' world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)
不起作用:
powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)
我该怎么逃避这个?或者更好的我如何一次性逃脱所有角色摆脱这个麻烦!
Neo*_*isk 10
使用反引号`来逃避您的特殊双引号,所以这:
powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)
成为这个:
powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str
Run Code Online (Sandbox Code Playgroud)
编辑:
选项1.如果您更喜欢使用here-strings,则可以将上述内容更改为powershell -file并运行您的powershell脚本文件.尽可能多地使用here-strings.
选项2.如果您不想在调用/处理应用程序/脚本中处理特殊引号字符,则可以切换到使用单引号.在这种情况下,你只需要通过将它们加倍来逃避单引号,如下所示:
powershell -noexit -command $str = 'hello ''123''” world'; write-host $str
Run Code Online (Sandbox Code Playgroud)
请注意,我没有对你的双引号字符做任何事情,它工作得很好.
所以你的字符串替换代码可能会变得稍微容易阅读和维护,特别是如果编辑器没有正确显示这个字符(就像Powershell控制台那样 - 它显示了常规的双引号).
小智 5
字符串是通过两边用 4 次引号括起来的文本来识别的: """" text """"
例子:
Run Code Online (Sandbox Code Playgroud)powershell.exe -command """""Recognized as string """""
Output Stream:> Recognized as string
Run Code Online (Sandbox Code Playgroud)对于子表达式中的引号,双引号,即 8 倍引号
例子:
Run Code Online (Sandbox Code Playgroud)powershell.exe -command """""""""How to quote in subexpression"""""""""
Output Stream:> "How to quote in subexpression"
Run Code Online (Sandbox Code Playgroud)从命令提示符运行此命令(实际上是在 PowerShell 中):
powershell -noexit -command { $str = "hello '123' world"; write-host $str }
Run Code Online (Sandbox Code Playgroud)
生成:
hello '123' world
Run Code Online (Sandbox Code Playgroud)
这能满足您的需要吗?
| 归档时间: |
|
| 查看次数: |
23440 次 |
| 最近记录: |