I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Get system command output

From Rosetta Code
Task
Get system command output
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Execute a system command and get its output into the program. The output may be stored in any kind of collection (array, list, etc.).

Related task



Ada[edit]

Works with: GNAT
with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with GNAT.Expect; use GNAT.Expect;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with GNAT.String_Split; use GNAT.String_Split;
 
procedure System_Command is
Command  : String  := "ls -l";
Args  : Argument_List_Access;
Status  : aliased Integer;
Separators : constant String := LF & CR;
Reply_List : Slice_Set;
 
begin
Args := Argument_String_To_List (Command);
-- execute the system command and get the output in a single string
declare
Response : String :=
Get_Command_Output
(Command => Args (Args'First).all,
Arguments => Args (Args'First + 1 .. Args'Last),
Input => "",
Status => Status'Access);
begin
Free (Args);
-- split the output in a slice for easier manipulation
if Status = 0 then
Create (S => Reply_List,
From => Response,
Separators => Separators,
Mode => Multiple);
end if;
end;
-- do something with the system output. Just print it out
for I in 1 .. Slice_Count (Reply_List) loop
Put_Line (Slice (Reply_List, I));
end loop;
 
end System_Command;

Aime[edit]

o_("-- ", sshell().plan("expr", "8", "*", "9").link.b_dump('\n'), " --\n");
Output:
-- 72 --

AWK[edit]

 
BEGIN {
 
# For Windows
out = system2var("dir")
print out
 
# Non-Windows
out = getline2var("ls -l")
print out
}
 
# For a Windows environment using system() method
function system2var(command ,tempfile, cmd, out, rec, data, i) {
tempfile = "C:\\TEMP\\TMP.TMP"
cmd = command " > " tempfile
system(cmd)
close(cmd)
while (getline rec < tempfile > 0) {
if ( ++i == 1 )
data = rec
else
data = data "\n" rec
}
return(data)
}
 
# If command returns an ERRNO function returns null string
function getline2var(command ,fish, scale, ship) {
command = command " 2>/dev/null"
while ( (command | getline fish) > 0 ) {
if ( ++scale == 1 )
ship = fish
else
ship = ship "\n" fish
}
close(command)
return ship
}

BaCon[edit]

' Get system command
result$ = EXEC$("fortune")
PRINT CHOP$(result$)
PRINT "First word: " & TOKEN$(result$, 1)
Output:
prompt$ ./get-system-command
Little known fact about Middle Earth: The Hobbits had a very sophisticated
computer network!  It was a Tolkien Ring...
First word: Little

Batch File[edit]

 
@echo off
setlocal enabledelayedexpansion

:: Without storing the output of the command, it can be viewed by inputting the command

dir

 
:: Storing the output of 'dir' as "line[]" containing the respective lines of output (starting at line[1])

:: Note: This method removes any empty lines from the output
set tempcount=0
for /f "tokens=*" %%i in ('dir') do (
set /a tempcount+=1
set "line!tempcount!=%%i"
)
:: The array would be viewed like this
for /l %%i in (1,1,%tempcount%) do echo !line%%i!

 
:: Storing the output of 'dir' in a file, then outputting the contents of the file to the screen

:: NOTE: rewrites any file named "out.temp" in the current directory
dir>out.temp
type out.temp
del out.temp
 
pause>nul
 

C[edit]

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, char **argv)
{
if (argc < 2) return 1;
 
FILE *fd;
fd = popen(argv[1], "r");
if (!fd) return 1;
 
char buffer[256];
size_t chread;
/* String to store entire command contents in */
size_t comalloc = 256;
size_t comlen = 0;
char *comout = malloc(comalloc);
 
/* Use fread so binary data is dealt with correctly */
while ((chread = fread(buffer, 1, sizeof(buffer), fd)) != 0) {
if (comlen + chread >= comalloc) {
comalloc *= 2;
comout = realloc(comout, comalloc);
}
memmove(comout + comlen, buffer, chread);
comlen += chread;
}
 
/* We can now work with the output as we please. Just print
* out to confirm output is as expected */

fwrite(comout, 1, comlen, stdout);
free(comout);
pclose(fd);
return 0;
}
 

C#[edit]

using System;
 
namespace GetSystemCommandOutput {
class Program {
static void Main(string[] args) {
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c echo Hello World";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
 
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine("Output is {0}", output);
}
}
}

C++[edit]

#include <fstream>
#include <iostream>
 
std::string execute(const std::string& command) {
system((command + " > temp.txt").c_str());
 
std::ifstream ifs("temp.txt");
std::string ret{ std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>() };
ifs.close(); // must close the inout stream so the file can be cleaned up
if (std::remove("temp.txt") != 0) {
perror("Error deleting temporary file");
}
return ret;
}
 
int main() {
std::cout << execute("whoami") << '\n';
}

Boost alternative[edit]

Library: Boost
#include <iostream>
#include <string>
#include <vector>
#include <boost/process.hpp>
 
// Returns a vector containing names of files in the specified directory
// by capturing the output of the "ls" command, which is specific to
// Unix-like operating systems.
// Obviously there are better ways of listing files in a directory; this
// is just an example showing how to use boost::process.
std::vector<std::string> list_files(const std::string& directory) {
namespace bp = boost::process;
bp::ipstream input;
bp::child process("/bin/ls", directory, bp::std_out > input);
std::vector<std::string> files;
std::string file;
while (getline(input, file))
files.push_back(file);
process.wait();
if (process.exit_code() != 0)
throw std::runtime_error("Process did not complete successfully.");
return files;
}
 
int main(int argc, char** argv) {
try {
for (auto file : list_files(argc > 1 ? argv[1] : "."))
std::cout << file << '\n';
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Clojure[edit]

sh returns a map of exit code, stdout, and stderr from the command:

(use '[clojure.java.shell :only [sh]])
(sh "echo" "Hello")
Output:
{:exit 0, :out "Hello\n", :err ""}

Common Lisp[edit]

Library: trivial-shell
(trivial-shell:shell-command "uname -imp")
Output:
"x86_64 AMD A10-5750M APU with Radeon(tm) HD Graphics AuthenticAMD

We can also use functions specific to Common Lisp implementations. In SBCL, we have RUN-PROGRAM, which returns a process object. This object will contain an output stream if we use the :output keyword. We can then read from the stream:

(defparameter *my-proc*
(sb-ext:run-program "mplayer" (list "/path/to/groovy/tune")
:search t :output :stream :wait nil))
(read-line (sb-ext:process-output *my-proc*) nil)
Library: uiop

A bit more general, using uiop and grabbing output as a string:

(uiop:run-program '("ls" "-l" "-a") :output :string)

D[edit]

import std.process;
import std.stdio;
 
void main() {
auto cmd = executeShell("echo hello");
 
if (cmd.status == 0) {
writeln("Output: ", cmd.output);
} else {
writeln("Failed to execute command, status=", cmd.status);
}
}
Output:
Output: hello

F#[edit]

 
// System Command Output. Nigel Galloway: October 6th., 2020
let n=new System.Diagnostics.Process(StartInfo=System.Diagnostics.ProcessStartInfo(RedirectStandardOutput=true,RedirectStandardError=true,UseShellExecute=false,
FileName= @"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\FSharp\fsc.exe",Arguments="--help"))
n.Start()
printfn "%s" ((n.StandardOutput).ReadToEnd())
n.Close()
 
Output:
 
Microsoft (R) F# Compiler version 10.8.0.0 for F# 4.7
Copyright (c) Microsoft Corporation. All Rights Reserved.


                - OUTPUT FILES -
--out:<file>                             Name of the output file (Short form:
                                         -o)
--target:exe                             Build a console executable
--target:winexe                          Build a Windows executable
--target:library                         Build a library (Short form: -a)
--target:module                          Build a module that can be added to
                                         another assembly
--delaysign[+|-]                         Delay-sign the assembly using only
                                         the public portion of the strong
                                         name key
--publicsign[+|-]                        Public-sign the assembly using only
                                         the public portion of the strong
                                         name key, and mark the assembly as
                                         signed
--doc:<file>                             Write the xmldoc of the assembly to
                                         the given file
--keyfile:<file>                         Specify a strong name key file
--keycontainer:<string>                  Specify a strong name key container
--platform:<string>                      Limit which platforms this code can
                                         run on: x86, Itanium, x64,
                                         anycpu32bitpreferred, or anycpu. The
                                         default is anycpu.
--nooptimizationdata                     Only include optimization
                                         information essential for
                                         implementing inlined constructs.
                                         Inhibits cross-module inlining but
                                         improves binary compatibility.
--nointerfacedata                        Don't add a resource to the
                                         generated assembly containing
                                         F#-specific metadata
--sig:<file>                             Print the inferred interface of the
                                         assembly to a file
--nocopyfsharpcore                       Don't copy FSharp.Core.dll along the
                                         produced binaries


                - INPUT FILES -
--reference:<file>                       Reference an assembly (Short form:
                                         -r)
--compilertool:<file>                    Reference an assembly or directory
                                         containing a design time tool (Short
                                         form: -t)


                - RESOURCES -
--win32res:<file>                        Specify a Win32 resource file (.res)
--win32manifest:<file>                   Specify a Win32 manifest file
--nowin32manifest                        Do not include the default Win32
                                         manifest
--resource:<resinfo>                     Embed the specified managed resource
--linkresource:<resinfo>                 Link the specified resource to this
                                         assembly where the resinfo format is
                                         <file>[,<string
                                         name>[,public|private]]


                - CODE GENERATION -
--debug[+|-]                             Emit debug information (Short form:
                                         -g)
--debug:{full|pdbonly|portable|embedded} Specify debugging type: full,
                                         portable, embedded, pdbonly. ('full'
                                         is the default if no debuggging type
                                         specified and enables attaching a
                                         debugger to a running program,
                                         'portable' is a cross-platform
                                         format, 'embedded' is a
                                         cross-platform format embedded into
                                         the output file).
--embed[+|-]                             Embed all source files in the
                                         portable PDB file
--embed:<file;...>                       Embed specific source files in the
                                         portable PDB file
--sourcelink:<file>                      Source link information file to
                                         embed in the portable PDB file
--optimize[+|-]                          Enable optimizations (Short form:
                                         -O)
--tailcalls[+|-]                         Enable or disable tailcalls
--deterministic[+|-]                     Produce a deterministic assembly
                                         (including module version GUID and
                                         timestamp)
--pathmap:<path=sourcePath;...>          Maps physical paths to source path
                                         names output by the compiler
--crossoptimize[+|-]                     Enable or disable cross-module
                                         optimizations


                - ERRORS AND WARNINGS -
--warnaserror[+|-]                       Report all warnings as errors
--warnaserror[+|-]:<warn;...>            Report specific warnings as errors
--warn:<n>                               Set a warning level (0-5)
--nowarn:<warn;...>                      Disable specific warning messages
--warnon:<warn;...>                      Enable specific warnings that may be
                                         off by default
--consolecolors[+|-]                     Output warning and error messages in
                                         color


                - LANGUAGE -
--langversion:{?|version|latest|preview} Display the allowed values for
                                         language version, specify language
                                         version such as 'latest' or
                                         'preview'
--checked[+|-]                           Generate overflow checks
--define:<string>                        Define conditional compilation
                                         symbols (Short form: -d)
--mlcompatibility                        Ignore ML compatibility warnings


                - MISCELLANEOUS -
--nologo                                 Suppress compiler copyright message
--help                                   Display this usage message (Short
                                         form: -?)
[email protected]<file>                                Read response file for more options


                - ADVANCED -
--codepage:<n>                           Specify the codepage used to read
                                         source files
--utf8output                             Output messages in UTF-8 encoding
--preferreduilang:<string>               Specify the preferred output
                                         language culture name (e.g. es-ES,
                                         ja-JP)
--fullpaths                              Output messages with fully qualified
                                         paths
--lib:<dir;...>                          Specify a directory for the include
                                         path which is used to resolve source
                                         files and assemblies (Short form:
                                         -I)
--simpleresolution                       Resolve assembly references using
                                         directory-based rules rather than
                                         MSBuild resolution
--targetprofile:<string>                 Specify target framework profile of
                                         this assembly. Valid values are
                                         mscorlib, netcore or netstandard.
                                         Default - mscorlib
--baseaddress:<address>                  Base address for the library to be
                                         built
--checksumalgorithm:{SHA1|SHA256}        Specify algorithm for calculating
                                         source file checksum stored in PDB.
                                         Supported values are: SHA1 or SHA256
                                         (default)
--noframework                            Do not reference the default CLI
                                         assemblies by default
--standalone                             Statically link the F# library and
                                         all referenced DLLs that depend on
                                         it into the assembly being generated
--staticlink:<file>                      Statically link the given assembly
                                         and all referenced DLLs that depend
                                         on this assembly. Use an assembly
                                         name e.g. mylib, not a DLL name.
--pdb:<string>                           Name the output debug file
--highentropyva[+|-]                     Enable high-entropy ASLR
--subsystemversion:<string>              Specify subsystem version of this
                                         assembly
--quotations-debug[+|-]                  Emit debug information in quotations

Factor[edit]

with-process-reader is a combinator that encapsulates reading the output of a system command. It also throws an error along with the appropriate exit status in the event of failure.

USING: io.encodings.utf8 io.launcher ;
"echo hello" utf8 [ contents ] with-process-reader .
Output:
"hello\n"

FreeBASIC[edit]

' FB 1.05.0 Win64
 
'capture the output of the 'dir' command and print it to a text file
 
Open "dir_output.txt" For Output As #1
Open Pipe "dir" For Input As #2
Dim li As String
 
While Not Eof(2)
Line Input #2, li
Print #1, li
Wend
 
Close #2
Close #1
End

Gambas[edit]

Click this link to run this code

Public Sub Main()
Dim sStore As String
 
Shell "ls" To sStore
Print sStore
 
End

Output:

1.txt
20150328 _204330.mp4
barcode.tar
Business costs.ods
cafe
Charlie.fcstd
code128.ttf
Coffee icon.odg
DBStore
delete
delete.csv
Delete.lst
delete's.txt
...

Genie[edit]

[indent=4]
/*
Get system command output, in Genie
 
valac getSystemCommandOutput.gs
./getSystemCommandOutput
*/

 
init
try
// Blocking with output capture
standard_output : string
standard_error : string
exit_status : int
Process.spawn_command_line_sync("sh -c 'ls getSys*'",
out standard_output, out standard_error, out exit_status)
print standard_output
except e : SpawnError
stderr.printf("%s\n", e.message)
Output:

Using an extra sh invocation, to limit the ls using file name expansion for the sample capture.

prompt$ valac getSystemCommandOutput.gs
prompt$ ./getSystemCommandOutput
getSystemCommandOutput
getSystemCommandOutput.gs

Go[edit]

package main
 
import (
"fmt"
"log"
"os/exec"
)
 
func main() {
output, err := exec.Command("ls", "-l").CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Print(string(output))
}

Haskell[edit]

Works with: GHC version 8.0.2
Library: process
#!/usr/bin/env stack
-- stack --resolver lts-8.15 --install-ghc runghc --package process
 
import System.Process (readProcess)
 
main :: IO ()
main = do
-- get the output of the process as a list of lines
results <- lines <$> readProcess "hexdump" ["-C", "/etc/passwd"] ""
 
-- print each line in reverse
mapM_ (putStrLn . reverse) results

Icon and Unicon[edit]

#
# piped.icn, Get system command output
#
# Dedicated to the public domain
#
procedure main()
# start with an empty list
directory := []
 
# ls for UNIX, dir for other, assume Windows
command := if &features == "UNIX" then "ls" else "dir"
 
# open command in pipe mode
p := open(command, "p") | stop("Cannot open pipe for ", command)
 
# read in results and append to list
while put(directory, read(p))
 
# display the fifth entry, if there is one
write(\directory[5])
 
close(p)
end
Output:
prompt$ unicon -s piped.icn -x
piped.u

IS-BASIC[edit]

100 OPEN #1:"dirinfo.txt" ACCESS OUTPUT
110 SET DEFAULT CHANNEL 1
120 EXT "dir"
130 CLOSE #1
140 SET DEFAULT CHANNEL 0

J[edit]

We will box the result of uname -imp on a linux system, to show that we have captured the command output in J:

   require 'task'
<shell 'uname -imp'
┌─────────────────────┐
│x86_64 x86_64 x86_64 │
└─────────────────────┘

Caution: I have sometimes seen some versions of linux refuse to execute subshells after a few hundred thousand shell commands (the exec system call fails). I've not found any satisfying documentation on why this happens, but I strongly suspect kernel memory fragmentation (the examples where this happened were also using a lot of memory to accumulate results and it happened much more frequently an machines with little memory than on machines with more memory). Exiting J and starting a new process has cleared it up when it has happened. Anyways, I usually prefer to do that kind of processing before J starts, just to be safe.

(I've seen other problems on windows and osx - I am only singling out linux here because it is the most convenient for command line and system command use.)

Java[edit]

Works with: Java version 7
import java.io.*;
import java.util.*;
 
public class SystemCommand {
 
public static void main(String args[]) throws IOException {
 
String command = "cmd /c dir";
Process p = Runtime.getRuntime().exec(command);
 
try (Scanner sc = new Scanner(p.getInputStream())) {
 
System.out.printf("Output of the command: %s %n%n", command);
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
}
}
}

Output:

Output of the command: cmd /c dir 

 Het volume in station C heeft geen naam.
 Het volumenummer is 10CE-30C3

 Map van C:\projects\SystemCommand

30-06-2014  00:48    <DIR>          .
30-06-2014  00:48    <DIR>          ..
30-06-2014  00:48    <DIR>          build
30-06-2014  00:46             3.624 build.xml
30-06-2014  00:48    <DIR>          dist
30-06-2014  00:46                85 manifest.mf
30-06-2014  00:46    <DIR>          nbproject
30-06-2014  00:46    <DIR>          src
               2 bestand(en)            3.709 bytes
               6 map(pen)  756.833.009.664 bytes beschikbaar

Jsish[edit]

var commandOutput = exec('ls -gocart', { retAll:true });
puts(commandOutput.data);

The jsish exec command (like many jsish commands) accepts an optional option object, details available with interactive help:

# help exec
exec(val:string, options:string|object=void)
Execute an OS command.
If the command ends with '&', set the 'bg' option to true.
The second argument can be a string, which is the same as setting the 'inputStr' option.
By default, returns the string output, unless the 'bg', 'inputStr', 'retCode' or 'retAll' options are used

[exec options]
Option          Type    Description [Flags]
----------------------------------------------------------------------------
bg              BOOL    Run command in background using system() and return OS code.
chdir           STRING  Change to directory.
inputStr        STRING  Use string as input and return OS code.
noError         BOOL    Suppress all OS errors.
noRedir         BOOL    Disable redirect and shell escapes in command.
noShell         BOOL    Do not use native popen which invokes via /bin/sh.
trim            BOOL    Trim trailing whitespace from output.
retAll          BOOL    Return the OS return code and data as an object.
retCode         BOOL    Return only the OS return code.

With retAll the returned object has fields for .code, .status, .data.

The fifth element of the sample capture (ls -gocart) being:

# commandOutput.data.split('\n')[4];
-rw-rw-r--. 1   155 Feb  8 07:52 JSON.jsi

Julia[edit]

Works with: Julia version 0.6

In a single string:

ls = readstring(`ls`)

In multiple lines:

ll = readlines(`ls -l`)

Kotlin[edit]

// version 1.0.6
 
import java.util.Scanner
 
fun main(args: Array<String>) {
val command = "cmd /c chcp"
val p = Runtime.getRuntime().exec(command)
val sc = Scanner(p.inputStream)
println(sc.nextLine())
sc.close()
}
Output:
Active code page: 850

LIL[edit]

The library from lil.c does not include a system command, but main.c for the lil shell does.

set rc [system ls -go]
Output:
prompt$ ./lil
Little Interpreted Language Interactive Shell
# set rc [system ls -go]
total 1076
-rw-rw-r--. 1    729 Jan 14  2019 and.lil
drwxrwxr-x. 3   4096 Jan 14  2019 atom
-rw-rw-r--. 1    798 Jan 14  2019 call.lil
-rw-rw-r--. 1   3079 Aug  4 22:44 catcher.lil
drwxrwxr-x. 2   4096 Jan 14  2019 dll
-rw-rw-r--. 1    593 Jan 14  2019 dollar.lil
-rw-rw-r--. 1    697 Jan 14  2019 downeval.lil
-rw-rw-r--. 1    904 Jan 14  2019 enveval.lil
-rw-rw-r--. 1   1172 Jan 14  2019 expr.lil
-rw-rw-r--. 1    180 Jan 14  2019 fileio.lil
-rw-rw-r--. 1    427 Jan 14  2019 filter.lil
drwxrwxr-x. 2   4096 Jan 14  2019 fplil
-rw-rw-r--. 1   1369 Jan 14  2019 funcs.lil
-rw-rw-r--. 1     49 Jan 14  2019 hello.lil
-rw-rw-r--. 1    368 Jan 14  2019 jaileval.lil
-rw-rw-r--. 1 214990 Aug  8 03:18 liblil.a
-rw-rw-r--. 1    843 Jan 14  2019 liblil.tgt
-rwxrwxr-x. 1 174216 Aug  8 03:18 lil
-rw-rw-r--. 1 108062 Aug  8 03:18 lil.c
-rw-rw-r--. 1   5963 Jan 14  2019 lil.h
-rw-rw-r--. 1 214000 Aug  8 03:18 lil.o
-rw-rw-r--. 1    108 Jan 14  2019 lil.pro
-rw-rw-r--. 1   1244 Jan 14  2019 lil.tgt
-rw-rw-r--. 1    362 Jan 14  2019 lil.wpj
-rw-rw-r--. 1    666 Jan 14  2019 lists.lil
-rw-rw-r--. 1    469 Jan 14  2019 local.lil
-rw-rw-r--. 1   6082 Jan 14  2019 main.c
-rw-rw-r--. 1 137440 Jul 26 12:29 main.o
-rw-rw-r--. 1    968 Jan 14  2019 Makefile
-rw-rw-r--. 1    455 Jan 14  2019 Makefile.bcc
-rw-rw-r--. 1   1955 Jan 14  2019 mandelbrot.lil
-rw-rw-r--. 1    603 Jan 14  2019 mkmsvc.bat
-rw-rw-r--. 1    699 Jan 14  2019 mlcmt.lil
-rw-rw-r--. 1   1653 Jan 14  2019 oop_animals.lil
-rw-rw-r--. 1   1929 Jan 14  2019 oop.lil
-rw-rw-r--. 1  57495 Jan 14  2019 readme.txt
-rw-rw-r--. 1    811 Jan 14  2019 recfuncdef.lil
-rw-rw-r--. 1   1231 Jan 14  2019 renamefunc.lil
-rw-rw-r--. 1    333 Jan 14  2019 result.lil
-rw-rw-r--. 1    310 Jan 14  2019 return.lil
-rw-rw-r--. 1   1096 Jan 14  2019 robot.lil
-rw-rw-r--. 1   2368 Jan 14  2019 sm.lil
-rw-rw-r--. 1   1187 Jan 14  2019 strings.lil
-rw-rw-r--. 1    813 Jan 14  2019 topeval.lil
-rw-rw-r--. 1    615 Jan 14  2019 trim.lil
-rw-rw-r--. 1   1720 Jan 14  2019 upeval.lil
drwxrwxr-x. 2   4096 Jan 14  2019 vim
-rw-rw-r--. 1   1995 Jan 14  2019 watch.lil

# length $rc
2107

Lingo[edit]

Library: Shell Xtra
sx = xtra("Shell").new()
put sx.shell_cmd("cd C:\dev\lsw\lib & dir")
 
-- "
<snip>
31.08.2016 21:25 <DIR> .
31.08.2016 21:25 <DIR> ..
20.08.2016 04:58 <DIR> aes
23.06.2016 18:23 <DIR> audio
21.07.2016 19:19 <DIR> avmedia
23.06.2016 18:22 <DIR> base64
23.06.2016 18:21 <DIR> base9
<snip>"

Lua[edit]

local output = io.popen("echo Hurrah!")
print(output:read("*all"))
Output:
Hurrah!

M2000 Interpreter[edit]

Make a UTF-16LE txt.out from dir using a new cmd with /U

 
Module CheckIt {
Dos "cd "+quote$(Dir$) +" & cmd /U /C dir *.txt >txt.out";
Document txt$
Repeat {
Wait 100
Try {
load.doc txt$, "txt.out"
}
} Until doc.len(txt$)<>0
Report txt$
}
Checkit
 

Mathematica[edit]

RunProcess["date"]
Output:
<|"ExitCode" -> 0, "StandardOutput" -> "Wed Oct  4 14:01:01 BST 2017", "StandardError" -> ""|>

Neko[edit]

/* Get system command output, neko */
var process_run = $loader.loadprim("[email protected]_run", 2);
var process_stdout_read = $loader.loadprim("[email protected]_stdout_read", 4);
var process_stderr_read = $loader.loadprim("[email protected]_stderr_read", 4);
var process_stdin_close = $loader.loadprim("[email protected]_stdin_close", 1);
var process_exit = $loader.loadprim("[email protected]_exit", 1);
var sys_exit = $loader.loadprim("[email protected]_exit", 1);
 
/* work buffer */
var bufsize = 1024;
var buffer = $smake(bufsize);
 
/* default command is ls, otherwise pass command line arguments */
var argc = $asize($loader.args);
var cmd = "ls";
var args;
 
/* Check command line arguments */
if argc > 0 {
cmd = $loader.args[0];
}
if argc > 1 {
args = $asub($loader.args, 1, argc - 1);
}
 
/* spawn process, with arguments */
var proc = process_run(cmd, args);
 
/* Close input channel - command might be waiting for input */
process_stdin_close(proc);
 
/* capture and print stdout */
var not_done = true;
var len = 0;
do {
try {
len = process_stdout_read(proc, buffer, 0, bufsize);
} catch exc {
not_done = false;
}
if (not_done) $print($ssub(buffer, 0, len));
} while not_done;
 
/* capture and print any stderr */
not_done = true;
len = 0;
do {
try {
len = process_stderr_read(proc, buffer, 0, bufsize);
} catch exc {
not_done = false;
}
if (not_done) $print($ssub(buffer, 0, len));
} while not_done;
 
/* Get the exit status */
var ps = process_exit(proc);
sys_exit(ps);
Output:
prompt$ nekoc getcommand.neko
prompt$ neko getcommand | tail -4
webbing.n
xml
ZipEx.hx
zipper.n

prompt$ neko getcommand ls -gocart | tail -4
-rw-rw-r--.  1     121 Sep 18 13:36 swap.neko
-rw-rw-r--.  1    1513 Sep 18 18:32 getcommand.neko
drwxr-xr-x. 15    4096 Sep 18 18:32 .
-rw-rw-r--.  1     615 Sep 18 18:32 getcommand.n

Nim[edit]

import osproc
 
# Output string and error code
let (lsalStr, errCode) = execCmdEx("ls -al")
 
echo "Error code: " & $errCode
echo "Output: " & lsalStr
 
 
# Output string only
let lsStr = execProcess("ls")
 
echo "Output: " & lsStr
 

Objeck[edit]

class Test {
function : Main(args : String[]) ~ Nil {
output := System.Runtime->CommandOutput("ls -l");
each(i : output) {
output[i]->PrintLine();
};
}
}
 

ooRexx[edit]

version 1[edit]

/* 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

version 2[edit]

cmd='dir tu*.rex /od'
cmd '| rxqueue'
Say 'Output of "'cmd'"'
Say
Do While queued()>0
parse pull text
Say text
End
Output:
Output of "dir tu*.rex /od"

 Datenträger in Laufwerk I: ist USB DISK
 Volumeseriennummer: 5D55-13AC

 Verzeichnis von I:\

31.08.2016  19:36             1.358 turing.rex
31.08.2016  19:49             1.398 turing2.rex
               2 Datei(en),          2.756 Bytes
               0 Verzeichnis(se),  3.357.933.568 Bytes frei 

version 3[edit]

dir='dir.dir'
cmd='dir t*.rex /od'
cmd '>'dir
'dir tu*.rex /od >'dir
Say 'Output of "'cmd'"'
Say
Do While lines(dir)>0
Say linein(dir)
End
Call lineout oid
Output:
identical to version 2's output

PARI/GP[edit]

externstr("time/t")

Perl[edit]

Uses the qx{} construct (which is a synonym for backticks, e.g. `command`) to execute a given command and redirect its output. A (somewhat contrived*) example, capturing only STDOUT:

my @directories = grep { chomp; -d } `ls`;
for (@directories) {
chomp;
...; # Operate on directories
}
  • Perl's opendir function should be used in preference to parsing ls--it's safer, faster, and more portable.

Perl also honors shell redirections:

my $command = shift or die "No command supplied\n";
my @output_and_errors = qx/$command 2>&1/ or die "Couldn't execute command\n";

qx// is implemented internally with the built-in function readpipe, which can be invoked directly as readpipe EXPR (where EXPR is some command) and assigned to scalars or lists just like qx/command/ or `command`.

The open command can also be used to open pipes using the -| mode:

use autodie;
my $enc = ':encoding(UTF-8)';
my $child_pid = open(my $pipe, "-|$enc", 'ls');
while (<$pipe>) {
# Print all files whose names are all lowercase
print if m/[^A-Z]+/;
}

Phix[edit]

constant tmp = "hostname.txt",
cmd = iff(platform()=WINDOWS?"hostname":"uname -n")
{} = system_exec(sprintf("%s > %s",{cmd,tmp}),4)
string host = trim(get_text(tmp))
{} = delete_file(tmp)
?host
Output:
"Pete-PC"

See also demo\capture_console.exw (needs a bit more work on linux)

Phixmonti[edit]

"hostname.txt" var hname
"hostname > " hname chain cmd
hname "r" fopen
dup fgets print fclose
"del " hname chain cmd

PicoLisp[edit]

: (in '(uname "-om") (line T))
-> "aarch64 Android"

PowerShell[edit]

Capture system disk label information as an array of strings:

 
[string[]]$volume = cmd /c vol
 
$volume
 
Output:
 Volume in drive C is Ordo-Xenos
 Volume Serial Number is 8C33-162D

Python[edit]

>>> 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

R[edit]

 
system("wc -l /etc/passwd /etc/group", intern = TRUE)
 
Output:
[1] "  49 /etc/passwd" "  80 /etc/group"  " 129 total"   

Racket[edit]

We use #lang racket/base to show which module system is in. It would be imported anyway if we use the larger #lang racket.

This demonstrates one function: system</system>. It is the simplest of a family of commands in the <code>racket/system collection.

See documentation for system and friends.

#lang racket/base
 
(require racket/system
(only-in racket/port with-output-to-string)
tests/eli-tester)
 
(test
 ;; system runs command and outputs to current output port (which is stdout unless we catch it)
(system "ls /etc/motd") => #t
 ;; it throws an error on non-zero exit code (so I need to catch it in this error handler)
(system "false") => #f  ; nothing printed to stdout/stderr
(system "ls /etc/mosh") => #f ; error report printed to stderr
 ;; output can be captured by redirecting stdout/stderr (which are known as current-output-port and
 ;; current-error-port in racket parlance).
 ;; the command printed a \n, so there is a newline captured by the system command
(with-output-to-string (λ () (system "ls /etc/motd"))) => "/etc/motd\n"
 ;; no \n is captured when none is captured
(with-output-to-string (λ () (system "echo -n foo"))) => "foo"
 ;; error is still not captured (it's still printed to stderr)
(with-output-to-string (λ () (system "echo -n foo; echo bar 1>&2"))) => "foo"
 ;; we can capture both with:
(let* ((out-str-port (open-output-string))
(err-str-port (open-output-string))
(system-rv
(parameterize ((current-output-port out-str-port) (current-error-port err-str-port))
(system "echo -n foo; echo bar 1>&2"))))
(values system-rv (get-output-string out-str-port) (get-output-string err-str-port)))
=> (values #t "foo" "bar\n"))
Output:
/etc/motd

the following goes to standard error:

ls: /etc/mosh: No such file or directory
bar

and back to standard output:

7 tests passed

Raku[edit]

(formerly Perl 6)

If you don't want to execute it in shell (and you probably don't), then use this:

say run($command, $arg1, $arg2, :out).out.slurp;

Unfortunately, it is very long to type, but that is the only way to pass your variables as arguments safely.

You might be tempted to start using shell when you have to pipe something, but even in that case there is no need to do so. See this code:

my $p1 = run 'echo', 'Hello, world', :out;
my $p2 = run 'cat', '-n', :in($p1.out), :out;
say $p2.out.slurp-rest;

See docs for more info.

If you really want to run something in shell and you understand potential security problems, then you can use qx// (interpolates environment variables) or qqx// (interpolates normally). See the docs for more info.

say qx[dir]
Output:
Find_URI_in_text.p6  History_variables.p6  K-d_tree.pl
Fractran.pl	     History_variables.pl  XML_Input.p6

REXX[edit]

Works with: Regina REXX
/*REXX program  executes a  system command  and displays the results  (from an array).  */
parse arg xxxCmd /*obtain the (system) command from CL.*/
trace off /*suppress REXX error msgs for fails. */
@.= 0 /*assign default in case ADDRESS fails.*/
address system xxxCmd with output stem @. /*issue/execute the command and parms. */
if rc\==0 then say copies('─', 40) ' return code ' rc " from: " xxxCmd
/* [↑] display if an error occurred.*/
do #=1 for @.0 /*display the output from the command. */
say strip(@.#, 'T') /*display one line at a time──►terminal*/
end /*#*/ /* [↑] displays all the output. */
exit 0 /*stick a fork in it, we're all done. */
output   from the executed command   (under Windows/XP):       dir   g:sub*.2*
 Volume in drive G is -----G-----
 Volume Serial Number is 6826-1B4B

 Directory of G:\

05/22/2012  08:27                30 SUBSET.2
05/24/2012  03:55         2,117,571 SUBSET.20
05/24/2012  03:55         1,132,068 SUBSET.21
05/24/2012  09:56           522,155 SUBSET.22
05/24/2012  09:56           193,293 SUBSET.23
05/24/2012  09:56            71,931 SUBSET.24
05/24/2012  09:56            15,995 SUBSET.25
05/24/2012  09:56             3,188 SUBSET.26
05/24/2012  09:56               471 SUBSET.27
               9 File(s)      4,056,702 bytes
               0 Dir(s)  18,252,660,736 bytes free

Ring[edit]

 
system("dir C:\Ring\doc")
 

Output:

 Volume in drive C is Helyi lemez
 Volume Serial Number is F0B2-B1C8

 Directory of C:\Ring\doc

2016. 04. 05.  17:19    <DIR>          .
2016. 04. 05.  17:19    <DIR>          ..
2016. 04. 07.  07:44         3 276 076 Fayed_RingDoc_1.0.chm
2016. 04. 06.  19:00         5 371 211 Fayed_RingDoc_1.0.pdf
               2 File(s)      8 647 287 bytes
               2 Dir(s)  949 801 435 136 bytes free

Ruby[edit]

Many options, google exec or system or %x. Demonstrating backticks:

str = `ls`   
arr = `ls`.lines

Run BASIC[edit]

a$ = shell$("dir") ' Returns the directory info into a$
print a$ ' prints the directory
 

Rust[edit]

use std::process::Command;
use std::io::{Write, self};
 
fn main() {
let output = Command::new("/bin/cat")
.arg("/etc/fstab")
.output()
.expect("failed to execute process");
 
io::stdout().write(&output.stdout);
}

Scala[edit]

import scala.io.Source
 
val command = "cmd /c echo Time at %DATE% %TIME%"
val p = Runtime.getRuntime.exec(command)
val sc = Source.fromInputStream(p.getInputStream)
println(sc.mkString)

Sidef[edit]

Using backticks:

var output = `ls`             # `output` is a string
var lines = `ls`.lines # `lines` is an array

Using pipes:

var pipe   = %p(ls)           # same as: Pipe('ls')
var pipe_h = pipe.open_r # open the pipe for reading
var lines = [] # will store the lines of the output
pipe_h.each { |line| lines << line }

Stata[edit]

Redirect the output to a temporary file, then read its contents into a result macro r(out).

program shellout, rclass
tempfile f
tempname m
shell `0' > `f'
file open `m' using "`f'", read binary
file seek `m' eof
file seek `m' query
local n=r(loc)
if `n'>0 {
file seek `m' tof
file read `m' %`n's s
file close `m'
return local out "`s'"
}
end

Example:

. shellout dir /b *.dta
. display r(out)
auto.dta
titanium.dta
 
. shellout python -V
. display r(out)
Python 3.6.2

Swift[edit]

import Foundation
 
let process = Process()
 
process.launchPath = "/usr/bin/env"
process.arguments = ["pwd"]
 
let pipe = Pipe()
process.standardOutput = pipe
 
process.launch()
 
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String.init(data: data, encoding: String.Encoding.utf8)
 
print(output!)

Standard ML[edit]

val useOS =  fn input =>
let
val text = String.translate (fn #"\"" => "\\\""|n=>str n ) input ;
val shellCommand = " echo " ^ text ^ "| gzip -c " ;
val fname = "/tmp/fConv" ^ (String.extract (Time.toString (Posix.ProcEnv.time()),7,NONE) );
val me = ( Posix.FileSys.mkfifo
(fname,
Posix.FileSys.S.flags [ Posix.FileSys.S.irusr,Posix.FileSys.S.iwusr ]
) ;
Posix.Process.fork ()
) ;
in
if (Option.isSome me) then
let
val fin =BinIO.openIn fname
in
( Posix.Process.sleep (Time.fromReal 0.1) ;
BinIO.inputAll fin before
(BinIO.closeIn fin ; OS.FileSys.remove fname )
)
end
else
( OS.Process.system ( shellCommand ^ " > " ^ fname ^ " 2>&1 " ) ;
Word8Vector.fromList [] before OS.Process.exit OS.Process.success
)
end;
 

call

useOS "This is my text, zip it now" ;
val it =
  fromList[0wx1F, 0wx8B, 0wx8, 0wx0, 0wxBE, 0wxCE, 0wx7F, 0wx5E, 0wx0, 0wx3,
     ...]: BinIO.vector

Tcl[edit]

The exec makes this straight-forward for most commands.

set data [exec ls -l]
puts "read [string length $data] bytes and [llength [split $data \n]] lines"

There are a few exceptions, such as the DIR command on Windows, where they need to be run slightly differently due to being system shell builtins rather than executables. In that case, the auto_execok standard library command is used to look up how to run the command (strictly it can be used for any command — it will do path resolution, etc. — but is only necessary for system builtins).

set data [exec {*}[auto_execok DIR]]

By default, Tcl will use the system encoding (as reported by encoding system) to understand the output byte-stream as characters, and will auto-convert all the various types of newline terminators into U+00000A characters. Control over this is possible by launching the subprocess as a pipe, configuring the pipe, and then reading the pipe in its entirety.

# This syntax is pretty ugly, alas
set pipe [open |[list ls -l] "r"]
fconfigure $pipe -encoding iso8859-1 -translation lf
set data [read $pipe]
close $pipe

This is usually not necessary except when dealing with binary data output.

Ursa[edit]

This program reads the output of the ifconfig command into the string stream 'output', then writes it to the screen.

> decl iodevice iod
> decl string<> arg
> append "ifconfig" arg
> set iod (ursa.util.process.start arg)
> decl string<> output
> set output (iod.readlines)
> for (decl int i) (< i (size output)) (inc i)
.. out output<i> endl console
..end for
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128
inet 127.0.0.1 netmask 0xff000000
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=27<RXCSUM,TXCSUM,VLAN_MTU,TSO4>
ether d4:9a:20:b8:8d:2c
nd6 options=1<PERFORMNUD>
media: autoselect
status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:26:08:e0:67:cc
inet6 fe80::226:8ff:fee0:67cc%en1 prefixlen 64 scopeid 0x5
inet 172.20.30.66 netmask 0xffffff00 broadcast 172.20.30.255
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
lladdr d4:9a:20:ff:fe:b8:8d:2c
nd6 options=1<PERFORMNUD>
media: autoselect <full-duplex>
status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 02:26:08:e0:67:cc
media: autoselect
status: inactive
>

VBScript[edit]

This program implements a function that executes a DOS command and returns the output to the caller.

For Each line In ExecCmd("ipconfig /all")
Wscript.Echo line
Next
 
'Execute the given command and return the output in a text array.
Function ExecCmd(cmd)
 
'Execute the command
Dim wso : Set wso = CreateObject("Wscript.Shell")
Dim exec : Set exec = wso.Exec(cmd)
Dim res : res = ""
 
'Read all result text from standard output
Do
res = res & VbLf & exec.StdOut.ReadLine
Loop Until exec.StdOut.AtEndOfStream
 
'Return as a text array
ExecCmd = Split(Mid(res,2),vbLf)
End Function

Visual Basic .NET[edit]

Translation of: C#
Module Module1
 
Sub Main()
Dim proccess As New Process
Dim startInfo As New ProcessStartInfo
 
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.FileName = "cmd.exe"
startInfo.Arguments = "/c echo Hello World"
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
 
proccess.StartInfo = startInfo
proccess.Start()
 
Dim output = proccess.StandardOutput.ReadToEnd
Console.WriteLine("Output is {0}", output)
End Sub
 
End Module

Wren[edit]

Wren CLI doesn't currently expose a way to either execute a system command or to get its output.

However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.

/* get_system_command_output.wren */
class Command {
foreign static output(name, param) // the code for this is provided by Go
}
 
System.print(Command.output("ls", "-ls"))

which we embed in the following Go program and run it.

Library: WrenGo
/* get_system_command_output.go */
package main
 
import (
wren "github.com/crazyinfin8/WrenGo"
"log"
"os"
"os/exec"
)
 
type any = interface{}
 
func getCommandOutput(vm *wren.VM, parameters []any) (any, error) {
name := parameters[1].(string)
param := parameters[2].(string)
var cmd *exec.Cmd
if param != "" {
cmd = exec.Command(name, param)
} else {
cmd = exec.Command(name)
}
cmd.Stderr = os.Stderr
bytes, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return string(bytes), nil
}
 
func main() {
vm := wren.NewVM()
fileName := "get_system_command_output.wren"
methodMap := wren.MethodMap{"static output(_,_)": getCommandOutput}
classMap := wren.ClassMap{"Command": wren.NewClass(nil, nil, methodMap)}
module := wren.NewModule(classMap)
vm.SetModule(fileName, module)
vm.InterpretFile(fileName)
vm.Free()
}

zkl[edit]

From the REPL on Linux. Runs a command in the shell with stdout redirected to file, then slurps the file. A bit greasy since there isn't a way to find/generate a unique unused file name.

zkl: System.cmd("date >foo.txt")
0 // date return code
zkl: File("foo.txt").read().text
Wed Aug 20 00:28:55 PDT 2014