简体   繁体   English

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

[英]shell script to pass argument to input file

I have shell script which takes some input from user and pass that input to the file which i am using inside my shell script我有 shell 脚本,它从用户那里获取一些输入并将该输入传递给我在 shell 脚本中使用的文件

Shell script myscript.sh Shell 脚本myscript.sh

kubectl create -f de_pod.yaml

here is 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


this is how i am running the script这就是我运行脚本的方式

sh myscript.sh my1stinput my2ndinput

if you look at de_pod.yaml at line command: ["python", "/script.py", "$Input1", "$input2"] here i am using the user input after running my myscript.sh .如果您查看de_pod.yaml在 line command: ["python", "/script.py", "$Input1", "$input2"]这里我在运行myscript.sh后使用用户输入。 but both $input1 and $input2 is not populating my value$input1$input2都没有填充我的值

what i am doing wrong?我做错了什么?

Not the best solution, but if you want to use the pod deployment this way, then it should work.不是最好的解决方案,但如果您想以这种方式使用 pod 部署,那么它应该可以工作。 To generate yaml files with different parameters, usually use Helm charts生成不同参数的yaml文件,通常使用Helm图表

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"

What i suspect you want is something like this.我怀疑你想要的是这样的。

myscript.sh : myscript.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 - ;

If the values in the 2 arguments are complex values though, then some extra thought should be given to making sure they're properly escaped in the sed patterns.如果 2 arguments 中的值是复数,那么应该额外考虑确保它们在sed模式中正确转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM