shell 将参数传递给输入文件的脚本

shell script to pass argument to input file

我有 shell 脚本,它从用户那里获取一些输入并将该输入传递到我在 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.yamlcommand: ["python", "/script.py", "$Input1", "$input2"],我正在使用 运行 我的 myscript.sh 之后的用户输入。但是 $input1$input2 都没有填充我的值

我做错了什么?

不是最好的解决方案,但如果您想以这种方式使用 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//g" | sed "s/input2//g"

我猜你想要的是这样的。

myscript.sh:

#!/bin/bash
[[ "${#}" -ne 2 ]] && {
    echo "Usage: [=10=] <something_something> <something_else>" 1>&2;
    exit 1;
};
template="/path/to/de_pod.yaml";
my1stinput=""; printf -v my1stinput '%q' "";
my2ndinput=""; printf -v my2ndinput '%q' "";
sed -e "s/$Input1/${my1stinput}/g" -e "s/$Input2/${my2ndinput}/g" "${template}" | kubectl create -f - ;

如果 2 个参数中的值是复数,则应额外考虑确保它们在 sed 模式中正确转义。