簡體   English   中英

如何在Groovy中執行shell命令並獲取返回碼$?

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

我無法獲得在Groovy中執行shell腳本的返回代碼(不是輸出或錯誤)。

對於我嘗試過的所有內容,要么讓我逃避,要么只打印$? 而不是給我1或0。

groovy:75:美元符號后的非法字體; 解決方案:要么轉義文字美元符號“\\ $ 5”,要么將值表達式“$ {5}”@第75行,第24列括起來。

以下是我嘗試的解決方案,都不起作用。

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

def process = "echo hello world".execute()
print "Output: " + process.text
print "Exit code: " + process.exitValue()

為什么這些命令有不同的輸出?

$../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>
$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM