blob: 53b8a2070755fe3ea44eec4acaec08bf7aac3072 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{commands} ---
Fred Drakebf0f4341999-04-22 15:53:35 +00002 Utilities for running commands}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake47894d21999-04-22 15:19:01 +00004\declaremodule{standard}{commands}
5 \platform{Unix}
6\modulesynopsis{Utility functions for running external commands.}
7\sectionauthor{Sue Williams}{sbw@provis.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Fred Drake4fd12921997-06-12 16:05:46 +00009
Fred Drakefdbd51d1998-03-11 06:24:46 +000010The \module{commands} module contains wrapper functions for
11\function{os.popen()} which take a system command as a string and
12return any output generated by the command and, optionally, the exit
13status.
Fred Drake4fd12921997-06-12 16:05:46 +000014
Andrew M. Kuchlingd2ee30b2006-10-27 14:54:43 +000015The \module{subprocess} module provides more powerful facilities for
16spawning new processes and retrieving their results. Using the
17\module{subprocess} module is preferable to using the \module{commands}
18module.
19
Fred Drake47894d21999-04-22 15:19:01 +000020The \module{commands} module defines the following functions:
Fred Drake4fd12921997-06-12 16:05:46 +000021
Fred Drake4fd12921997-06-12 16:05:46 +000022
Fred Drake4fd12921997-06-12 16:05:46 +000023\begin{funcdesc}{getstatusoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000024Execute the string \var{cmd} in a shell with \function{os.popen()} and
25return a 2-tuple \code{(\var{status}, \var{output})}. \var{cmd} is
26actually run as \code{\{ \var{cmd} ; \} 2>\&1}, so that the returned
27output will contain output or error messages. A trailing newline is
28stripped from the output. The exit status for the command can be
Fred Drake47894d21999-04-22 15:19:01 +000029interpreted according to the rules for the C function
Fred Drakefdbd51d1998-03-11 06:24:46 +000030\cfunction{wait()}.
Fred Drake4fd12921997-06-12 16:05:46 +000031\end{funcdesc}
32
33\begin{funcdesc}{getoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000034Like \function{getstatusoutput()}, except the exit status is ignored
35and the return value is a string containing the command's output.
Fred Drake4fd12921997-06-12 16:05:46 +000036\end{funcdesc}
37
38\begin{funcdesc}{getstatus}{file}
39Return the output of \samp{ls -ld \var{file}} as a string. This
Fred Drakefdbd51d1998-03-11 06:24:46 +000040function uses the \function{getoutput()} function, and properly
41escapes backslashes and dollar signs in the argument.
Fred Drake4fd12921997-06-12 16:05:46 +000042\end{funcdesc}
43
44Example:
45
46\begin{verbatim}
47>>> import commands
48>>> commands.getstatusoutput('ls /bin/ls')
49(0, '/bin/ls')
50>>> commands.getstatusoutput('cat /bin/junk')
51(256, 'cat: /bin/junk: No such file or directory')
52>>> commands.getstatusoutput('/bin/junk')
53(256, 'sh: /bin/junk: not found')
54>>> commands.getoutput('ls /bin/ls')
55'/bin/ls'
56>>> commands.getstatus('/bin/ls')
57'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
58\end{verbatim}
Andrew M. Kuchlingd2ee30b2006-10-27 14:54:43 +000059
60\begin{seealso}
61 \seemodule{subprocess}{Module for spawning and managing subprocesses.}
62\end{seealso}