I have a lua script named simple.lua:
local value = ARGV[1]
local max = tonumber(value)
local num = redis.call('scard', KEYS[1])
if num < max then
return 1
else
return 0
end
With the --ldb --eval option, it seems that ARGV[1] always got a confusing nil value.
λ redis-cli --ldb --eval ./simple.lua 1 'test' 1
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local value = ARGV[1]
lua debugger> step
* Stopped at 2, stop reason = step over
-> 2 local max = tonumber(value)
lua debugger> step
* Stopped at 3, stop reason = step over
-> 3 local num = redis.call('scard', KEYS[1])
lua debugger> step
<redis> scard 1
<reply> 0
* Stopped at 4, stop reason = step over
-> 4 if num < max then
lua debugger> print
<value> value = nil
<value> max = nil
<value> num = 0
lua debugger> step
(error) ERR Error running script (call to f_d4c80f0cc613ef5ffa4db930fd6f71192a4b6dd9): @user_script:4: user_script:4: attempt to compare number with nil
However, when I eval the script string content within redis-cli, it works!
λ redis-cli
127.0.0.1:6379> eval "local value = ARGV[1] local max = tonumber(value) local num = redis.call('scard', KEYS[1]) if num < max then return 1 else return 0 end" 1 'test' 1
(integer) 1
127.0.0.1:6379>
Why will the ARGV[1] get a nil value when redis-cli --eval a lua file? 😂
Comment From: t3link
ps: redis info here
127.0.0.1:6379> info
# Server
redis_version:6.2.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a96bd29ff16c5226
redis_mode:standalone
os:Linux 4.19.0-14-amd64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:8.3.0
process_id:4276
process_supervised:no
run_id:068031b43d72978bd731b0f1929b6e02aea66577
tcp_port:6379
server_time_usec:1621267320023975
uptime_in_seconds:9665
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:10654583
executable:/opt/redis/src/redis-server
config_file:
io_threads_active:0
Comment From: t3link
... I just found the document solved my question.
The Redis EVAL command takes the list of keys the script uses, and the other non key arguments, as different arrays. When calling EVAL you provide the number of keys as a number. However with redis-cli and using the --eval option above, there is no need to specify the number of keys explicitly. Instead it uses the convention of separating keys and arguments with a comma. This is why in the above call you see foo , bar as arguments.
redis-cli --ldb --eval ./simple.lua 'test' , 1 works fine.