Dump table to console terminal

Hi all,

I want to dump json format content to console. which json format content is write in a lua file, so I want to use luci.util.dumptable this api to show the json content and use luci.util.exec api for redirect the showed content to /dev/console. But I always get error when execute the lua file. Below is my source code.
Can someone help me how to achieve my goal? Many thanks!

local util = require "luci.util"
local table = util.dumptable(node, 100);   //node is a json format table 
util.exec("echo table = " .. table .. "> /dev/console")

What error do you get?

The util.dumptable() prcoedure is a debug function that prints a given Lua table recursively to stderr. It does not return anything.

Even if it would return a somehow formatted Lua table as string, you shouldn't pass it as-is to a shell command, you would need to quote and escape it properly to avoid misinterpretation or command injection by the intermediate shell.

Also invoking a shell to to echo arbitrary input into a specific file is inefficient and error prone. The proper solution is to open the target file and to write into it.

The following, self-contained example illustrates a better approach:

#!/usr/bin/env lua

local io = require "io"
local json = require "luci.jsonc"

local my_table = {
        foo = "bar",
        lala = true,
        nested = { 1, 2, 3 },
        more = {
                data = "foo",
                number = 123
        }
}


local console = io.open("/dev/console", "w")

console:write(json.stringify(my_table, true))
console:close()
3 Likes

I still get nothing by the way you provided. But I see the lua file I debug using io.stderr api which can show on luci System Log webpage or logread command. I'll use this to show the content I want to see.
Thank you for your reply , jow !

It show nothing on console. And I don't know where I can see the error message.

I see the error message on gui, below is the show:

attempt to index local 'console' (a nil value)