【问题标题】:sed unable to parse multi-line string (Jenkins) parametersed 无法解析多行字符串 (Jenkins) 参数
【发布时间】:2021-05-11 02:21:03
【问题描述】:

我有 app_name 作为多行字符串参数,我想循环它以便为应用程序创建入口对象。

我将它们定义如下:

现在,我的 Jenkins 文件的 shell 脚本 sn-p 如下所示:

echo "${app_name}"
            for i in "${app_name}"; do
                kubectl delete ing ${i} -n${deployment_namespace}
                cp -apvf  /tmp/flexicache-ing.yaml /tmp/flexicache-ing-${i}.yaml
                pwd
                sed -i 's/#{deployment_ns}#/'"${deployment_namespace}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{app_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{chart_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                kubectl apply -f /tmp/flexicache-ing-${i}.yaml
            done

输出:

+ echo 'flexicache-data-processor
flexicache-incident-data-processor'
flexicache-data-processor
flexicache-incident-data-processor
++ tput bold
+ bold=''
++ tput sgr0
+ normal=''
+ for i in '"${app_name}"'
+ kubectl delete ing flexicache-data-processor flexicache-incident-data-processor -nsit1-blue
Error from server (NotFound): ingresses.extensions "flexicache-data-processor" not found
Error from server (NotFound): ingresses.extensions "flexicache-incident-data-processor" not found
+ cp -apvf /tmp/flexicache-ing.yaml /tmp/flexicache-ing-flexicache-data-processor flexicache-incident-data-processor.yaml
cp: target 'flexicache-incident-data-processor.yaml' is not a directory

我也尝试过使用数组,但似乎效果不佳。

需要了解我做错了什么。

P.S: 修改代码如下:

for i in ${app_name}; do
                kubectl delete ing ${i} -n${deployment_namespace}
                cp -apvf  /tmp/flexicache-ing.yaml /tmp/flexicache-ing-${i}.yaml
                pwd
                sed -i 's/#{deployment_ns}#/'"${deployment_namespace}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{app_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{chart_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                kubectl apply -f /tmp/flexicache-ing-${i}.yaml
            done

输出仍然失败:

+ sed -i 's/#{app_name}#/flexicache-data-processor
flexicache-incident-data-processor/' /tmp/flexicache-ing-flexicache-data-processor.yaml
sed: -e expression #1, char 40: unterminated `s' command

编辑 2:在 bt 仍然没有工作之前,我确实使用了

            while IFS= read -r i; do
                kubectl delete ing ${i} -n${deployment_namespace}
                cp -apvf  /tmp/flexicache-ing.yaml /tmp/flexicache-ing-${i}.yaml
                pwd
                sed -i 's/#{deployment_ns}#/'"${deployment_namespace}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{app_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                sed -i 's/#{chart_name}#/'"${app_name}"'/' /tmp/flexicache-ing-${i}.yaml
                kubectl apply -f /tmp/flexicache-ing-${i}.yaml
            done <<< "${app_name}"

sed 失败的输出...

+ sed -i 's/#{chart_name}#/flexicache-data-processor
flexicache-incident-data-processor/' /tmp/flexicache-ing-flexicache-data-processor.yaml
sed: -e expression #1, char 42: unterminated `s' command

编辑:尝试了下面的代码,没有任何魅力.....

