Unable to run shell script inside Jenkins pipeline

2019-07-13 01:24发布

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条回答
冷血范
2楼-- · 2019-07-13 02:01

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}\"")
查看更多
登录 后发表回答