如何将ECHO JSON内容与EXE文件相呼应,然后将输出保存到文件中?
我正在使用Git Bash中的脚本,该脚本对HTTP端点的curl调用很少,期望和产生Protobuf。
卷发输出被管道输送到自定义proto2json.exe文件,最后将结果保存到JSON文件:
#!/bin/bash
SCRIPT_DIR=$(dirname $0)
JSON2PROTO="$SCRIPT_DIR/json2proto.exe"
PROTO2JSON="$SCRIPT_DIR/proto2json.exe"
echo '{"key1":"value1","version":3}' | $JSON2PROTO -v 3 > request.dat
curl --insecure --data-binary @request.dat --output - https://localhost/protobuf | $PROTO2JSON -v 3 > response.json
脚本运行良好,现在我试图将其移植到PowerShell:
$SCRIPT_DIR = Split-Path -parent $PSCommandPath
$JSON2PROTO = "$SCRIPT_DIR/json2proto.exe"
$PROTO2JSON = "$SCRIPT_DIR/proto2json.exe"
@{
key1 = value1;
version = 3;
} | ConvertTo-Json | &$JSON2PROTO -v 3 > request.dat
不幸的是,当我将生成的二进制文件比较中的脚本“ git bash”和powershell,然后我看到后一个文件输入了额外的零字节。
github问题#1908 与我的问题有关吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您最终是在此之后的:
至于您尝试了:
在PowerShell中,
>
是of-file-file
CMDLET Windows PowerShell中编码的默认输出字符是“ Unicode”(UTF -16LE)
- 它是您所看到的 - 以及在PowerShell(Core)7+中,Bom -bom -less utf8。要控制字符编码,请调用
out-file
或,对于文本输入,set-content
withincoding
参数。请注意,您可能还必须确保首先正确地将外部程序的输出正确地解码,这是基于存储在
[console] :: Outputencoding中的编码 - 参见此答案有关更多信息。
请注意,您无法避免使用PowerShell中的这些解码 +重新编码步骤,因为 PowerShell管道目前无法用作RAW BYTES 的管道,因为在
最后,请注意,
offile
和set-content
在默认情况下,请附加 tailting a 平台本地newline to输出文件。而-noneWline
抑制它,它也抑制了多个输入对象之间的newlines ,因此您可能必须使用-join
操作员以newlines手动加入所需的输入格式,例如
(1,2)-join“ n” | set -content -nonewline out.txt
[system [system]。 io.file] :: writealllines()
将数组作为行的元素写入输出文件,每行用平台本地newline 终止,即(
0xd
0xa
)在Windows上,LF(0xa
)在Unix-like平台上。[system] io.file] :: writealltext()
写入a 单(潜在的多行)字符串 as-is-is-is 将其写入输出文件。<<<<<<<<<<<<<<<< /p>
重要:始终通过完整路径 与文件相关的.net API,因为PowerShell的当前位置(目录)通常与.NET的不同。
It looks like you're ultimately after this:
As for what you tried:
In PowerShell,
>
is an effective alias of theOut-File
cmdlet, whose default output character encoding in Windows PowerShell is "Unicode" (UTF-16LE) - which is what you saw - and, in PowerShell (Core) 7+, BOM-less UTF8. To control the character encoding, callOut-File
or, for text input,Set-Content
with the-Encoding
parameter.Note that you may also have to ensure that an external program's output is first properly decoded, which happens based on the encoding stored in
[Console]::OutputEncoding
- see this answer for more information.Note that you can't avoid these decoding + re-encoding steps in PowerShell as of v7.2.4, because the PowerShell pipeline currently cannot serve as a conduit for raw bytes, as discussed in this answer, which also links to the GitHub issue you mention.
Finally, note that both
Out-File
andSet-Content
by default append a trailing, platform-native newline to the output file. While-NoNewLine
suppresses that, it also suppresses newlines between multiple input objects, so you may have to use the-join
operator to manually join the inputs with newlines in the desired format, e.g.(1, 2) -join "`n" | Set-Content -NoNewLine out.txt
If, in Windows PowerShell, you want to create UTF-8 files without a BOM, you can't use a file-writing cmdlet and must instead use .NET APIs directly (PowerShell (Core) 7+, by contrast, produces BOM-less UTF-8 files by default, consistently). .NET APIs do and always have created BOM-less UTF-8 files by default; e.g.:
[System.IO.File]::WriteAllLines()
writes the elements of an array as lines to an output file, with each line terminated with a platform-native newline, i.e. CRLF (0xD
0xA
) on Windows, and LF (0xA
) on Unix-like platforms.[System.IO.File]::WriteAllText()
writes a single (potentially multi-line) string as-is to an output file.Important: Always pass full paths to file-related .NET APIs, because PowerShell's current location (directory) usually differs from .NET's.