我有 shell 腳本,它從用戶那里獲取一些輸入并將該輸入傳遞給我在 shell 腳本中使用的檔案
外殼腳本 myscript.sh
kubectl create -f de_pod.yaml
這是 de_pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
restartPolicy: Never
containers:
- name: run-esp
image: myimage:1
command: ["python", "/script.py", "$Input1", "$input2"]
imagePullPolicy: Always
stdin: true
tty: true
這就是我運行腳本的方式
sh myscript.sh my1stinput my2ndinput
如果您查看此處的de_pod.yaml行command: ["python", "/script.py", "$Input1", "$input2"],我將在運行我的myscript.sh. 但兩者$input1并$input2沒有填充我的價值
我在做什么錯?
uj5u.com熱心網友回復:
我懷疑你想要的是這樣的。
我的腳本.sh :
#!/bin/bash
[[ "${#}" -ne 2 ]] && {
echo "Usage: ${0} <something_something> <something_else>" 1>&2;
exit 1;
};
template="/path/to/de_pod.yaml";
my1stinput=""; printf -v my1stinput '%q' "${1}";
my2ndinput=""; printf -v my2ndinput '%q' "${2}";
sed -e "s/\$Input1/${my1stinput}/g" -e "s/\$Input2/${my2ndinput}/g" "${template}" | kubectl create -f - ;
如果 2 個引數中的值是復數值,那么應該額外考慮確保它們在sed模式中正確轉義。
uj5u.com熱心網友回復:
不是最好的解決方案,但如果您想以這種方式使用 pod 部署,那么它應該可以作業。生成不同引數的yaml檔案,通常使用Helm charts
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
restartPolicy: Never
containers:
- name: run-esp
image: myimage:1
command: ["python", "/script.py", "input1", "input2"]
imagePullPolicy: Always
stdin: true
tty: true
kubectl create -f de_pod.yaml | sed "s/input1/$1/g" | sed "s/input2/$2/g"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/390147.html
標籤:linux 猛击 壳 Kubernetes kubectl
