2012-01-12 2 views
3

내 루아 프로그램에서 외부 프로그램의 출력을 잡아야한다. 이 외부 프로그램에는 특정 환경 변수가 필요합니다. 그래서 나는이 작업을 수행 : 환경이 큰 경우 분명히popen with environment

e = "" 
e = e .. "A=100;" 
e = e .. "B=Hi;" 
e = e .. "C=Test;" 
file = io.popen(e .. "/bin/aprogr") 

는 popen의 인수는() (있는 경우) 제한을 전달할 수 있습니다.

환경을 외부 프로그램에 전달하는 다른 방법이 있습니까?

답변

4

ExtensionProposal API에는 os.spawn 함수가 있습니다. 다음과 같이

당신은 그것을 사용할 수 있습니다

require"ex" 
local proc, err = os.spawn{ 
    command = e.."/bin/aprogr", 
    args = { 
     "arg1", 
     "arg2", 
     -- etc 
    }, 
    env = { 
     A = 100, -- I assume it tostrings the value 
     B = "Hi", 
     C = "Test", 
    }, 
    -- you can also specify stdin, stdout, and stderr 
    -- see the proposal page for more info 
} 
if not proc then 
    error("Failed to aprogrinate! "..tostring(err)) 
end 

-- if you want to wait for the process to finish: 
local exitcode = proc:wait() 

lua-ex-pai은 POSIX 및 Windows 용 구현을 제공합니다.

이 구현의 사전 컴파일 된 바이너리는 LuaForWindows 배포본과 함께 번들로 제공됩니다.

require"ex" 
local file = io.pipe() 
local proc = assert(os.spawn(e.."/bin/aprogr", { 
    env={ A = 100, B = "Hi", C = "Test" }, 
    stdout = file, 
})) 
-- write to file as you wish 
: 여기

은 사용 사례에 대한보다 간결 버전입니다