如何在Groovy中执行shell命令并获取返回代码$? [英] How to execute shell command in Groovy and get the return code $?
问题描述
我无法通过在Groovy中执行shell脚本来获取返回代码(不是输出或错误).
I can't get the return code (not the output or error) from executing a shell script within Groovy.
对于我尝试过的所有操作,它要么要求我退出,要么只打印$?而不是给我1或0.
For all what I tried, it either ask me to escape or just print the $? instead of give me 1 or 0.
groovy:75:美元符号后的非法字符串体字符; 解决方案:在第75行的第24列中,转义文字美元符号"\ $ 5"或将值表达式"$ {5}"括在括号中.
groovy: 75: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 75, column 24.
下面是我尝试过的解决方案,所有方法均无效.
Below are solutions I tried, all don't work.
println "../src/check_job_log.s ${it}.log".execute().text
println "Check log ${it}.log completed"
//assert ("echo \$?".execute().text == "1")
//output = """echo $?""".execute().text
println(['echo', '$?'].execute().text)
// below is code for @that other guy
//def process = "echo hello world".execute()
def process = "../src/check_job_log.s ${it}.log".execute()
print "Output: " + process.text
print "Exit code: " + process.exitValue()
Output: Exit code: 01
推荐答案
使用Process.exitValue()
代替.text
(或除.text
之外):
Use Process.exitValue()
instead of (or in addition to) .text
:
def process = "echo hello world".execute()
print "Output: " + process.text
print "Exit code: " + process.exitValue()
这篇关于如何在Groovy中执行shell命令并获取返回代码$?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!