huanggaochi$ redis-cli --ldb --eval test.lua Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 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 foo = redis.call("ping") lua debugger> s <redis> ping <reply> "+PONG" * Stopped at 2, stop reason = step over -> 2 return foo lua debugger> quit
bdenmudeMacBook-Pro:~ huanggaochi$ redis-cli -h 10.32.64.19 -a q0QWFyMvT0 --ldb --eval test.lua 1 , 2 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 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 test = ARGV[1] lua debugger> n * Stopped at 2, stop reason = step over -> 2 return test lua debugger> print <value> test = "2" lua debugger> n
local bv = false; print(tostring(bv)); -- 输出"false"
local num1 = 10; local num2 = 10.0; local num3 = 10.03; print(tostring(num1)); --输出"10" print(tostring(num2)); --输出"10" print(tostring(num3)); --输出"10.03" local t = {x = 10,y = 0}; print(tostring(t)); -- 输出nil,不能将表类型转换为字符串
local num = tonumber("10"); -- 返回十进制数10 local num = tonumber("AF",16); -- 返回十六进制数175 local num = tonumber("0xA"); -- 返回10 local num = tonumber("56.9"); -- 返回56.9 local num = tonumber("0102"); -- 返回102 local num = tonumber("123456red"); -- 返回nil local num = tonumber("red"); -- 返回nil local num = tonumber("true"); -- 返回nil local num = tonumber({x =10, y = 20});-- 返回nil
local function split(str, delimiter) local dLen = string.len(delimiter) local newDeli = '' -- 仅考虑使用单个字符作为分割符,如果传进来一个长度大于1的字符串,则每个字符都算作一个分隔符 for i = 1, dLen, 1 do newDeli = newDeli .. "[" .. string.sub(delimiter, i, i) .. "]" end
-- 找到第一次匹配的位置区间,比如find("a,b,c", "[,]"),返回2, 2 local locaStart, locaEnd = string.find(str, newDeli) local arr = {} local n = 1 while locaStart ~= nil do if locaStart > 0 then -- 从字符串起始到分隔符区间的前一位 arr[n] = string.sub(str, 1, locaStart - 1) n = n + 1 end -- 把从分隔符区间的后一位到字符串末尾截出来作为新串 str = string.sub(str, locaEnd + 1, string.len(str)) -- 从新串中找下一个匹配的分隔符区间 locaStart, locaEnd = string.find(str, newDeli) end if str ~= nil then arr[n] = str end return arr end
直接在命令行执行 Lua 脚本
1 2 3
eval "return _VERSION" 0 eval "return {KEYS[1], ARGV[1]}" 1 a, b eval "return {ARGV[1]}" 0 b
# command.txt中是一大堆set命令,比如: # set k0 v0 # set k1 v1 # ... # set k100000 v100000
$ cat command.txt | redis-cli -h 127.0.0.1 -p 6379 -n 0 --pipe All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 11