Issue
Here is my pipeline script where I'm calling one shell script and passing the arguments. In this $np and $cp contains special character i.e. # ex: abcdefgh#abd#we23:
def name = 'xyz'
def np = ''
def cp = ''
def pnumber = 'if.com'
def mnumber ='12'
def unumber= 'abc'
pipeline{
stages{
stage('stage1'){
steps{
script{
cp = sh(script: "cat file.txt", returnStdout: true)
np = sh(script: "cat file2.txt", returnStdout: true)
}
}
stage('stage2'){
steps{
script{
sh "./myscript.sh $name $mnumber $pnumber $unumber $np $cp"
}
}
}
}
Where as my script is:
#!/bin/bash
name=$1
pnumber=$2
mnumber=$3
unumber=$4
np=$5
cp=$6
sqlplus $unumber/$np/$cp/$unumber/$mnumber/$pnumber<<EOF
select date/$name;
exit;
EOF
Output of the pipeline script I'm getting is without the last argument. For example:
./myscript.sh abc xyz 123 User abcdefgh#abd#we23
it is not taking the last argument.
Request your help in letting me know what is wrong with my script. I'm happy to provide you any other detail if required. Thanks
Solution
The most simple is to add single quotes to shell side and come back to the problem when you have '
in the password or other variables:
sh "./myscript.sh $name $mnumber $pnumber $unumber '$np' '$cp'"
The most reliable is to pass everything with env variables and then shell can quote it properly.
environment {
name = name
number = number
pnumber = pnumber
unumber = unumber
cp = readFile "file.txt"
np = readFile "file2.txt"
}
script {
sh './myscript.sh "$name" "$mnumber" "$pnumber" "$unumber" "$np" "$cp"'
}
Or you can serialize ex. to json and read it in a shellscript (or in python because it can import json).
You might want to research the groovy string interpolation rules and shell quoting rules and shell expansions. Groovy interpolates the string, which is then evaluated by shell quoting. It is a nightmare. https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4
Then your script should be fixed too - it is missing quoting. Post it on shellcheck and implement suggestions.
Note that your files could contain a trailing newline. Shell command substitution $(...)
removes trailing newlines. You might want to remove them in groovy or in bash.
Answered By - KamilCuk Answer Checked By - Timothy Miller (WPSolving Admin)