io.popen
Lua function

io.popen

Summary

Creates a pipe and executes a command

Prototype

f = io.popen (command, mode)



Description

Creates a pipe and executes a command. Mode can be one of:


  • "r" - The calling process can read the spawned command’s standard output via the returned stream. This is the default.

  • "w" - The calling process can write to the spawned command’s standard input via the returned stream.

  • "b" - Open in binary mode.

  • "t" - Open in text mode.


However io.popen is not supported under the version compiled into MUSHclient, so don't get too excited. :)

This is an example of using popen under the Linux Lua executable:


 f = assert (io.popen ("ls -l"))
  
 for line in f:lines() do
   print(line)
 end -- for loop
   
 f:close()


An alternative to using pipes, if you want to capture operating system output, is to redirect command output to a temporary file, like this:


-- get a temporary file name
n = os.tmpname ()

-- execute a command
os.execute ("dir > " .. n)

-- display output
for line in io.lines (n) do
  print (line)
end

-- remove temporary file
os.remove (n)



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_f:close f:close (Closes a file)
LUA_f:flush f:flush (Flushes outstanding data to disk)
LUA_f:lines f:lines (Returns an iterator function for reading the file line-by-line)
LUA_f:read f:read (Reads the file according to the specified formats)
LUA_f:seek f:seek (Sets and gets the current file position)
LUA_f:setvbuf f:setvbuf (Sets the buffering mode for an output file)
LUA_f:write f:write (Writes to a file)
LUA_io.close io.close (Closes a file)
LUA_io.flush io.flush (Flushes outstanding data to disk for the default output file)
LUA_io.input io.input (Opens filename for input in text mode)
LUA_io.lines io.lines (Returns an iterator function for reading a named file line-by-line)
LUA_io.open io.open (Opens a file)
LUA_io.output io.output (Opens a file for output)
LUA_io.read io.read (Reads from the default input file)
LUA_io.stderr io.stderr (File handle for standard error file)
LUA_io.stdin io.stdin (File handle for standard input file)
LUA_io.stdout io.stdout (File handle for standard output file)
LUA_io.tmpfile io.tmpfile (Returns a handle to a temporary file)
LUA_io.type io.type (Returns type of file handle)
LUA_io.write io.write (Writes to the default output file)

(Help topic: lua=io.popen)

DOC_contents Documentation contents page