简体   繁体   中英

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 script myscript.sh

kubectl create -f de_pod.yaml

here is 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 . but both $input1 and $input2 is not populating my value

what i am doing wrong?

Not the best solution, but if you want to use the pod deployment this way, then it should work. To generate yaml files with different parameters, usually use 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"

What i suspect you want is something like this.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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