【问题标题】:Where does PowerShell get the newline characters it puts in here strings?PowerShell 从哪里获取它放入此处字符串的换行符?
【发布时间】:2018-03-14 22:26:54
【问题描述】:

考虑以下 PowerShell 代码:

@"


"@.GetEnumerator() | %{[int]$_}

在我的电脑上输出

13
10

它们分别是用于回车和换行的 ASCII 控制字符的十进制表示。

The same code executed on AppVeyor outputs just a single number:

10

换句话说,PowerShell 在此处字符串中使用的字符之间似乎存在差异。我预计来源是[System.Environment]::newlinethe same environment AppVeyor environment that output the single character in the here string, output

13
10

[System.Environment]::newline[System.Environment]::newline 似乎不是此处字符串中换行符的来源。

【问题讨论】:

  • 它只占用了字符串中的任何内容。 "@`"`n`n`n`"@.GetEnumerator() | %{[int]`$_}" | sc Test1.ps1; "@`"`n`r`n`n`"@.GetEnumerator() | %{[int]`$_}" | sc Test2.ps1; .\Test1.ps1 <#10#>; .\Test2.ps1 <#13, 10#>
  • 谢谢@PetSerAl。默认情况下,它看起来像 "AppVeyor is checking out files with LF (\n) endings on Windows"

标签: powershell newline appveyor herestring


【解决方案1】:

PowerShell 脚本本质上是一个字符串。而且它已经必须使用"`n""`r`n" 进行行分隔。因此,如果 PowerShell 字符串文字跨多行,则 PowerShell 解析器会保留在此字符​​串文字中使用的行分隔符,作为字符串文字值的一部分。

例如:

"'a`nb`r`nc'.GetEnumerator() | %{[int]`$_}" | Set-Content .\Test.ps1
.\Test.ps1

将打印:

97
10
98
13
10
99

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2014-07-05
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多