[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ



Creates a pipe and executes a command


f = io.popen (command, mode)


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
 end -- for loop

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)

-- remove temporary file
os.remove (n)

See Also ...

Lua functions

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


Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=io.popen)

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]