-1

I wrote a simple shell script to list get me some stuff and perform a grep based on pattern:

kubectl get virtualservice -A | grep '*' | while read -r line ; do
    echo "$line"
done

The line output is:

whatsapp-business       whatsapp-business-web      ["istio-system/istio-ingressgw"]   ["*"]          374d

I would like to get first and second parameters to pass in a command like:

kubectl get virtualservice -A | grep '*' | while read -r line ; do
    echo "$line"
    # kubectl delete virtualservice whatsapp-business-web -n whatsapp-business
    # that line should be dynamic based on $line
done

Any idea how can I achieve this? Im kinda new to shell script.

0

1 Answer 1

0

To extract the first and second parameters from the line and use them dynamically in your script, you can make use of shell string manipulation. Here's an updated version of your script:

kubectl get virtualservice -A | grep '*' | while read -r line ; do
    echo "$line"
    first_param=$(echo "$line" | awk '{print $1}')
    second_param=$(echo "$line" | awk '{print $2}')
    kubectl delete virtualservice "$second_param" -n "$first_param"
done

In this script, we use awk to extract the first and second parameters from the line using space as the delimiter. The first_param variable will store the first parameter, and the second_param variable will store the second parameter. Then, you can use these variables dynamically in the kubectl delete command to delete the virtual service.

Sign up to request clarification or add additional context in comments.

1 Comment

Presuming a fixed number of parameters, wouldn't while read - r p1 p2 p3 p4 p5; do be easier? (you can add a 6th variable to the read, e.g. junk to catch all additional input, if any)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.