時間:2021/07/01
事件:測試在寫腳本時需要向POD的文件中增加內容\
問題:執行 kubectl exec -it podname -n namespace -- echo "hello world" >> /etc/profile
沒有寫到POD的/etc/profile 文件,寫入到了本地的/etc/profile文件
在測試無法解決的時候需要找到我
解決:
模擬問題:
創建POD
kubectl exec -it podname -n namespace -- echo "hello world" >> /etc/profile
模擬寫入
kubectl exec -it nginx -n default -- bash `echo "hello world" >> /etc/profile`
裂開
嘗試
kubectl exec -it nginx -n default -- bash -c "echo "hello world" >> /etc/profile"
hello
裂開
再嘗試
kubectl exec -it nginx -n default -- bash -c "echo hello world >> /etc/profile"
問題解決
反思
如果用-c 那么bash 會從第一個非選項參數后面的字符串中讀取命令,如果字符串有多個空格,第一個空格前面的字符串是要執行的命令,也就是$0, 后面的是參數,即$1, $2....