Groovy代码示例 – groovy中如何执行shell命令?

groovy写shell也是很方便,具体写法如下:

//定义输出流和错误流
def sout = new StringBuilder(), serr = new StringBuilder()
//定义shell
def proc = 'ls /badDir'.execute()
//执行shell并获取输出和异常
proc.consumeProcessOutput(sout, serr)
//设置超时时间
proc.waitForOrKill(1000)
//打印输出和错误信息(假如有的话)
println "out> $sout err> $serr"

输出内容如下:

out>  err> ls: cannot access /badDir: No such file or directory
上一篇 Groovy代码示例 - 文件操作
下一篇 Groovy代码示例 - groovy脚本中如何调用及包含其他的脚本?
目录
文章列表
1 Jenkins+Docker自动化部署
Jenkins+Docker自动化部署
2
JS跳转阻止后退至上一页面,没有历史记录
JS跳转阻止后退至上一页面,没有历史记录
3
Spring Boot整合Shiro登录认证和权限管理
Spring Boot整合Shiro登录认证和权限管理
4
Robot Framework中解析utf-8格式的中文转换
Robot Framework中解析utf-8格式的中文转换
5
Spring Cloud 简单灰度发布
Spring Cloud 简单灰度发布
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。