2
votes

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

1

1 Answers

2
votes

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:

  1. 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.
  2. 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.