Get system command output: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Example (ooRexx):: Fixed header. (Try looking at mark-up of another task to get format right).)
(→‎{{header|ooRexx)}}: Add python.)
Line 5: Line 5:
:For some languages this is a non-trivial task.
:For some languages this is a non-trivial task.


=={{header|ooRexx)}}==
=={{header|ooRexx}}==
<lang ooRexx>
<lang ooRexx>
/* Execute a system command and retrieve its output into a stem. */
/* Execute a system command and retrieve its output into a stem. */
Line 38: Line 38:


[[User:MainframeFossil|MainframeFossil]] ([[User talk:MainframeFossil|talk]]) 21:49, 10 April 2014 (UTC)
[[User:MainframeFossil|MainframeFossil]] ([[User talk:MainframeFossil|talk]]) 21:49, 10 April 2014 (UTC)

=={{header|Python}}==
<lang python>>>> import subprocess
>>> returned_text = subprocess.check_output("dir", shell=True, universal_newlines=True)
>>> type(returned_text)
<class 'str'>
>>> print(returned_text)
Volume in drive C is Windows
Volume Serial Number is 44X7-73CE

Directory of C:\Python33

04/07/2013 06:40 <DIR> .
04/07/2013 06:40 <DIR> ..
27/05/2013 07:10 <DIR> DLLs
27/05/2013 07:10 <DIR> Doc
27/05/2013 07:10 <DIR> include
27/05/2013 07:10 <DIR> Lib
27/05/2013 07:10 <DIR> libs
16/05/2013 00:15 33,326 LICENSE.txt
15/05/2013 22:49 214,554 NEWS.txt
16/05/2013 00:03 26,624 python.exe
16/05/2013 00:03 27,136 pythonw.exe
15/05/2013 22:49 6,701 README.txt
27/05/2013 07:10 <DIR> tcl
27/05/2013 07:10 <DIR> Tools
16/05/2013 00:02 43,008 w9xpopen.exe
6 File(s) 351,349 bytes
9 Dir(s) 46,326,947,840 bytes free

>>> # Ref: https://docs.python.org/3/library/subprocess.html</lang?

Revision as of 07:27, 11 April 2014

Get system command output


Execute a system command and get its output into the program. The output may be stored in any kind of collection (array, list, etc.).
For some languages this is a non-trivial task.

ooRexx

<lang ooRexx> /* Execute a system command and retrieve its output into a stem. */

 trace normal

/* Make the default values for the stem null strings. */

 text. = 

/* Issue the system command. "address command" is optional.) */

 address command 'ls -l | rxqueue'

/* Remember the return code from the command. */

 ls_rc = rc

/* Remember the number of lines created by the command. */

 text.0 = queued()

/* Fetch each line into a stem variable. */

 do t = 1 to text.0
   parse pull text.t
 end

/* Output each line in reverse order. */

 do t = text.0 to 1 by -1
   say text.t
 end

/* Exit with the system command's return code. */ exit ls_rc </lang>


MainframeFossil (talk) 21:49, 10 April 2014 (UTC)

Python

<lang python>>>> import subprocess >>> returned_text = subprocess.check_output("dir", shell=True, universal_newlines=True) >>> type(returned_text) <class 'str'> >>> print(returned_text)

Volume in drive C is Windows
Volume Serial Number is 44X7-73CE
Directory of C:\Python33

04/07/2013 06:40 <DIR> . 04/07/2013 06:40 <DIR> .. 27/05/2013 07:10 <DIR> DLLs 27/05/2013 07:10 <DIR> Doc 27/05/2013 07:10 <DIR> include 27/05/2013 07:10 <DIR> Lib 27/05/2013 07:10 <DIR> libs 16/05/2013 00:15 33,326 LICENSE.txt 15/05/2013 22:49 214,554 NEWS.txt 16/05/2013 00:03 26,624 python.exe 16/05/2013 00:03 27,136 pythonw.exe 15/05/2013 22:49 6,701 README.txt 27/05/2013 07:10 <DIR> tcl 27/05/2013 07:10 <DIR> Tools 16/05/2013 00:02 43,008 w9xpopen.exe

              6 File(s)        351,349 bytes
              9 Dir(s)  46,326,947,840 bytes free

>>> # Ref: https://docs.python.org/3/library/subprocess.html</lang?