groovyshell java if,Groovy执行shell命令

回答(7)

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

好的,我自己解决了;

def sout = new StringBuilder(), serr = new StringBuilder()

def proc = 'ls /badDir'.execute()

proc.consumeProcessOutput(sout, serr)

proc.waitForOrKill(1000)

println "out> $sout err> $serr"

显示:

out> err> ls: cannot access /badDir: No such file or directory

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

"ls".execute() 返回 Process 对象,这就是 "ls".execute().text 的工作原理 . 您应该能够只读取错误流以确定是否有任何错误 .

Process 上有一个额外的方法,允许您传递 StringBuffer 来检索文本: consumeProcessErrorStream(StringBuffer error) .

例:

def proc = "ls".execute()

def b = new StringBuffer()

proc.consumeProcessErrorStream(b)

println proc.text

println b.toString()

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

// a wrapper closure around executing a string

// can take either a string or a list of strings (for arguments with spaces)

// prints all output, complains and halts on error

def runCommand = { strList ->

assert ( strList instanceof String ||

( strList instanceof List && strList.each{ it instanceof String } ) \

)

def proc = strList.execute()

proc.in.eachLine { line -> println line }

proc.out.close()

proc.waitFor()

print "[INFO] ( "

if(strList instanceof List) {

strList.each { print "${it} " }

} else {

print strList

}

println " )"

if (proc.exitValue()) {

println "gave the following error: "

println "[ERROR] ${proc.getErrorStream()}"

}

assert !proc.exitValue()

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在上面提供的答案中添加一个更重要的信息 -

对于一个过程

def proc = command.execute();

总是试着用

def outputStream = new StringBuffer();

proc.waitForProcessOutput(outputStream, System.err)

//proc.waitForProcessOutput(System.out, System.err)

而不是

def output = proc.in.text;

在groovy中执行命令后捕获输出,因为后者是阻塞调用(SO question for reason) .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

我发现这更像是惯用的:

def proc = "ls foo.txt doesnotexist.txt".execute()

assert proc.in.text == "foo.txt\n"

assert proc.err.text == "ls: doesnotexist.txt: No such file or directory\n"

正如另一篇文章提到的,这些是阻塞调用,但由于我们想要使用输出,这可能是必要的 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

def exec = { encoding, execPath, execStr, execCommands ->

def outputCatcher = new ByteArrayOutputStream()

def errorCatcher = new ByteArrayOutputStream()

def proc = execStr.execute(null, new File(execPath))

def inputCatcher = proc.outputStream

execCommands.each { cm ->

inputCatcher.write(cm.getBytes(encoding))

inputCatcher.flush()

}

proc.consumeProcessOutput(outputCatcher, errorCatcher)

proc.waitFor()

return [new String(outputCatcher.toByteArray(), encoding), new String(errorCatcher.toByteArray(), encoding)]

}

def out = exec("cp866", "C:\\Test", "cmd", ["cd..\n", "dir\n", "exit\n"])

println "OUT:\n" + out[0]

println "ERR:\n" + out[1]

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

command = "ls *"

def execute_state=sh(returnStdout: true, script: command)

但如果命令失败,进程将终止

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值