我是靠谱客的博主 满意荔枝,这篇文章主要介绍Groovy执行脚本命令shell command,现在分享给大家,希望可以做个参考。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1): 直接执行一个字符串语句,executing a string

复制代码
1
2
3
4
5
6
7
8
9
10
A string can be executed in the standard java way: def command = """executable arg1 arg2 arg3"""// Create the String def proc = command.execute() // Call *execute* on the string proc.waitFor() // Wait for the command to finish // Obtain status and output println "return code: ${ proc.exitValue()}" println "stderr: ${proc.err.text}" println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

Gotchas: Take care if you wish to pass a quoted argument that contains white space – it will be split into multiple arguments, e.g.:

复制代码
1
2
"""executable "first with space" second""".execute()

will invoke executable with the following arguments:

  • arg1 = "first
  • arg2 = with
  • arg3 = space"
  • arg4 = second

In such a case, you may prefer to use one of the array or list of String variations, e.g.:

复制代码
1
2
["executable", "first with space", "second"].execute()

2) Processing shell command with piping in groovy script, 在groovy 脚本中使用管道功能

def proc = "ffmpeg -i /tmp/sample.m4a -f ffmetadata 2>&1 | grep Duration".execute()

是不行的,the '2>1&1' bit is shell functionality, and Groovy processes don't invoke the shell, they just start a program. If you really need it, you should do something like:

 

def proc1 = ['/bin/bash', '-c', '/usr/local/bin/ffmpeg -i /tmp/sample.m4a -f ffmetadata 2>&1'].execute()

or similar - do some experimenting. That is, you need to call the shell with the command you want executed as argument.

3): using ant builder's exec task

Ant has an exec task and it be accessed from the AntBuilder object

复制代码
1
2
3
4
5
6
7
8
9
10
11
def ant = new AntBuilder() // create an antbuilder ant.exec(outputproperty:"cmdOut", errorproperty: "cmdErr", resultproperty:"cmdExit", failonerror: "true", executable: '/opt/myExecutable') { arg(line:"""*"first with space"* second""") } println "return code: ${ant.project.properties.cmdExit}" println "stderr: ${ant.project.properties.cmdErr}" println "stdout: ${ ant.project.properties.cmdOut}"

转载于:https://my.oschina.net/jjyuangu/blog/1815945

最后

以上就是满意荔枝最近收集整理的关于Groovy执行脚本命令shell command的全部内容,更多相关Groovy执行脚本命令shell内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部