Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do the equivalent of 2>&1 in Julia

Tags:

shell

julia

Suppose I have a command

`echo hello`

Now, I'd like to redirect STDOUT and STDERR of this to a single stream, so that it's something like 2>&1 in bash. I see two Julia issues, but still don't understand how it's supposed to work in Julia v.0.4.

https://github.com/JuliaLang/julia/issues/5344

https://github.com/JuliaLang/julia/issues/5349

like image 590
m33lky Avatar asked Jan 04 '23 00:01

m33lky


1 Answers

See the help for pipeline, in particular:

run(pipeline(`echo hello`, stdout=STDOUT, stderr=STDOUT))

which will redirect both to the same stream (the process STDOUT). This can be something else also.

Here is the help you can get from the REPL:

help?> pipeline
search: pipeline

  pipeline(command; stdin, stdout, stderr, append=false)

  Redirect I/O to or from the given command. Keyword arguments specify which
  of the command's streams should be redirected. append controls whether file
  output appends to the file. This is a more general version of the 2-argument
  pipeline function. pipeline(from, to) is equivalent to pipeline(from,
  stdout=to) when from is a command, and to pipeline(to, stdin=from) when from
  is another kind of data source.

  Examples:

  run(pipeline(`dothings`, stdout="out.txt", stderr="errs.txt"))
  run(pipeline(`update`, stdout="log.txt", append=true))

  pipeline(from, to, ...)

  Create a pipeline from a data source to a destination. The source and
  destination can be commands, I/O streams, strings, or results of other
  pipeline calls. At least one argument must be a command. Strings refer to
  filenames. When called with more than two arguments, they are chained
  together from left to right. For example pipeline(a,b,c) is equivalent to
  pipeline(pipeline(a,b),c). This provides a more concise way to specify
  multi-stage pipelines.

  Examples:

  run(pipeline(`ls`, `grep xyz`))
  run(pipeline(`ls`, "out.txt"))
  run(pipeline("out.txt", `grep xyz`))

Also, you should upgrade to at least Julia 0.5. 0.4 is no longer supported, and 0.6 will be released shortly.

like image 131
Fengyang Wang Avatar answered Jan 13 '23 13:01

Fengyang Wang