lua shell-less相当于`io.popen`

时间:2016-05-25 22:01:50

标签: shell io lua

Lua的io.popen对于捕获命令的stdout很有用,但它依赖于/bin/sh进行分词,全局扩展等。

所以我可以做到以下内容并且效果很好

-- this unfortunately uses /bin/sh
local command = "ls -1";

local ph = io.popen(command, "r")

while true do
   local line = ph:read()
   if line == nil then
      break
   end
   io.write("I read a line! " .. line .. "\n")
end

io.write("Done with line reading!\n")

是否有一些功能是spawn*fork/exec*周围的薄包装,我可以通过{"ls", "-1"}来避免shell?

0 个答案:

没有答案