Skip to content

Created I/O redirection

kingoftheconnors requested to merge kingoftheconnors/apolo:io_redirection into master

apolo.run sends the process’s output to the output stream, pushes errors to the error stream and reads its input from the input stream. This set of modifiers allows the user to redirect all of the above stream to files or to other streams:

Executing the command as run.to will write the output to a file instead of to stdout. If the file already exists, its contents will be overwritten:

    run.to("dir_files.txt")('ls -l')

Executing the command as run.append_to will append the output to a file instead of to stdout. If the file doesn’t exist, it will be created, and if it does exist the output will be appended to the end of the file. If both .to and .append_to are used, the program will default to using .to and overwrite the file.

Executing the command as run.from will get the input from a file instead of from stdin:

    run.from("programming-in-lua.txt")('grep "function"')

Executing the command as run.err_to will write the error stream (stderr) to a file. If the file already exists, its contents will be overwritten, just like .to.

Executing the command as run.append_err_to will append the error stream (stderr) to a file. It works like .append_to.

Executing the command as run.err_to_out will append the error stream (stderr) of all processes to the output stream (stdout). Modifiers like .err_to will not take the error stream into account anymore, while modifiers like .out_to will:

    run.err_to_out("lua purposefully_broken_program.lua")
    run.err_to_out.out_to("err_log.txt")("lua purposefully_broken_program.lua")

Be careful when using this modifier on a pipe, as multiple piped processes may write their errors to out at the same time:

    run.err_to_out.pipe("lua purposefully_broken_program.lua", "lua other_broken_program.lua")

Executing the command as run.out_to_err will append the output stream (stdout) of the process (or the last process in the pipe, if there is one) to the error stream (stderr). Similar to err_to_out, this modifier does not work with out_to but does work with err_to.

Linux Notes

If a file being written to doesn't exist, a file will be created. The created file will have read, write and execution permissions for the current user only. In chmod terms, this is equivalent to 700.

Edited by kingoftheconnors

Merge request reports