0

The result of the executed command appears on the screen in the console. how to save it to a txt file. I searched for a solution in various forums, but unfortunately I did not find it.

Help with the implementation of the script.

The lua script executes the command:
core.console('lf em 410x read')
As a result, the screen shows:

-----------------------------------------------------------------------
               Put the EM4102 tag on the coil PM3 to read
-----------------------------------------------------------------------
Press any key to continue . . .
     Read TAG ID:
[+] EM 410x ID B9519AD890

How to save the entire screen to a txt file?

ESkri
  • 1,461
  • 1
  • 1
  • 8

1 Answers1

1

Let's say you have a script.lua which is just

print("hello world")

You have two options to save its output to a file:

Shell

Simply redirect stdout to a file using >file.txt. Assuming you invoke your script as lua script.lua, the command will become lua script.lua >file.txt. This is what I'd recommend as it requires no changes to the script.

Lua

Lua offers io.output(file) to change the output file. This would require changing script.lua to:

io.output(assert(io.open("file.txt", "w"))) -- if this is for binary data, use wb on windows
io.write("hello world\n")

Note that I've changed the print to io.write (and added a manual Linux linefeed; this should be normalized to CRLF on Windows though if you use w rather than wb as mode for the file). From my testing it appears that print ignores changing the output file via io.output. You could implement your own print in terms of io.write:

function print(...)
    local t = {}
    for i = 1, select("#", ...) do
        t[i] = tostring(select(i, ...))
    end
    io.write(table.concat(t, "\t"), "\n")
end

Closing the file is not necessary: It will be closed automatically when the process exits.

Luatic
  • 8,513
  • 2
  • 13
  • 34
  • I would like the screen content to be saved to a txt file. – Jarek Barwinski Aug 21 '23 at 10:41
  • @JarekBarwinski The screen content? Please provide further context. Originally your question contained no mention of a scriptable game whatsoever. Now I see something hinting at a game, but you're still not even mentioning the name of it. – Luatic Aug 21 '23 at 11:01
  • @JarekBarwinski Either way you should be able to override `core.console` using `local core_console = core.console; function core.console(...) print(...) return core.console(...) end` where `print` is the overridden `print` to output both to the screen and to a file. – Luatic Aug 21 '23 at 11:02
  • Any chance for a script? I'm just starting to write something in LUA. – Jarek Barwinski Aug 21 '23 at 11:29
  • Does not work with command output. Log txt is only made if the print command is used. Log is made after the script finishes. I knew it wasn't easy because I couldn't find a solution to the problem anywhere. Saving the screen is by print. In my case, the result of the command is returned, not print. if someone knows how to solve my problem ... I will be very grateful . I try in all ways and I do not know how to dump the terminal screen to a txt file. Any way thanks. – Jarek Barwinski Aug 21 '23 at 15:49