lua 中使用 io.popen 命令时连接字符串变量

我想在 URL 中连接这个名为 test 的字符串,但似乎行不通:

test = "1.1.1.1"
local geoip = io.popen("wget -qO- 'https://api.ipgeolocationapi.com/geolocate/' .. test  .. '":read():match'"name":"(.-)"')
print(geoip)

我得到了这个错误:

lua: hello.lua:3: ')' expected near ':'

我也尝试过这种方式,但是得到了相同的错误:

test = "1.1.1.1"
command = "wget -qO- https://api.ipgeolocationapi.com/geolocate/" .. test
local geoip = io.popen(command:read():matchname":"(.-)"')
print(geoip)

URL 应该附加上测试字符串。 有什么好的办法实现这个吗?

点赞
用户6632736
用户6632736

你需要修正你的引号和括号;你的 test 在一个字符串字面量中,而 readmatchio.popen 调用中:

local geoip = io.popen("wget -qO- 'https://api.ipgeolocationapi.com/geolocate/" .. test .. "'"):read():match('"name":"(.-)"')
2020-10-22 04:30:04