有時我們想執行係統的命令,然後使用它們返回的任何東西,為此我們可以簡單地使用os.execute()函數或io.popen()函數。
之間的區別os.execute()函數和io.popen()函數是輸出值os.execute()函數更難處理,這就是為什麽建議使用io.popen()函數,其輸出值更易於處理和使用。
io.popen()在單獨的進程中啟動該程序並返回一個文件句柄,您可以使用該句柄從該程序中讀取數據。
用法
output = io.popen(command)
現在我們知道了io.popen()函數確實如此,讓我們在 Lua 示例中使用它。
示例
考慮下麵顯示的例子——
local handle = io.popen("echo hello")
local result = handle:read("*a")
handle:close()
在上麵的代碼中,我們使用了io.popen它返回一個文件句柄,我們可以用它來讀取命令的輸出。
輸出
hello
相關用法
- Lua string.char()用法及代碼示例
- Lua string.gsub()用法及代碼示例
- Lua table.pack()用法及代碼示例
- Lua string.byte()用法及代碼示例
- Lua string.lower()用法及代碼示例
- Lua string.format()用法及代碼示例
- Lua table.unpack()用法及代碼示例
- Lua string.upper()用法及代碼示例
- Lua math.modf()用法及代碼示例
- Lodash _.isValidDate()用法及代碼示例
- Lodash _.isInteger()用法及代碼示例
- Lodash _.sampleSize()用法及代碼示例
- Lodash _.fromQuery()用法及代碼示例
- Lodash _.noConflict()用法及代碼示例
- Lodash _.Intersection()用法及代碼示例
- Lodash _.values()用法及代碼示例
- Less isstring()用法及代碼示例
- Lodash _.isLength()用法及代碼示例
- Lodash _.third()用法及代碼示例
- Lodash _.negate()用法及代碼示例
注:本文由純淨天空篩選整理自Mukul Latiyan大神的英文原創作品 io.popen() function in Lua Programming。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。