How does Redis run lua script? - lua

local a = {}
for i,v in ipairs(KEYS) do
a[i] = redis.call('hgetall',v)
end
return a
Above is my script
eval test.lua 3 user:1 user:2 user:3 0
(error) ERR Error compiling script (new function): user_script:2: '=' expected near 'end'
Above is my redis command and error
Can anyone tell why the error happened and show me how to write a correct command?

The [EVAL command] (https://redis.io/commands/eval) expects the first argument to be the actual script, not a filename. Fix that and it should work.

Related

Concatenate string variable in lua when using io.popen command

I want to concatenate this string called test inside the URL but it doesn't seem to work:
test = "1.1.1.1"
local geoip = io.popen("wget -qO- 'https://api.ipgeolocationapi.com/geolocate/' .. test .. '":read():match'"name":"(.-)"')
print(geoip)
I got this error:
lua: hello.lua:3: ')' expected near ':'
I also tried doing like this way but I got the same error:
test = "1.1.1.1"
command = "wget -qO- https://api.ipgeolocationapi.com/geolocate/" .. test
local geoip = io.popen(command:read():matchname":"(.-)"')
print(geoip)
The url should append the test string. Any idea how to achieve this ?
You need to fix your quotes and parentheses; your test is inside a string literal and read and match are inside io.popen call:
local geoip = io.popen ("wget -qO- 'https://api.ipgeolocationapi.com/geolocate/" .. test .. "'"):read ():match '"name":"(.-)"'

io.popen how to get the go output in lua

How could I capture compiler errors go in lua?
I'm trying to get the output of the comp compiler errors in a tmux panel using lua
when executing the script the result is only shown in the current panel and not in the second panel
and the /tmp/output file is always empty
cmd=io.popen("go build -gcflags=-e scree.go")
f=io.open("/tmp/output")
f:write(cmd:read("*all"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
Is there any way to do this without using a temporary file?
I'm not totally clear on this. But maybe something like the following. i.e. pipe stderr to stdout and capture the result (not tested).
f = assert (io.popen ("go build -gcflags=-e scree.go 2>&1"))
for line in f:lines() do
os.execute("tmux run-shell -t 2 'echo " .. line .. "' ")
end
f:close()
I think the key is that popen won't capture stderr. See further details about that here

Error while running Lua script from redis client

I have the "Hello World" program in Lua script.
I am trying to call the script from (Chocolatey) Redis client.
I keep getting this error
(error) ERR Error compiling script (new function): user_script:1: function argument expected near '.'
Redis Script: "hello.lua"
local msg = "Hello, world!"
return msg
Chocolatey Redis Client:
127.0.0.1:6379> EVAL "D:\hello.lua" 0
Error Message
(error) ERR Error compiling script (new function): user_script:1: function argument expected near '.'
EVAL accepts the script itself, not a filename.
Try this:
EVAL 'local msg = "Hello, world!" return msg' 0
EDIT: to execute a script in a file, redis-cli provides the --eval switch that you can use as follows:
redis-cli --eval <path-to-script-file> [key1 [key2] ...] , [arg1 [arg2] ...]
I'm not familiar with the Windows fork, but it should be supported by it as well in all likelihood.
In *nix, you can also use the shell to provide the contents of the script to the cli, for example:
redis-cli SCRIPT LOAD "$(cat path-to-script-file)"
will load the contents in the file to Redis. There should be a similar way for achieving this in Windows but that's outside my current scope ;)

error while executing lua script for redis server

I was following this simple tutorial to try out a simple lua script
http://www.redisgreen.net/blog/2013/03/18/intro-to-lua-for-redis-programmers/
I created a simple hello.lua file with these lines
local msg = "Hello, world!"
return msg
And i tried running simple command
EVAL "$(cat /Users/rsingh/Downloads/hello.lua)" 0
And i am getting this error
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
I can't find what is wrong here and i haven't been able to find someone who has come across this.
Any help would be deeply appreciated.
Your problem comes from the fact you are executing this command from an interactive Redis session:
$ redis-cli
127.0.0.1:6379> EVAL "$(cat /path/to/hello.lua)" 0
(error) ERR Error compiling script (new function): user_script:1: unexpected symbol near '$'
Within such a session you cannot use common command-line tools like cat et al. (here cat is used as a convenient way to get the content of your script in-place). In other words: you send "$(cat /path/to/hello.lua)" as a plain string to Redis, which is not Lua code (of course), and Redis complains.
To execute this sample you must stay in the shell:
$ redis-cli EVAL "$(cat /path/to/hello.lua)" 0
"Hello, world!"
If you are coming from windows and trying to run a lua script you should use this format:
redis-cli --eval script.lua
Run this from the folder where your script is located and it will load a multi line file and execute it.
On the off chance that anyone's come to this from Windows instead, I found I had to do a lot of juggling to achieve the same effect. I had to do this:
echo “local msg = 'Hello, world!'; return msg” > hello.lua
for /F "delims=" %i in ('type hello.lua') do #set cmd=%i
redis-cli eval "%cmd%" 0
.. if you want it saved as a file, although you'll have to have all the content on one line. If you don’t just roll the content into a set command
set cmd=“local msg = 'Hello, world!'; return msg”
redis-cli eval "%cmd%" 0

In Lua, is there a function that will tell me what current version I'm running?

Subject says it all. I would like to know if my host interpreter is running Lua 5.2 or 5.1
There is global variable _VERSION (a string):
print(_VERSION)
-- Output
Lua 5.2
UPD :
Other methods to distinguish between Lua versions:
if _ENV then
-- Lua 5.2
else
-- Lua 5.1
end
UPD2 :
--[=[
local version = 'Lua 5.0'
--[[]=]
local n = '8'; repeat n = n*n until n == n*n
local t = {'Lua 5.1', nil,
[-1/0] = 'Lua 5.2',
[1/0] = 'Lua 5.3',
[2] = 'LuaJIT'}
local version = t[2] or t[#'\z'] or t[n/'-0'] or 'Lua 5.4'
--]]
print(version)
If you also need the third digit in Lua version (not available in _VERSION) you need to parse the output of lua -v command on the command line.
For platforms that support io.popen this script will do the trick, but only if the script is run by the standalone interpreter (not in interactive mode).IOW the arg global table must be defined:
local i_min = 0
while arg[ i_min ] do i_min = i_min - 1 end
local lua_exe = arg[ i_min + 1 ]
local command = lua_exe .. [[ -v 2>&1]] -- Windows-specific
local fh = assert( io.popen( command ) )
local version = fh:read '*a'
fh:close()
-- use version in the code below
print( version )
print( version:match '(%d%.%d%.%d)' )
Note that lua -v writes on stderr on Windows (for Linux I don't know), so the command for io.popen (which only captures stdout) must redirect stderr to stdout and the syntax is platform-specific.
_VERSION holds the interpreter version. Check the manual for reference.

Resources