Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect stdout to both file and console?

Tags:

julia

Don't have no idea how to do it. How can I collect info to a txt file and print to terminal? I need it to output to txt file and to terminal Don't have no idea how to do it

like image 246
OskanZ Avatar asked Oct 29 '25 00:10

OskanZ


1 Answers

What you want is the tee shell function. You could emulate it with a function based on julia's print function method that allows you to specify the IO destination:

function teeprint(io1::IO, io2::IO, xs...)
    print(io1, xs...)
    print(io2, xs...)
end

Used this way:

fp = open("temptext.txt", "w")

lines = split("""
print([io::IO], xs...)

  Write to io (or to the default output stream stdout if io is not given) 
  a canonical (un-decorated) text
  representation. The representation used by print includes minimal 
  formatting and tries to avoid Julia-specific
  details.
""", "\n")

for line in lines
    teeprint(fp, stdout, line)
    teeprint(fp, stdout, "\n")
end

close(fp)
like image 123
Bill Avatar answered Oct 30 '25 15:10

Bill