【发布时间】: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/…