Get system command output: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 178: | Line 178: | ||
</lang> |
</lang> |
||
=={{header|C |
=={{header|C sharp|C#}}== |
||
<lang cpp>#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'; |
|||
}</lang> |
|||
=={{header|C#|C sharp}}== |
|||
<lang csharp>using System; |
<lang csharp>using System; |
||
Line 218: | Line 198: | ||
} |
} |
||
} |
} |
||
}</lang> |
|||
=={{header|C++}}== |
|||
<lang cpp>#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'; |
|||
}</lang> |
}</lang> |
||
Line 509: | Line 509: | ||
The fifth element of the sample capture (''ls -gocart'') being: |
The fifth element of the sample capture (''ls -gocart'') being: |
||
<pre># commandOutput.data.split('\n')[4]; |
<pre># commandOutput.data.split('\n')[4]; |
||
-rw-rw-r--. 1 155 Feb 8 07:52 JSON.jsi</pre> |
-rw-rw-r--. 1 155 Feb 8 07:52 JSON.jsi</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 621: | Line 621: | ||
{{out}} |
{{out}} |
||
<pre>Hurrah!</pre> |
<pre>Hurrah!</pre> |
||
=={{header|Mathematica}}== |
|||
<lang Mathematica>RunProcess["date"]</lang> |
|||
{{out}} |
|||
<pre><|"ExitCode" -> 0, "StandardOutput" -> "Wed Oct 4 14:01:01 BST 2017", "StandardError" -> ""|></pre> |
|||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Line 644: | Line 639: | ||
</lang> |
</lang> |
||
=={{header|Mathematica}}== |
|||
<lang Mathematica>RunProcess["date"]</lang> |
|||
{{out}} |
|||
<pre><|"ExitCode" -> 0, "StandardOutput" -> "Wed Oct 4 14:01:01 BST 2017", "StandardError" -> ""|></pre> |
|||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
Line 814: | Line 813: | ||
<lang parigp>externstr("time/t")</lang> |
<lang parigp>externstr("time/t")</lang> |
||
=={{ |
=={{header|Perl}}== |
||
Uses the qx{} construct (which is a synonym for backticks, e.g. `command`) to execute a given command and redirect its output. |
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: |
A (somewhat contrived*) example, capturing only STDOUT: |
||
Line 837: | Line 836: | ||
print if m/[^A-Z]+/; |
print if m/[^A-Z]+/; |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
If you don't want to execute it in shell (and you probably don't), then use this: |
|||
<lang perl6>say run($command, $arg1, $arg2, :out).out.slurp-rest;</lang> |
|||
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: |
|||
<lang perl6>my $p1 = run 'echo', 'Hello, world', :out; |
|||
my $p2 = run 'cat', '-n', :in($p1.out), :out; |
|||
say $p2.out.slurp-rest;</lang> |
|||
See [http://doc.perl6.org/type/Proc docs] for more info. |
|||
If you really want to run something in shell and you understand potential security problems, then you can use <code>qx//</code> (interpolates environment variables) or <code>qqx//</code> (interpolates normally). See [http://doc.perl6.org/language/quoting the docs for more info]. |
|||
<lang perl6>say qx[dir]</lang> |
|||
{{out}} |
|||
<pre>Find_URI_in_text.p6 History_variables.p6 K-d_tree.pl |
|||
Fractran.pl History_variables.pl XML_Input.p6</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 917: | Line 897: | ||
>>> # Ref: https://docs.python.org/3/library/subprocess.html</lang> |
>>> # Ref: https://docs.python.org/3/library/subprocess.html</lang> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<lang rsplus> |
<lang rsplus> |
||
Line 975: | Line 956: | ||
7 tests passed |
7 tests passed |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
If you don't want to execute it in shell (and you probably don't), then use this: |
|||
<lang perl6>say run($command, $arg1, $arg2, :out).out.slurp-rest;</lang> |
|||
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: |
|||
<lang perl6>my $p1 = run 'echo', 'Hello, world', :out; |
|||
my $p2 = run 'cat', '-n', :in($p1.out), :out; |
|||
say $p2.out.slurp-rest;</lang> |
|||
See [http://doc.perl6.org/type/Proc docs] for more info. |
|||
If you really want to run something in shell and you understand potential security problems, then you can use <code>qx//</code> (interpolates environment variables) or <code>qqx//</code> (interpolates normally). See [http://doc.perl6.org/language/quoting the docs for more info]. |
|||
<lang perl6>say qx[dir]</lang> |
|||
{{out}} |
|||
<pre>Find_URI_in_text.p6 History_variables.p6 K-d_tree.pl |
|||
Fractran.pl History_variables.pl XML_Input.p6</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,027: | Line 1,028: | ||
2 Dir(s) 949 801 435 136 bytes free |
2 Dir(s) 949 801 435 136 bytes free |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
|||
Many options, google exec or system or %x. Demonstrating backticks: |
|||
<lang ruby>str = `ls` |
|||
arr = `ls`.lines</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
Line 1,033: | Line 1,039: | ||
</lang> |
</lang> |
||
=={{header|Ruby}}== |
|||
Many options, google exec or system or %x. Demonstrating backticks: |
|||
<lang ruby>str = `ls` |
|||
arr = `ls`.lines</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<lang rust>use std::process::Command; |
<lang rust>use std::process::Command; |
||
Line 1,057: | Line 1,059: | ||
val sc = Source.fromInputStream(p.getInputStream) |
val sc = Source.fromInputStream(p.getInputStream) |
||
println(sc.mkString)</lang> |
println(sc.mkString)</lang> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
Using backticks: |
Using backticks: |
||
Line 1,129: | Line 1,132: | ||
close $pipe</lang> |
close $pipe</lang> |
||
This is usually not necessary except when dealing with binary data output. |
This is usually not necessary except when dealing with binary data output. |
||
=={{header|Ursa}}== |
|||
This program reads the output of the ifconfig command into the string stream 'output', then writes it to the screen. |
|||
<lang ursa>> 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 |
|||
> </lang> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Line 1,175: | Line 1,221: | ||
End Module</lang> |
End Module</lang> |
||
=={{header|Ursa}}== |
|||
This program reads the output of the ifconfig command into the string stream 'output', then writes it to the screen. |
|||
<lang ursa>> 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 |
|||
> </lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |