powershell 中的 << EOF 等价物是什么

not*_*bit 7 powershell redirection string

相当于Linux命令的powershell是什么:

tee file.txt <<EOF
Run Code Online (Sandbox Code Playgroud)

终端<input>会是这样的:

blah blah <\n>
beh ble <\n>
EOF
Run Code Online (Sandbox Code Playgroud)

假设我复制上面的 3 行,然后输入一些(尚未知的)powershell 命令。然后粘贴到终端中。(大概是在粘贴操作之后,在输入末尾点击Ctrl- 。)然后希望在文件file.txt中找到所有这些。D

我想我们必须:

  1. $file 的获取终端输入和未知命令A
  2. 等待来自 shell 的管道输入。
  3. 等待Ctrl-D并中断 (EOF) shell 输入。
  4. 使用未知命令B将输入缓冲区通过管道传输到 $file 。

编辑:2020-01-17

我意识到我错过了上面列表中我自己的观点(第 3 点)。重点是不必输入Ctrl-D而是让 shell 查找“EOF”序列,就像上面的 Linux bash 命令一样。这始终在 bash 脚本中完成,用于创建配置文件甚至其他 bash 脚本。这一功能在 PWSH 中也非常有用。

我能够达到上述目的的方法是使用Tee-Object“here”运算符 ( @'...),如下所示:

$ @"
>> Even if you have not created a profile,
>> the path of the profile file is:
>> $profile.
>> "@ | Tee-Object -FilePath "test.txt" -Append
Even if you have not created a profile,
the path of the profile file is:
C:\Users\XXXX\Documents\PowerShell\Microsoft.PowerShell_profile.ps1.
Run Code Online (Sandbox Code Playgroud)

但我想要左侧的T 恤和右侧的Here-is ,作为命令的最后一部分。这基本上会完成等效的命令。Linux 中的here字符串在这个答案中得到了很好的解释。

phu*_*clv 1

字符串可以包含换行符,为什么不使用它呢?

PS D:\> echo "blah blah
>> beh ble" | tee file.txt
blah blah
beh ble
PS D:\> cat .\file.txt
blah blah
beh ble
Run Code Online (Sandbox Code Playgroud)

因此,如果命令是从另一个应用程序复制的,则"首先键入,粘贴字符串并使用另一个". 但为什么不直接打开文本编辑器来粘贴呢?

如果字符串中可以有引号字符,则使用here-string

PS D:\> echo @"
>> here string
>>     with embedded quote '"'
>> "@
here string
    with embedded quote '"'
Run Code Online (Sandbox Code Playgroud)