6
votes

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.

3

3 Answers

8
votes

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!"
2
votes

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.

0
votes

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