Concatenate string variable in lua when using io.popen command - lua

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":"(.-)"'

Related

How does Redis run lua script?

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.

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 ;)

Lua - popen command with multiple double quotes

I am writing a Lua script to manage Virtualbox on windows.
It seems that multiple double quotes are not parsed correctly. I am using the following function to implement this:
--Get output from an OS command - http://stackoverflow.com/questions/132397/get-back-the-output-of-os-execute-in-lua
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
This code works so long an the machine name doesn't have a space, but machines can have spaces so I have to support them:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo '..key
The following code does not work at all but it does give the correct format of the command to the log file so the syntax should be correct:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
logger:write("[",os.date("%Y-%m-%d %H:%M:%S"),"] Command: ",command,"\n")
vmStateRaw = os.capture(command, "raw")
Log file entry:
[2014-12-06 16:09:18] Command: "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
Interpreter output:
'\Program' is not recognized as an internal or external command,
operable program or batch file.
I have found that the following syntax works:
local command = '""\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
Log file output:
[2014-12-06 16:27:54] Command: ""\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
So this question isn't to solve a problem as I have aleady done that. I want to understand why the last command works as my current understand means this should not work.
TIA
The issue has to do with how system in C works. Under windows, system internally calls
cmd /c yourinput
Since os.execute just delegates to system (see here), your command likely ends up executing as:
cmd /c "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
For reference, from help cmd:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
Since your command contains 4 double quotes in there, it parses your command with the old behavior. This is why you need to surround your entire command with an extra set of " double quotes.

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

Get the output value of system

I am using the following method to get the time of the video with ffmpeg do not know what reason I can not put the output of the command
command =~ /Duration: ([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
variable for time and then insert in the
can someone give a help?
def get_time_video
command = system " ffmpeg -i video.flv 2>&1 "
command =~ /Duration: ([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
time = " #{$1}:#{$2}:#{$3} "
puts time # 00:00:30
update_attribute(:time, “#{time}”)
end
The Kernel.system function returns true or false as seen in the Documentation. If you want to parse the output of a command, you can use the backtick notation:
system = `ffmpeg -i video.flv 2>&1`

Resources