[ec2-user@svcm268fa99bnp ~]$ echo ${app_name}
+ echo flexicache-data-processor flexicache-incident-data-processor
flexicache-data-processor flexicache-incident-data-processor
++ printf '\033]0;%s@%s:%s\007' ec2-user svcm268fa99bnp '~'
[ec2-user@svcm268fa99bnp ~]$ while IFS= read -r i; do cp -apvf /tmp/flexicache-ing.yaml /tmp/flexicache-ing-${i}.yaml; pwd; sed -i 's/#{app_name}#/'"${app_name}"'/' | tr '|' '\n' /tmp/flexicache-ing-${i}.yaml; done <<< "${app_name}"
+ IFS=
+ read -r i
+ cp -apvf /tmp/flexicache-ing.yaml /tmp/flexicache-ing-flexicache-data-processor.yaml
‘/tmp/flexicache-ing.yaml’ -> ‘/tmp/flexicache-ing-flexicache-data-processor.yaml’
+ pwd
/home/ec2-user
+ tr '|' '\n' /tmp/flexicache-ing-flexicache-data-processor.yaml
+ sed -i 's/#{app_name}#/flexicache-data-processor
flexicache-incident-data-processor/'
tr: extra operand ‘/tmp/flexicache-ing-flexicache-data-processor.yaml’
Try 'tr --help' for more information.
sed: -e expression #1, char 40: unterminated `s' command
+ IFS=
+ read -r i
+ cp -apvf /tmp/flexicache-ing.yaml /tmp/flexicache-ing-flexicache-incident-data-processor.yaml
‘/tmp/flexicache-ing.yaml’ -> ‘/tmp/flexicache-ing-flexicache-incident-data-processor.yaml’
+ pwd
/home/ec2-user
+ tr '|' '\n' /tmp/flexicache-ing-flexicache-incident-data-processor.yaml
+ sed -i 's/#{app_name}#/flexicache-data-processor
flexicache-incident-data-processor/'
tr: extra operand ‘/tmp/flexicache-ing-flexicache-incident-data-processor.yaml’
Try 'tr --help' for more information.
sed: -e expression #1, char 40: unterminated `s' command
+ IFS=
+ read -r i
++ printf '\033]0;%s@%s:%s\007' ec2-user svcm268fa99bnp '~'
[ec2-user@svcm268fa99bnp ~]$

【问题讨论】:

  • 为什么不呢?当问题显示缺乏基本的工具知识时该怎么办?这不是一个学习基础知识的学习平台 - stackoverflow 是一个供知道编程的人询问如何解决难题的平台。您可以在 mywiki.wooledge.org/BashGuide 学习 bash 脚本,tr 不将文件作为参数,请参阅 man tr。研究 shell 中双引号的作用以及何时引用字符串,研究单词吐词扩展。使用shellcheck.net 检查您的脚本
  • 对投票的抱怨揭示了对该平台如何运作的根本缺乏了解,并且很容易吸引额外的反对票。只需再次拨打tour 并查看help center,以便更好地了解该网站的运作方式,特别是投票是如何秘密的、个人的,并旨在为未来的访问者提供帮助。
  • 请不要repost exactly the same question. 我已经回答了,所以现在关闭它作为前一个的副本似乎没有实际意义;但实际上,在这个网站上呆了几年之后,你真的应该知道得更好。见meta.stackoverflow.com/questions/265233/…

标签: bash sed


【解决方案1】:

在同一个文件上重复运行 sed -i 是您要避免的反模式。

引用输入字符串使循环只运行一次,循环变量等于整个字符串。也许更好的安排是while read 循环,它本质上一次读取一行。

在循环内部,您想使用循环变量$i,而不是整个字符串$app_name。这就是你最后一次尝试的错误所在。

echo "$app_name"
while IFS= read -r i; do
    kubectl delete ing "$i" -n"$deployment_namespace"
    cp -apvf  /tmp/flexicache-ing.yaml /tmp/flexicache-ing-"$i".yaml
    pwd
    sed -e "s/#{deployment_ns}#/$deployment_namespace/" \
        -e "s/#{app_name}#/$i/" \
        -e "s/#{chart_name}#/$i/" /tmp/flexicache-ing.yaml \
            >/tmp/flexicache-ing-"$i".yaml
    kubectl apply -f /tmp/flexicache-ing-"$i".yaml
done <<<"$app_name"

这里的&lt;&lt;&lt; 字符串语法是特定于 Bash 的;如果你不能使用 Bash,试试echo "$app_name" | while IFS= read -r i ...

cp 命令可能是多余的;我把它留了下来,以防cp 的选项做了一些需要保留的重要事情,即使我们随后用新内容覆盖了复制的文件。

您可能想删除循环前的pwd,也许还有echo

变量名周围的大括号在这里没有什么用处;我将它们取出并添加了proper quoting。我还简化了sed 脚本中的引用;那里似乎不需要单引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多