2011-03-08 17 views
11

コマンドライン引数をとる実行ファイルを実行するには、Luaでio.popenを使用する必要があります。 期待される出力をキャプチャできるように、プロセスがLuaで終了するのを待つ方法はありますか? io.popen - プロセスがLuaで終了するのを待つ方法は?

local command = "C:\Program Files\XYZ.exe /all" 

    hOutput = io.popen(command) 
    print(string.format(""%s", hOutput)) 

は、実行可能ファイルは、コマンドライン引数 /allで呼び出される必要があるXYZ.exeであると仮定します。

io.popen(command)が実行されると、プロセスは印刷する必要のある文字列を返します。

マイコードスニペット:

function capture(cmd, raw) 
    local f = assert(io.popen(cmd, 'r')) 
    -- wait(10000); 
    local s = assert(f:read('*a')) 
    Print(string.format("String: %s",s)) 
    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 
local command = capture("C:\Tester.exe /all") 

あなたの助けが理解されるであろう。

+0

私は何とかそれが適切に – Chet

+0

機能キャプチャ(CMD、生) ローカルF =アサート(io.popen(CMD、R '')) が動作していないコードが生じています - (10000)を待ちます。 ローカルの場合= assert(f:read( '* a')) f:close()raw (s、 '%s + $'、 '') s = string.gsub(s、 '[\ n \ r] +' 、 '') 返信s end ローカルコマンド=キャプチャ( "C:\ Tester.exe/all") – Chet

答えて

18

標準のLuaを使用している場合、コードはちょっと変わって見えます。私はio.popenのタイムアウトやプラットフォームの依存関係に関するセマンティクスについては完全にはわかりませんが、少なくとも私のマシンでは以下のように動作します。

local file = assert(io.popen('/bin/ls -la', 'r')) 
local output = file:read('*all') 
file:close() 
print(output) --> Prints the output of the command. 
関連する問題