动态命令行参数没有传递给脚本。

huangapple go评论86阅读模式
英文:

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 = &#39;xyz&#39;
def np = &#39;&#39;
def cp = &#39;&#39;
def pnumber = &#39;if.com&#39;
def mnumber =&#39;12&#39;
def unumber= &#39;abc&#39;
pipeline{
 stages{

stage(&#39;stage1&#39;){
steps{
  script{
      cp = sh(script: &quot;cat file.txt&quot;, returnStdout: true)
      np = sh(script: &quot;cat file2.txt&quot;, returnStdout: true)
     }
}

stage(&#39;stage2&#39;){
steps{
  script{
      sh &quot;./myscript.sh $name $mnumber $pnumber $unumber $np $cp&quot;
     }
}
}
}

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&lt;&lt;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命令中添加单引号,并在密码或其他变量中使用&#39;时返回到问题:

sh "./myscript.sh $name $mnumber $pnumber $unumber '&#39;$np&#39;' '&#39;$cp&#39;'"

最可靠的方法是使用环境变量传递所有内容,然后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 &#39; in the password or other variables:

sh &quot;./myscript.sh $name $mnumber $pnumber $unumber &#39;$np&#39; &#39;$cp&#39;&quot;

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 &quot;file.txt&quot;
    np = readFile &quot;file2.txt&quot;
}
script {
    sh &#39;./myscript.sh &quot;$name&quot; &quot;$mnumber&quot; &quot;$pnumber&quot; &quot;$unumber&quot; &quot;$np&quot; &quot;$cp&quot;&#39;
}

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.

huangapple
  • 本文由 发表于 2023年8月8日 20:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定