Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/sys.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClement Farabet <clement.farabet@gmail.com>2012-08-18 18:16:59 +0400
committerClement Farabet <clement.farabet@gmail.com>2012-08-18 18:16:59 +0400
commit423f4f621494c4b7b72a056423c95384f925598e (patch)
tree3f38c1cccdd015738c6b5851f7b16f74c77d65ed
parent3e9e7da0d4f4f41d4e45952e5a1923cc5b02795b (diff)
Smarter sys.execute() [doesnt use a tmp file anymore]
-rw-r--r--init.lua15
1 files changed, 5 insertions, 10 deletions
diff --git a/init.lua b/init.lua
index 9ee1aa6..ca444c9 100644
--- a/init.lua
+++ b/init.lua
@@ -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
--------------------------------------------------------------------------------