执行外部命令 [英] Execute external command

查看:81
本文介绍了执行外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道它是Scala还是Play!问题.我想从Play应用程序中执行一些外部命令,从命令中获取输出,并根据命令输出向用户显示报告.有人可以帮忙吗?

I do not know whether it is a Scala or Play! question. I want to execute some external command from my Play application, get the output from the command and show a report to user based on the command output. Can anyone help?

例如,当我从shell输入 my-command 时,它会显示以下输出,我想捕获并显示在网络中:

For example, when I enter my-command from shell it shows output like below, which I want to capture and show in web:

Id    Name   IP
====================
1     A      x.y.z.a
2     B      p.q.r.s

请不要担心输出的格式和解析.从功能上讲,我正在寻找类似PHP exec 的东西.我知道Java Runtime.getRuntime().exec("command"),但是是否有任何Scala/Play版本可以达到目的?

Please, do not worry about format and parsing of the output. Functionally, I am looking something like PHP exec. I know about java Runtime.getRuntime().exec("command") but is there any Scala/Play version to serve the purpose?

推荐答案

您必须首先导入scala的流程包.方法 !! 可以满足您的需要,它会执行该语句并捕获文本输出.

You have to import the process packages of scala first. The method !! does what you need, it executes the statement and captures the text output.

import scala.sys.process._
val cmd = "uname -a" // Your command
val output = cmd.!! // Captures the output

这篇关于执行外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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