Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{commands} --- |
Fred Drake | bf0f434 | 1999-04-22 15:53:35 +0000 | [diff] [blame] | 2 | Utilities for running commands} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 47894d2 | 1999-04-22 15:19:01 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{commands} |
| 5 | \platform{Unix} |
| 6 | \modulesynopsis{Utility functions for running external commands.} |
| 7 | \sectionauthor{Sue Williams}{sbw@provis.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 9 | |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 10 | The \module{commands} module contains wrapper functions for |
| 11 | \function{os.popen()} which take a system command as a string and |
| 12 | return any output generated by the command and, optionally, the exit |
| 13 | status. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 15 | The \module{subprocess} module provides more powerful facilities for |
| 16 | spawning new processes and retrieving their results. Using the |
| 17 | \module{subprocess} module is preferable to using the \module{commands} |
| 18 | module. |
| 19 | |
Fred Drake | 47894d2 | 1999-04-22 15:19:01 +0000 | [diff] [blame] | 20 | The \module{commands} module defines the following functions: |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 21 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 22 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{getstatusoutput}{cmd} |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 24 | Execute the string \var{cmd} in a shell with \function{os.popen()} and |
| 25 | return a 2-tuple \code{(\var{status}, \var{output})}. \var{cmd} is |
| 26 | actually run as \code{\{ \var{cmd} ; \} 2>\&1}, so that the returned |
| 27 | output will contain output or error messages. A trailing newline is |
| 28 | stripped from the output. The exit status for the command can be |
Fred Drake | 47894d2 | 1999-04-22 15:19:01 +0000 | [diff] [blame] | 29 | interpreted according to the rules for the C function |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 30 | \cfunction{wait()}. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 31 | \end{funcdesc} |
| 32 | |
| 33 | \begin{funcdesc}{getoutput}{cmd} |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 34 | Like \function{getstatusoutput()}, except the exit status is ignored |
| 35 | and the return value is a string containing the command's output. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 38 | Example: |
| 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' |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 50 | \end{verbatim} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | |
| 52 | \begin{seealso} |
| 53 | \seemodule{subprocess}{Module for spawning and managing subprocesses.} |
| 54 | \end{seealso} |