Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement