diff options
author | Clement Farabet <clement.farabet@gmail.com> | 2012-08-18 18:16:59 +0400 |
---|---|---|
committer | Clement Farabet <clement.farabet@gmail.com> | 2012-08-18 18:16:59 +0400 |
commit | 423f4f621494c4b7b72a056423c95384f925598e (patch) | |
tree | 3f38c1cccdd015738c6b5851f7b16f74c77d65ed | |
parent | 3e9e7da0d4f4f41d4e45952e5a1923cc5b02795b (diff) |
Smarter sys.execute() [doesnt use a tmp file anymore]
-rw-r--r-- | init.lua | 15 |
1 files changed, 5 insertions, 10 deletions
@@ -69,18 +69,13 @@ toc = function(verbose) -------------------------------------------------------------------------------- -- execute an OS command, but retrieves the result in a string --- side effect: creates a file in /tmp/ -------------------------------------------------------------------------------- execute = function(cmd) - local tmpfile = '/tmp/lua.os.execute.out.' .. _G.tostring(clock()) - local cmd = cmd .. ' 1>'.. tmpfile..' 2>' .. tmpfile - os.execute(cmd) - local file = _G.assert(io.open(tmpfile)) - local str = file:read('*all') - file:close() - str:gsub('\n$','') - os.execute('rm ' .. tmpfile) - return str + local f = _G.assert(io.popen(cmd, 'r')) + local s = _G.assert(f:read('*a')) + f:close() + s = s:gsub('^%s*',''):gsub('%s*$','') + return s end -------------------------------------------------------------------------------- |