进行系统调用以将stdout输出作为字符串返回 [英] Making a system call that returns the stdout output as a string

查看:198
本文介绍了进行系统调用以将stdout输出作为字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl和PHP使用反引号进行此操作.例如,

Perl and PHP do this with backticks. For example,

$output = `ls`;

返回目录列表.类似的函数system("foo")返回给定命令foo的操作系统返回码.我说的是一个变体,它将foo输出的所有内容返回到stdout.

Returns a directory listing. A similar function, system("foo"), returns the operating system return code for the given command foo. I'm talking about a variant that returns whatever foo prints to stdout.

其他语言如何做到这一点?此功能有规范名称吗? (我要使用反引号";尽管也许我可以投币"syslurp".)

How do other languages do this? Is there a canonical name for this function? (I'm going with "backtick"; though maybe I could coin "syslurp".)

推荐答案

Perl:

$output = `foo`;

添加:这确实是一条多路领带.上面的代码也是有效的PHP,例如Ruby,也使用相同的反引号.

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.

这篇关于进行系统调用以将stdout输出作为字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