进行系统调用,以字符串形式返回 stdout 输出
Perl 和 PHP 通过反引号来实现这一点。 例如,
$output = `ls`;
返回目录列表。 类似的函数 system("foo")
返回给定命令 foo 的操作系统返回代码。 我说的是一个变体,它将 foo 打印的任何内容返回到标准输出。
其他语言是如何做到这一点的? 这个函数有规范的名称吗? (我将使用“反引号”;尽管也许我可以创造“syslurp”。)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
Python
Python <2.7 或 <3.1
从 subprocess.check_output() ="http://svn.python.org/view/python/trunk/Lib/subprocess.py?view=markup" rel="noreferrer">subprocess.py 或采用类似以下内容:
subprocess 模块自 2.4 起就已存在于 stdlib 中。
Python
Python <2.7 or <3.1
Extract
subprocess.check_output()
from subprocess.py or adapt something similar to:The subprocess module has been in the stdlib since 2.4.
Python:
Python:
[应 Alexman 和 dreeves -- 查看评论 --,您会在此DZones 找到Java Snippet 页面 一个独立于操作系统的完整版本,用于在本例中创建“ls”。 这是对他们的代码挑战。
下面的只是核心:Runtime.exec,加上2个线程来监听stdout和stderr。 ]
Java“简单!”:
或者用java代码
但是要做到这一点,你需要编码...
……这就尴尬了。
[At the request of Alexman and dreeves -- see comments --, you will find at this DZones Java Snippet page a full version Os-independent for making, in this instance, a 'ls'. This is a direct answer to their code-challenge.
What follows below is just the core: Runtime.exec, plus 2 thread to listen to stdout and stderr. ]
Java "Simple!":
Or in java code
But to do that, you need to code...
... this is embarrassing.
在 Perl 中执行此操作的另一种方法 (TIMTOWTDI)
当在 Perl 程序中嵌入相对较大的 shell 脚本时,这特别有用
Yet another way to do it in Perl (TIMTOWTDI)
This is specially useful when embedding a relatively large shell script in a Perl program
Ruby:反引号或“%x”内置语法。
Ruby: either backticks or the '%x' builtin syntax.
perl 中的另一种方法
这样做的优点是您可以选择分隔符,从而可以在命令中使用`(尽管恕我直言,如果您确实需要这样做,您应该重新考虑您的设计)。 另一个重要的优点是,如果使用单引号作为分隔符,变量将不会被插值(非常有用)
An alternative method in perl
This had the advantage that you can choose your delimiters, making it possible to use ` in the command (though IMHO you should reconsider your design if you really need to do that). Another important advantage is that if you use single quotes as delimiter, variables will not be interpolated (a very useful)
Haskell:
安装了 MissingH:
这是“粘合”语言中的一个简单操作就像 Perl 和 Ruby,但 Haskell 不是。
Haskell:
With MissingH installed:
This is an easy operation in "glue" languages like Perl and Ruby, but Haskell isn't.
在 shell 中
或其他
方法 第二种方法更好,因为它允许嵌套,但与第一种方法不同,并非所有 shell 都支持。
In shell
or alternatively
This second method is better because it allows nesting, but isn't supported by all shells, unlike the first method.
埃尔兰:
Erlang:
好吧,由于这是依赖于系统的,因此许多语言没有用于所需的各种系统调用的内置包装器。
例如,Common Lisp 本身并不是为了在任何特定系统上运行而设计的。 不过,SBCL(Steel Banks Common Lisp 实现)确实为类 Unix 系统提供了扩展,就像大多数其他 CL 实现一样。 当然,这比仅仅获得输出要“强大”得多(您可以控制运行过程,可以指定各种流方向等,请参阅SBCL手册,第6.3章),但很容易为此特定目的编写一个小宏:
现在,您可以像这样使用它:
也许您想将其全部放入一个字符串中。 该宏很简单(尽管也许可以使用更简洁的代码):
现在您可以通过此调用获取一个字符串:
Well, since this is system dependent, there are many languages that do not have a built-in wrapper for the various system calls needed.
For example, Common Lisp itself was not designed to run on any specific system. SBCL (the Steel Banks Common Lisp implementation), though, does provide an extension for Unix-like systems, as do most other CL implementations. This is much more "mighty" than just getting the output, of course (you have control over the running process, can specify all kinds of stream directions, etc., confer to the SBCL manual, chapter 6.3), but it is easy to write a little macro for this specific purpose:
Now, you can use it like this:
Perhaps you want to slurp it all into one string. The macro is trivial (though perhaps more concise code is possible):
Now you can get a string with this call:
数学:
Mathematica:
几年前,我为 插件 “nofollow noreferrer">jEdit 连接到本机应用程序。 这就是我用来从正在运行的可执行文件中获取流的方法。 唯一要做的就是
while((String s = stdout.readLine())!=null){...}
:Years ago I wrote a plugin for jEdit that interfaced to a native application. This is what I used to get the streams off the running executable. Only thing left to do is
while((String s = stdout.readLine())!=null){...}
:不要忘记TCL:
Don't forget Tcl:
C# 3.0,比 这个:
警告:生产代码应该正确处理 Process 对象...
C# 3.0, less verbose than this one:
Caveat: Production code should properly dispose the Process object...
Perl 中的另一种方式(或 2!)...
open 也可以是像这样写...
Yet another way (or 2!) in Perl....
open can also be written like so...
在 PHP 中
或
In PHP
or
C(带有
glibc
扩展):好的,不是很简洁或干净。 这就是C的生活...
C (with
glibc
extension):Okay, not really concise or clean. That's life in C...
在 Posix 兼容系统上的 C 中:
In C on Posix conformant systems:
为什么这里仍然没有 C# 人员:)
这是在 C# 中执行此操作的方法。 内置方式。
Why there is still no c# guy here :)
This is how to do it in C#. The built-in way.
Perl:
添加:这确实是多方面的联系。 上面的 PHP 也是有效的,例如 Ruby 也使用相同的反引号表示法。
Perl:
ADDED: This is really a multi-way tie. The above is also valid PHP, and Ruby, for example, uses the same backtick notation as well.
这是另一种 Lisp 方式:
然后,获取字符串:
如果您想运行一个命令,将大量信息打印到 STDOUT,您可以像这样运行它:
最后一个参数为输出预先分配大量空间大作家。 我猜想这个函数可能比一次一行读取输出流更快。
Here's another Lisp way:
Then, to get your string:
If you want to run a command that creates prints a great deal of info to STDOUT, you can run it like this:
The last parameter preallocates a large amount of space for the output from big-writer. I'm guessing this function could be faster than reading the output stream one line at a time.
Lua:
Lua:
J:
J:
Perl,另一种方式:
很有用,因为您可以提供命令输入,并分别返回 stderr 和 stdout。 远没有
IPC::Run
那么整洁/可怕/缓慢/令人不安,它可以为子例程设置管道。Perl, another way:
Useful because you can feed the command input, and get back both stderr and stdout separately. Nowhere near as neat/scary/slow/disturbing as
IPC::Run
, which can set up pipes to subroutines.Icon/Unicon:
文档称其为管道。 好处之一是它使输出看起来就像您正在读取文件一样。 这也意味着如果需要,您可以写入应用程序的标准输入。
Icon/Unicon:
The docs call this a pipe. One of the good things is that it makes the output look like you're just reading a file. It also means you can write to the app's stdin, if you must.
Clozure Common Lisp:
LispWorks
Clozure Common Lisp:
LispWorks
当然,它不是较小的(从所有可用的语言来看),但它不应该那么冗长。
这个版本很脏。 应该处理异常情况,阅读能力可能会得到提高。 这只是为了展示 java 版本如何启动。
完整程序如下。
示例输出(使用 type 命令)
示例输出(dir)
尝试任何
编辑
我必须承认我不能 100% 确定这是“最佳”方法。 请随意发布参考资料和/或代码来展示如何改进它或者有什么问题。
Granted, it is not the smaller ( from all the languages available ) but it shouldn't be that verbose.
This version is dirty. Exceptions should be handled, reading may be improved. This is just to show how a java version could start.
Complete program below.
Sample ouput ( using the type command )
Sample output ( dir )
Try any
EDIT
I must admit I'm not 100% sure this is the "best" way to do it. Feel free to post references and/or code to show how can it be improved or what's wrong with this.