Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{commands} --- |
| 2 | Wrapper functions for \function{os.popen()}.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{commands} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \sectionauthor{Sue Williams}{sbw@provis.com} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | |
| 6 | \modulesynopsis{Wrapper functions for \function{os.popen()}.} |
| 7 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 8 | |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 9 | The \module{commands} module contains wrapper functions for |
| 10 | \function{os.popen()} which take a system command as a string and |
| 11 | return any output generated by the command and, optionally, the exit |
| 12 | status. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 13 | |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 14 | The \module{commands} module is only usable on systems which support |
| 15 | \function{os.popen()} (currently \UNIX{}). It defines the following |
| 16 | functions: |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 17 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 18 | |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 19 | \begin{funcdesc}{getstatusoutput}{cmd} |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 20 | Execute the string \var{cmd} in a shell with \function{os.popen()} and |
| 21 | return a 2-tuple \code{(\var{status}, \var{output})}. \var{cmd} is |
| 22 | actually run as \code{\{ \var{cmd} ; \} 2>\&1}, so that the returned |
| 23 | output will contain output or error messages. A trailing newline is |
| 24 | stripped from the output. The exit status for the command can be |
| 25 | interpreted according to the rules for the \C{} function |
| 26 | \cfunction{wait()}. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 27 | \end{funcdesc} |
| 28 | |
| 29 | \begin{funcdesc}{getoutput}{cmd} |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 30 | Like \function{getstatusoutput()}, except the exit status is ignored |
| 31 | and the return value is a string containing the command's output. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 32 | \end{funcdesc} |
| 33 | |
| 34 | \begin{funcdesc}{getstatus}{file} |
| 35 | Return the output of \samp{ls -ld \var{file}} as a string. This |
Fred Drake | fdbd51d | 1998-03-11 06:24:46 +0000 | [diff] [blame] | 36 | function uses the \function{getoutput()} function, and properly |
| 37 | escapes backslashes and dollar signs in the argument. |
Fred Drake | 4fd1292 | 1997-06-12 16:05:46 +0000 | [diff] [blame] | 38 | \end{funcdesc} |
| 39 | |
| 40 | Example: |
| 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} |