I want to be able to call the parameters value from the shell and use it in my shell script. My priority is to not use If for every value selected in the params, but rather it choose the active choice. Is it possible as am having issue with this error?
line 4: ${params.prime_server}: bad substitution
#!/usr/bin/env groovy
properties([
parameters([
choice(
name: 'prime_server',
choices: ['','choice1', 'choice2', 'choice3','choice4'],
description: 'Name choice'
),
choice(
name: 'old_server',
choices: ['','choice1', 'choice2', 'choice3','choice4'],
description: 'Name choice'
)
])
])
stage('stage1') {
environment{
}
node("master") {
sh '''
echo -e "-------------------------------Calling the params value into shell----------------------------"
echo "${params.prime_server}"
echo "${params.old_server}"
oldserver="${params.old_server}"
primeserver="${params.prime_server}"
echo "${oldserver}"
echo "${primeserver}"
sed -i -e "0,/${oldserver}/ s/${oldserver}/${primeserver}/g" test.xml
'''.stripIndent()
}
}
Is there anyway to call this param value and assign it to variables in shell
CodePudding user response:
Shell block with single quotes does not take variables into consideration, so double quotes must be used. Example:
sh """
<your code>
"""
CodePudding user response:
You should be using double quotes if you want Groovy to do string interpolation. So change your shell block like below.
sh """
echo -e "-------------------------------Calling the params value into shell----------------------------"
echo "${params.prime_server}"
echo "${params.old_server}"
oldserver="${params.old_server}"
primeserver="${params.prime_server}"
echo "\${oldserver}"
echo "\${primeserver}"
sed -i -e "0,/${oldserver}/ s/${oldserver}/${primeserver}/g" test.xml
""".stripIndent()