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