[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Issue using quoted arguments on os.execute() on Windows
- From: Peter Cawley <lua@...>
- Date: Sun, 17 Nov 2013 22:30:11 +0000
I suspect that `os.execute(cmd)`, via the C runtime, ends up executing
`"cmd.exe /C " .. cmd`.
If you run `cmd.exe /?`, then part of the help text is the following:
--8x--------------------------------------------------
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.
--8x--------------------------------------------------
The end effect of these rules is that if you want the sensible
behaviour of quotes being preserved, and you want this consistently,
then your command line mustn't start with a quote character. To that
end, my personal approach is to do `os.execute("type NUL && "..cmd)`
rather than `os.execute(cmd)`.
On Sun, Nov 17, 2013 at 9:58 PM, Thijs Schreijer
<thijs@thijsschreijer.nl> wrote:
> I'm facing a weird issue, I'm calling an os command with an argument. Now both the command and the argument contain spaces in their filenames, hence I need to quote them with " ".
>
> The issue; it won't work if both (command and argument) are quoted, all other cases it does work as expected.
>
> Test code showing the issue (even without spaces in the names):
> --8x--------------------------------------------------
> md = [[c:\temp\exec_test.bat]]
> target = [[C:\rocktree\lib\lua\pe-parser.lua]]
>
> local function Q(arg)
> return '"'..arg..'"'
> end
>
> local function test_exec(cmd, target)
> cmd = cmd .." ".. target
> print("","Testing:", cmd)
> print("","",os.execute(cmd))
> print("")
> end
>
> print("execute")
> test_exec(cmd, target) -- none quoted
> test_exec(Q(cmd), target) -- command quoted
> test_exec(cmd, Q(target)) -- argument quoted
> test_exec(Q(cmd), Q(target)) -- both quoted --> fails
> --8x--------------------------------------------------
>
>
> The command called is a simple batch script displaying the command called and its first 3 parameters;
> --8x--------------------------------------------------
> @echo off
> echo CMD: =%0=
> echo 1 : =%1=
> echo 2 : =%2=
> echo 3 : =%3=
> --8x--------------------------------------------------
>
> The output
> ------------------------------------------------------
> C:\Users\Thijs\Desktop>c:\temp\exec_test.lua
> execute
> Testing: c:\temp\exec_test.bat C:\rocktree\lib\lua\pe-parser.lua
> CMD: =c:\temp\exec_test.bat=
> 1 : =C:\rocktree\lib\lua\pe-parser.lua=
> 2 : ==
> 3 : ==
> 0
>
> Testing: "c:\temp\exec_test.bat" C:\rocktree\lib\lua\pe-parser.lua
> CMD: =c:\temp\exec_test.bat=
> 1 : =C:\rocktree\lib\lua\pe-parser.lua=
> 2 : ==
> 3 : ==
> 0
>
> Testing: c:\temp\exec_test.bat "C:\rocktree\lib\lua\pe-parser.lua"
> CMD: =c:\temp\exec_test.bat=
> 1 : ="C:\rocktree\lib\lua\pe-parser.lua"=
> 2 : ==
> 3 : ==
> 0
>
> Testing: "c:\temp\exec_test.bat" "C:\rocktree\lib\lua\pe-parser.lua"
> The filename, directory name, or volume label syntax is incorrect.
> 1
>
>
> C:\Users\Thijs\Desktop>
> ------------------------------------------------------
>
> Apparently the command is not found in the final (both quoted) case.
> Anyone seen this before, or knows how to resolve this?
>
> Thijs
>
>
>
> PS: If I execute the exact same command on a command prompt, it works as expected.
>
> ------------------------------------------------------
> C:\Users\Thijs\Desktop>"c:\temp\exec_test.bat" "C:\rocktree\lib\lua\pe-parser.lua"
> CMD: ="c:\temp\exec_test.bat"=
> 1 : ="C:\rocktree\lib\lua\pe-parser.lua"=
> 2 : ==
> 3 : ==
>
> C:\Users\Thijs\Desktop>
> ------------------------------------------------------
>
>