How to execute shell command in Groovy and get the return code $?

戏子无情 提交于 2020-03-05 02:51:07

问题


I can't get the return code (not the output or error) from executing a shell script within Groovy.

For all what I tried, it either ask me to escape or just print the $? instead of give me 1 or 0.

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

回答1:


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()



回答2:


Why these command has different output?

$../src/check_job_log.s dml_ucd_test.sql.log
/iiss/prod/sql>
$echo $?
1
/iiss/prod/sql>
$../src/check_job_log.s dml_ucd_test.sql.log | echo $?
0
ops@uaiisst3:/iiss/prod/sql>
$


来源:https://stackoverflow.com/questions/55655206/how-to-execute-shell-command-in-groovy-and-get-the-return-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!