Home > Back-end >  shell script to pass argument to input file
shell script to pass argument to input file

Time:12-23

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?

CodePudding user response:

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.

CodePudding user response:

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"
  • Related