简体   繁体   中英

In Lua, how to print the console output into a file (piping) instead of using the standard output?

I workin' with Torch7 and Lua programming languages. I need a command that redirects the output of my console to a file , instead of printing it into my shell. For example, in Linux, when you type:

$ ls > dir.txt

The system will print the output of the command "ls" to the file dir.txt, instead of printing it to the default output console. I need a similar command for Lua. Does anyone know it?

[EDIT] An user suggests to me that this operation is called piping . So, the question should be: "How to make piping in Lua?"

[EDIT2] I would use this # command to do:

$ torch 'my_program' # printed_output.txt

Have a look here -> http://www.lua.org/pil/21.1.html

io.write seems to be what you are looking for.

Lua has no default function to create a file from the console output. If your applications logs its output -which you're probably trying to do-, it will only be possible to do this by modifying the Lua C++ source code.

If your internal system has access to the output of the console, you could do something similar to this (and set it on a timer, so it runs every 25ms or so):

dumpoutput = function()
    local file = io.write([path to file dump here], "w+")
    for i, line in ipairs ([console output function]) do
        file:write("\n"..line);
    end
end

Note that the console output function has to store the output of the console in a table . To clear the console at the end, just do os.execute( "cls" ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM