//获取标准输出
//第一种

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

//获取执行状态

//第一种

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

//无需返回值,仅执行shell命令

//最简单的方式

sh '<shell command>'

例如:

工作中需要获取shell 命令的执行状态,返回0或者非0
groovy语句写法为:

def exitValue = sh(script: "grep -i 'xxx' /etc/myfolder", returnStatus: true)
echo "return exitValue :${exitValue}"
if(exitValue != ){
执行操作
}

如果grep命令执行没有报错,正常情况下exitValue为0,报错则为非0

需要注意的是当命令中存在重定向的时候,会出现返回状态异常,因为我们要返回状态,删除重定向(&>/dev/null)即可,比如:

def exitValue = sh(script: "grep -i 'xxx' /etc/myfolder &>/dev/null", returnStatus: true)
xxx不存在,正常逻辑是返回非0,但是实际中返回的是0 。可以理解为先执行命令然后赋值操作,类似下面的动作:(个人理解)
sh "ls -l > commandResult"
result = readFile('commandResult').trim()

groovy中存在另外一种解析shell脚本的方法,在jenkins pipeline中会使用会报异常,jenkins相关资料中也没有看到此种用法,应该是不支持

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

写法为:

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

相关资料:

https://stackoverflow.com/questions/36547680/how-to-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-fro
https://issues.jenkins-ci.org/browse/JENKINS-26133
https://stackoverflow.com/questions/36956977/how-to-execute-a-command-in-a-jenkins-2-0-pipeline-job-and-then-return-the-stdou
---------------------
作者:liurizhou
来源:CSDN
原文:https://blog.csdn.net/liurizhou/article/details/86670092
版权声明:本文为博主原创文章,转载请附上博文链接!

jenkins pipeline中获取shell命令的标准输出或者状态的更多相关文章

  1. [转] Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出

    [From] https://issues.jenkins-ci.org/browse/JENKINS-44930 其做法是,把stdout定向到一个文件,sh 配置 returnStatus: tr ...

  2. linux C程序中获取shell脚本输出(如获取system命令输出)

    转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...

  3. 在jenkins中使用shell命令推送当前主机上的docker镜像到远程的Harbor私有仓库

    1.jenkins主机上的docker配置 先在Jenkins主机的docke上配置上Harbor私有仓库地址 cat /etc/docker/daemon.json { "insecure ...

  4. linux c程序中获取shell脚本输出的实现方法

    linux c程序中获取shell脚本输出的实现方法 1. 前言Unix界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些夸张,但不可否认的是,借助脚本确实能够极大的简化一些编程工作. ...

  5. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  6. Awk中调用shell命令

    Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...

  7. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...

  8. 【转载】如何在C语言中调用shell命令

    转载自:http://blog.csdn.net/chdhust/article/details/7951576 如何在C语言中调用shell命令 在linux操作系统中,很多shell命令使用起来非 ...

  9. 让你提前认识软件开发(23):怎样在C语言中运行shell命令?

    第1部分 又一次认识C语言 怎样在C语言中运行shell命令? [文章摘要] Linux操作系统具备开源等诸多优秀特性,因此在很多通信类软件(主流开发语言为C语言)中,开发平台都迁移到了Linux上, ...

随机推荐

  1. 约翰·麦斯威尔 | John C. Maxwell | A leader is one who knows the way, goes the way, and shows the way.

    约翰·麦斯威尔_百度百科https://baike.baidu.com/item/%E7%BA%A6%E7%BF%B0%C2%B7%E9%BA%A6%E6%96%AF%E5%A8%81%E5%B0%9 ...

  2. flutter 权限申请

    添加依赖 permission_handler: ^3.2.2 androidmanifest.xml添加对应的用户权限 在flutter 中app入口申请权限: import 'package:fl ...

  3. PPT插件(islide)

    https://www.islide.cc/features iSlide 主要功能模块   一键优化 将PPT中不规则的字体,段落,色彩,参考线布局,风格样式等一键化全局统一设置,构建专业和规范. ...

  4. Flutter修改应用图标及图标

    应用名称是需要在 Android 和 iOS 中分别调整的:Android 是在 AndroidManifest.xml 中修改:iOS 则是在 Info.plist 中修改的: 如果需要更改应用的 ...

  5. visual studio code跳转到定义处插件

    visual studio code 中使用跳转到定义处的插件 https://marketplace.visualstudio.com/items?itemName=Shan.code-settin ...

  6. 算法习题---5-7打印队列(UVa12100)

    一:题目 有一个打印机,有一些任务在排着队打印,每个任务都有优先级.打印时,每次取出队列第一个任务,如果它的优先级不是当前队列中最高的,就会被放到队尾,否则就打印出来.输出初始队列的第m个任务的打印时 ...

  7. Qt编写数据可视化大屏界面电子看板13-基础版

    一.前言 之前发布的Qt编写的可视化大屏电子看板系统,很多开发者比较感兴趣,也收到了很多反馈意见,纵观市面上的大屏系统,基本上都是B/S结构的web版本,需要在后台进行自定义配置模块,绑定数据源等,其 ...

  8. 宣化上人:大佛顶首楞严经四种清净明诲浅释(10-11) -------------------------------------------------------------------------------- (转自学佛网:http://www.xuefo.net/nr/article23/230920.html)

    大佛顶首楞严经四种清净明诲浅释(10) 唐天竺·沙门般剌密帝译 宣化上人主讲 一九八三年四月十七日晚讲于万佛圣城 是故阿难.若不断偷修禅定者.譬如有人.水灌漏卮.欲求其满.纵经尘劫.终无平复. 是故阿 ...

  9. php 3.2 生成压缩文件,并下载

    public function zip_download() { $array = array( 'http://local.qki.com/site_upload/erweima/20190826/ ...

  10. 通过pathinfo返回扩展名

    strtolower(pathinfo(abs.php,PATHINFO_EXTENSION)); 小写 通过pathinfo返回扩展名 pathinfo() 函数以数组的形式返回文件路径的信息. p ...