blob: 5a51ac4716e6f64aea7ada8955a97cce30b75c35 [file] [log] [blame]
Fred Drake7372e571997-12-17 13:51:08 +00001\section{Standard Module \sectcode{commands}}
Fred Drake4fd12921997-06-12 16:05:46 +00002\stmodindex{commands}
Fred Drake7372e571997-12-17 13:51:08 +00003\label{module-commands}
Fred Drake4fd12921997-06-12 16:05:46 +00004
Fred Drakefdbd51d1998-03-11 06:24:46 +00005The \module{commands} module contains wrapper functions for
6\function{os.popen()} which take a system command as a string and
7return any output generated by the command and, optionally, the exit
8status.
Fred Drake4fd12921997-06-12 16:05:46 +00009
Fred Drakefdbd51d1998-03-11 06:24:46 +000010The \module{commands} module is only usable on systems which support
11\function{os.popen()} (currently \UNIX{}). It defines the following
12functions:
Fred Drake4fd12921997-06-12 16:05:46 +000013
Fred Drake4fd12921997-06-12 16:05:46 +000014
Fred Drake4fd12921997-06-12 16:05:46 +000015\begin{funcdesc}{getstatusoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000016Execute the string \var{cmd} in a shell with \function{os.popen()} and
17return a 2-tuple \code{(\var{status}, \var{output})}. \var{cmd} is
18actually run as \code{\{ \var{cmd} ; \} 2>\&1}, so that the returned
19output will contain output or error messages. A trailing newline is
20stripped from the output. The exit status for the command can be
21interpreted according to the rules for the \C{} function
22\cfunction{wait()}.
Fred Drake4fd12921997-06-12 16:05:46 +000023\end{funcdesc}
24
25\begin{funcdesc}{getoutput}{cmd}
Fred Drakefdbd51d1998-03-11 06:24:46 +000026Like \function{getstatusoutput()}, except the exit status is ignored
27and the return value is a string containing the command's output.
Fred Drake4fd12921997-06-12 16:05:46 +000028\end{funcdesc}
29
30\begin{funcdesc}{getstatus}{file}
31Return the output of \samp{ls -ld \var{file}} as a string. This
Fred Drakefdbd51d1998-03-11 06:24:46 +000032function uses the \function{getoutput()} function, and properly
33escapes backslashes and dollar signs in the argument.
Fred Drake4fd12921997-06-12 16:05:46 +000034\end{funcdesc}
35
36Example:
37
38\begin{verbatim}
39>>> import commands
40>>> commands.getstatusoutput('ls /bin/ls')
41(0, '/bin/ls')
42>>> commands.getstatusoutput('cat /bin/junk')
43(256, 'cat: /bin/junk: No such file or directory')
44>>> commands.getstatusoutput('/bin/junk')
45(256, 'sh: /bin/junk: not found')
46>>> commands.getoutput('ls /bin/ls')
47'/bin/ls'
48>>> commands.getstatus('/bin/ls')
49'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
50\end{verbatim}