blob: 6b49faed081eb14540e6a68d7a5a29c976994f18 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{commands} ---
2 Wrapper functions for \function{os.popen()}.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{commands}
Fred Drake295da241998-08-10 19:42:37 +00004\sectionauthor{Sue Williams}{sbw@provis.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00005
6\modulesynopsis{Wrapper functions for \function{os.popen()}.}
7
Fred Drake4fd12921997-06-12 16:05:46 +00008
Fred Drakefdbd51d1998-03-11 06:24:46 +00009The \module{commands} module contains wrapper functions for
10\function{os.popen()} which take a system command as a string and
11return any output generated by the command and, optionally, the exit
12status.
Fred Drake4fd12921997-06-12 16:05:46 +000013
Fred Drakefdbd51d1998-03-11 06:24:46 +000014The \module{commands} module is only usable on systems which support
15\function{os.popen()} (currently \UNIX{}). It defines the following
16functions:
Fred Drake4fd12921997-06-12 16:05:46 +000017
Fred Drake4fd12921997-06-12 16:05:46 +000018
Fred Drake4fd12921997-06-12 16:05:46 +000019\begin{funcdesc}{getstatusoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000020Execute the string \var{cmd} in a shell with \function{os.popen()} and
21return a 2-tuple \code{(\var{status}, \var{output})}. \var{cmd} is
22actually run as \code{\{ \var{cmd} ; \} 2>\&1}, so that the returned
23output will contain output or error messages. A trailing newline is
24stripped from the output. The exit status for the command can be
25interpreted according to the rules for the \C{} function
26\cfunction{wait()}.
Fred Drake4fd12921997-06-12 16:05:46 +000027\end{funcdesc}
28
29\begin{funcdesc}{getoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000030Like \function{getstatusoutput()}, except the exit status is ignored
31and the return value is a string containing the command's output.
Fred Drake4fd12921997-06-12 16:05:46 +000032\end{funcdesc}
33
34\begin{funcdesc}{getstatus}{file}
35Return the output of \samp{ls -ld \var{file}} as a string. This
Fred Drakefdbd51d1998-03-11 06:24:46 +000036function uses the \function{getoutput()} function, and properly
37escapes backslashes and dollar signs in the argument.
Fred Drake4fd12921997-06-12 16:05:46 +000038\end{funcdesc}
39
40Example:
41
42\begin{verbatim}
43>>> import commands
44>>> commands.getstatusoutput('ls /bin/ls')
45(0, '/bin/ls')
46>>> commands.getstatusoutput('cat /bin/junk')
47(256, 'cat: /bin/junk: No such file or directory')
48>>> commands.getstatusoutput('/bin/junk')
49(256, 'sh: /bin/junk: not found')
50>>> commands.getoutput('ls /bin/ls')
51'/bin/ls'
52>>> commands.getstatus('/bin/ls')
53'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
54\end{verbatim}