提问人:Robert Ranjan 提问时间:6/7/2023 最后编辑:VeverkeRobert Ranjan 更新时间:6/7/2023 访问量:166
如何将stderr重定向到NuShell中的文件?
how to redirect stderr to a file in NuShell?
问:
这是我尝试过的
ls | to json | save fileList.json
open fileList.json | from json | save error.log
open fileList.json | from json | save --stderr error.log
它没有保存任何文件error.log
仅供参考,添加了(无用的)第二部分以创建错误消息以保存它。| from json
尝试了以下示例,但没有奏效:help save
> do i {} | save foo.txt --stderr foo.txt
Error: nu::shell::cant_convert
× Can't convert to Closure.
╭─[entry #62:1:1]
1 │ do i {} | save foo.txt --stderr foo.txt
· ┬
· ╰── can't convert string to Closure
╰────
答:
2赞
pmf
6/7/2023
#1
您使用正确,但缺少 nu 的内部命令不会“打印到 stderr”。save
如果要捕获可能失败的内部命令(例如),请使用 和 :ls
try
catch
try { ls nonexistent.file } catch { echo "mishap" | save error.txt }
# or
try { ls nonexistent.file } catch { |e| echo $e.msg | save error.txt }
如果要捕获外部命令(例如 cat)发送到块中的 STDOUT 或 STDERR 的输出,您可以像尝试过的那样使用:do
save
do { cat nonexistent.file } | save out.txt --stderr err.txt
但是使用外部命令,您还可以更直接地使用 、 和 来引导它们的原始流:out>
err>
out+err>
cat nonexistent.file out> out.txt err> err.txt
最后,您可能还有兴趣使用 which 为您提供 、 和 作为记录中的键(然后您可以进一步处理,例如 with 或 etc.):complete
stdout
stderr
exit_code
to text | save …
to json | save …
get stderr | save …
do { cat nonexistent.file } | complete
╭───────────┬──────────────────────────────────────────────────╮
│ stdout │ │
│ stderr │ cat: nonexistent.file: No such file or directory │
│ │ │
│ exit_code │ 1 │
╰───────────┴──────────────────────────────────────────────────╯
评论
do -i {} | …
-i
i
-
-