【发布时间】:2019-10-25 08:16:32
【问题描述】:
我正在尝试将函数的输出分配给变量,但我做不到。 :(
我使用 Lua 5.3.4 和 luarocks 3.2.1 (+luasql-postgres)
我的脚本:
luasql = require "luasql.postgres"
value=arg[1]
current_time=os.date("%Y-%m-%d %H:%M:%S")
env = luasql.postgres()
con = assert (env:connect('postgres', 'postgres', 'postgres', '192.168.241.93','5432'))
function printvalues(res)
row = res:fetch ({}, "a")
while row do
print(string.format("%-12s", row.client_addr))
row = res:fetch (row, "a")
end
print()
end
res = assert (con:execute('select client_addr from pg_stat_replication order by replay_lag asc limit 1'))
--txn = res
a = {myfunc = printvalues(res)}
if a == '192.168.242.41' then
print("backend1")
elseif a == '192.168.241.76' then
print("backend2")
end
print(string.format("%-12s",a))
print(a)
脚本结果:
root@haproxy:/etc/haproxy# lua scripts/test.lua
192.168.1.76
table: 0x559fadf97080
table: 0x559fadf97080
请告诉我:
如何将函数的结果分配给变量?
如何去除函数输出中的空行printvalues(res)
【问题讨论】: