groovy - Groovy 比较字符串
问题描述
我有如下的 Groovy 代码
def retVal = sh(returnStdout: true, script: "curl ${URI}; echo \$?")
println("Return value: ${retVal}") -> it printed 0
if (retVal == "0") {
println("Successfull") -> it doesn't go here
}
为什么无法捕捉到上述情况?
解决方案
首先,您似乎错误地使用了 Jenkins API。
如果您只需要进程的退出代码,请使用returnStatus: true
:
def retVal = sh(returnStatus: true, script: "curl ${URI}")
if (retVal == 0) {
println 'success'
} else {
println "Something wrong, exit code was $retVal")
}
现在,如果你真的想要标准输出,也许首先通过调用它来清理字符串trim()
,或者尝试将字符串与正则表达式匹配:
if (retValue ~== /\s*0\s*/) {
println "success"
} else {
println "Something wrong, exit code was '$retVal'")
}
我总是在我打印的值周围加上引号,以确保换行符或空格不会让我在错误的值上浪费时间。
推荐阅读
- python - '连接中止。',错误(32,'断管')
- r - rforcecom.getObjectDescription 对来 salesforce 数据表有错误
- html - 如何使用 htaccess 将请求重定向到一个文件夹到一个新的子域?
- grpc - 损坏的文件是无效参数吗?
- c++ - 在浮点数之后计算下一个更高整数的有效方法?
- excel - Powershell 在 Excel 中查找和替换
- c# - 如何让嵌套结构访问其父类的字段而不使这些字段公开或内部?
- google-apps-script - 根据单元格的颜色拆分行并解构逗号分隔的字符串
- php - 为什么他们会在下面的代码中退出 While 循环?
- python - 你如何将匀称的多面体转换为多边形