欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jenkins?pipeline中獲取shell命令的標(biāo)準(zhǔn)輸出或者狀態(tài)的方法小結(jié)

 更新時(shí)間:2024年02月17日 10:18:45   作者:發(fā)哥隨手記  
這篇文章主要介紹了jenkins?pipeline中獲取shell命令的標(biāo)準(zhǔn)輸出或者狀態(tài),工作中需要獲取shell?命令的執(zhí)行狀態(tài),返回0或者非0,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
//獲取標(biāo)準(zhǔn)輸出
//第一種
result = sh returnStdout: true ,script: "<shell command>"
result = result.trim()
//第二種
result = sh(script: "<shell command>", returnStdout: true).trim()
//第三種
sh "<shell command> > commandResult"
result = readFile('commandResult').trim()
//獲取執(zhí)行狀態(tài)
//第一種
result = sh returnStatus: true ,script: "<shell command>"
result = result.trim()
//第二種
result = sh(script: "<shell command>", returnStatus: true).trim()
//第三種
sh '<shell command>; echo $? > status'
def r = readFile('status').trim()
//無(wú)需返回值,僅執(zhí)行shell命令
//最簡(jiǎn)單的方式
sh '<shell command>'

例如:
工作中需要獲取shell 命令的執(zhí)行狀態(tài),返回0或者非0
groovy語(yǔ)句寫(xiě)法為:

def exitValue = sh(script: "grep -i 'xxx' /etc/myfolder", returnStatus: true)
// 如果grep命令執(zhí)行沒(méi)有報(bào)錯(cuò),正常情況下exitValue為0,報(bào)錯(cuò)則為非0
echo "return exitValue :${exitValue}"
if(exitValue != 0){
   //執(zhí)行操作
}

需要注意的是當(dāng)命令中存在重定向的時(shí)候,會(huì)出現(xiàn)返回狀態(tài)異常,因?yàn)槲覀円祷貭顟B(tài),刪除重定向(&>/dev/null)即可,比如:

def exitValue = sh(script: "grep -i 'xxx' /etc/myfolder &>/dev/null", returnStatus: true)
// xxx不存在,正常邏輯是返回非0,但是實(shí)際中返回的是0 。
// 可以理解為先執(zhí)行命令然后賦值操作,類(lèi)似下面的動(dòng)作:(個(gè)人理解)
sh "ls -l > commandResult"
result = readFile('commandResult').trim()

groovy中存在另外一種解析shell腳本的方法,在jenkins pipeline中會(huì)使用會(huì)報(bào)異常,jenkins相關(guān)資料中也沒(méi)有看到此種用法,應(yīng)該是不支持

groovy.lang.MissingPropertyException: No such property: rhel for class: groovy.lang.Binding

寫(xiě)法為:

def command = "git log"
def proc = command.execute()
proc.waitFor()
def status = proc.exitValue()

到此這篇關(guān)于jenkins pipeline中如何獲取shell命令的標(biāo)準(zhǔn)輸出或者狀態(tài)的文章就介紹到這了,更多相關(guān)jenkins pipeline獲取shell命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論