Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error while executing lua script for redis server

Tags:

redis

lua

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.

like image 935
Raghvendra Singh Avatar asked Jan 22 '14 21:01

Raghvendra Singh


People also ask

What is Lua script in Redis?

Executing Lua in Redis. Redis lets users upload and execute Lua scripts on the server. Scripts can employ programmatic control structures and use most of the commands while executing to access the database. Because scripts execute in the server, reading and writing data from scripts is very efficient.

Which Lua command causes script to terminate when Redis returns?

call() will raise a Lua error that in turn will force EVAL to return an error to the command caller, while redis. pcall will trap the error returning a Lua table representing the error." So according to the documentation , in the case of using redis.


1 Answers

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!"
like image 130
deltheil Avatar answered Sep 20 '22 18:09

deltheil