英文:
Dynamic command line arguments are not getting passed to the script
问题
这是我的流水线脚本,我在其中调用一个shell脚本并传递参数。在这个脚本中,$np和$cp包含特殊字符,例如#,例如: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"
}
}
}
}
而我的脚本是:
#!/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
我得到的流水线脚本的输出没有最后一个参数。例如:
./myscript.sh abc xyz 123 User abcdefgh#abd#we23
它没有接受最后一个参数。
请帮助我找出我的脚本有什么问题。如果需要,我可以提供其他详细信息。谢谢。
英文:
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
答案1
得分: 1
最简单的方法是在shell命令中添加单引号,并在密码或其他变量中使用'
时返回到问题:
sh "./myscript.sh $name $mnumber $pnumber $unumber ''$np'' ''$cp''"
最可靠的方法是使用环境变量传递所有内容,然后shell可以正确引用它:
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"'
}
或者你可以将其序列化为JSON格式,在shell脚本中读取它(或者在Python中读取,因为Python可以导入JSON)。
你可能需要研究一下Groovy的字符串插值规则、shell的引用规则和shell的扩展。Groovy插值字符串,然后由shell引用进行评估。这是一场噩梦。https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4
然后你的脚本也应该修复-缺少引用。将其发布到shellcheck并实施建议。
请注意,你的文件可能包含尾随换行符。Shell命令替换$(...)
会删除尾随换行符。你可能希望在groovy或bash中删除它们。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论