Reputation: 239
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?
Upvotes: 2
Views: 4928
Reputation: 106
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}\"")
Upvotes: 5