像 exec() 这样的东西在 Perl 中返回一个值?
我的印象(也许是错误的)Perl 中的“exec”函数不返回值(我的印象是它只是运行命令)。情况是 Perl 脚本正在服务器上运行,我需要调用该脚本在 Linux 机器内运行命令,同时还要返回结果。这怎么能做到呢?
I am under the impression (perhaps wrongly) that the 'exec' function in Perl does not return a value (I get the impression it just runs the command). The situation is that a Perl script is running on a server and I need to invoke this script to run commands inside the Linux box, but also return the results. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果“返回结果”是指返回命令的 STDOUT,那么您需要的是反引号或 qx()。例如:
请注意,不会返回发送至 STDERR 的消息。
如果您指的是程序的退出状态,请使用系统:
If by "return the results" you mean return STDOUT of the commands, what you need is backticks or
qx()
. E.g.:Do note that messages to STDERR are not returned.
If you mean the exit status of the program, use system:
exec() 不仅不返回值,而且根本不返回。
但我很确定你不想要 system() 的返回值,
您似乎想要命令的输出,所以:
Not only does exec() not return a value, it does not return at all.
But I'm pretty sure that you do NOT want the return value of system(),
you seem to want the output of the command, so:
运行程序并保存其结果的常用 Perl 方法是 反引号< /a>:
The usual perl way to run a program and save its results is backticks:
来自文档:http://perldoc.perl.org/functions/system.html
我一般用这个:
From the documentation : http://perldoc.perl.org/functions/system.html
I generally use this :
捕获 STDOUT:
捕获返回状态:
Perldocs 捕获 STDERR 和 STDOUT:
< em>请参阅链接以了解捕获一个输出流的其他方法,但不能捕获另一个输出流
Capture STDOUT:
Capture Return Status:
Perldocs Capture STDERR and STDOUT:
See the link for other ways to capture one, output stream, but not the other
在 perl 中使用 back-tic
允许您执行 shell 命令,并且它返回任何输出。
using the back-tic
in perl allows you to execute shell commands and it returns whatever the output was.