1

Here is what I tried

ls | to json | save fileList.json
open fileList.json | from json | save error.log
open fileList.json | from json | save --stderr error.log

It didn't save anything to file error.log

FYI, added the (useless)second part | from json to create error message to save it.

Tried below example from help save but didn't work:

> 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
   ╰────

related question on saving stdout to a file

Veverke
  • 9,208
  • 4
  • 51
  • 95
Robert Ranjan
  • 1,538
  • 3
  • 19
  • 17
  • 1
    Note: The example from the manual, `do -i {} | …`, uses `-i` as argument (you have provided `i` without the dash, making it just a string). – pmf Jun 07 '23 at 12:04
  • thanks @pmf, without the `-` it works. Not sure, why they put the (confusing) `-` there in an example. It will trip many as more people start using the shell. – Robert Ranjan Jun 07 '23 at 16:08

1 Answers1

2

You are using save correctly, but you are missing that nu's internal commands do not "print to stderr".

If you want to catch an internal command (e.g. ls) that might fail, use try and catch:

try { ls nonexistent.file } catch { echo "mishap" | save error.txt }
# or
try { ls nonexistent.file } catch { |e| echo $e.msg | save error.txt }

If you want to capture the output that an external command (e.g. cat) has sent to STDOUT or STDERR in a do block, you can use save just like you have tried:

do { cat nonexistent.file } | save out.txt --stderr err.txt

But with external commands you can more directly also use out>, err>, and out+err> to channel their raw streams:

cat nonexistent.file out> out.txt err> err.txt

Finally, you might also be interested in using complete which provides you with stdout, stderr, and exit_code as keys in a record (which you can then further process like with to text | save … or to json | save … or get stderr | save … etc.):

do { cat nonexistent.file } | complete
╭───────────┬──────────────────────────────────────────────────╮
│ stdout    │                                                  │
│ stderr    │ cat: nonexistent.file: No such file or directory │
│           │                                                  │
│ exit_code │ 1                                                │
╰───────────┴──────────────────────────────────────────────────╯
pmf
  • 24,478
  • 2
  • 22
  • 31