【发布时间】:2021-01-09 04:19:34
【问题描述】:
考虑以下 shell 脚本,其中 POD 设置为 K8 pod 的名称。
kubectl exec -it $POD -c messenger -- bash -c "echo '$@'"
当我使用一个参数运行这个脚本时,它运行良好。
hq6:bot hqin$ ./Test.sh x
x
当我使用两个参数运行它时,它会爆炸。
hq6:bot hqin$ ./Test.sh x y
y': -c: line 0: unexpected EOF while looking for matching `''
y': -c: line 1: syntax error: unexpected end of file
我怀疑参数的传递方式有问题。
我该如何解决这个问题,以便我的 shell 按字面意思扩展参数,然后作为文字传递给在 kubectl exec 中运行的 bash?
请注意,删除单引号只会导致输出 x。
另请注意,我需要bash -c,因此我最终可以传入文件重定向:https://stackoverflow.com/a/49189635/391161。
【问题讨论】:
标签: bash kubernetes kubectl positional-parameter