[英]Execute bash command in jenkinsfile groovy
请帮忙
在这里,我有一部分 Jenkinsfile 像
@Library('groovy_shared_libraries')_
stage("Ensure droplet don`t exist and Create DO Droplet") {
// Ensure droplet don`t exist
ExistDroplet = sh(
script: "doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l"
returnStdout: true
).trim()
我如何在 jenkinsfile groovy 中执行这个 bash 命令?
doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l
使用当前的实现,它返回一个错误
WorkflowScript: 26: unexpected char: '\' @ line 26, column 63.
te droplet list | awk '{gsub(/\./, "", $
如果我在命令中添加额外的 \ 我有这个错误
WorkflowScript: 26: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
script: `"doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l",`
添加转义后 $2 jenkins 显示此错误
WorkflowScript: 26: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
script: `"doctl compute droplet list | awk '{gsub(/\\./, "", \$2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print \$2 }' | wc -l",`
这一切都适用于这个命令doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l
doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l
doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l
但我需要添加
awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g")
\
是 shell 和 Jenkins 的转义字符。 如果您想将反斜杠字符发送到 shell 您需要使用另一个用于 Jenkins 的反斜杠字符(注意\\
):
script: "doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.