Unable to run shell script inside Jenkins pipeline

情到浓时终转凉″ 提交于 2019-12-12 18:25:51

问题


I'm able to run the following shell script but couldn't run from Jenkins pipeline code.

Try 1.

node('buildnode') {

def value = "Myvalue"

def key = "Mykey"

sh '''

DATA=$(printf "%-50s \"$key\"" "$value")

echo "$DATA"

'''

}

output:

++ printf '%-50s ' ''
+ DATA=' 

Try 2:

Tried with sh " " "

DATA=$(printf "%-50s \"$key\"" "$value")

echo "$DATA"

" " "

output: :

illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}"

Can someone help me?


回答1:


This should work.

node('buildnode') {
    def value = "Myvalue" 
    def key = "Mykey"

    sh """
    DATA=\$(printf "%-50s \"${key}\" \"${value}\"")
    echo "\$DATA"
    """
}

You also need to escape $ when calling new subshell under """ """

DATA=$(printf "%-50s \"${key}\" \"${value}\"")


来源:https://stackoverflow.com/questions/50350713/unable-to-run-shell-script-inside-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!