1

Here is the shell script I want to execute. for example, I have 100 kubernetes pods with 100 different vehicles.

I want a list to be generated of cars and choose 10 particular cars from the list.

My goal is to deploy a new image into the selected car's pod.

I am able to generate a list of cars through the array and grep.

#!/bin/bash     
array=$(kubectl get pods -n ns -o=name | grep "cars" )
declare array
INDEX=1
for i in ${array[@]}; do
    echo ${INDEX} $i
    INDEX=$(expr $INDEX + 1)  ## this will make a numbered list.
done

sample output

1 cars-bmw
2 cars-audi
3 cars-volkswagen
4 cars-jaguar
5 cars-ferrari
6 cars-toyota
7 cars-honda

How to achieve selection of cars through either name or just selecting the number of the car?

Please suggest if there is another way to do this. or suggest a better practice for the shell script.

while [[ read  -p {array[@]} ]]; do
    echo "choosing car " {array[@]}
    kubectl edit pod -n ns $podname -o yaml
done
exit 1
1
  • You haven't declared an array in your example. It should be array=($(kubectl get pods -n ns -o=name | grep "cars" )) And there is no need to declare it afterwards as assigning does declare. Commented Dec 7, 2019 at 23:45

2 Answers 2

0

You could use select:

select car in "${array[@]}" exit; do
    case $car in
        exit) break;;
        *)
            printf 'Choosing car: %s\n' "$car"
            kubectl edit pod -n ns "$car" -o yaml
        ;;
    esac
done

of course this would depend on you properly setting the array first by using the array=() syntax:

array=($(kubectl get pods -n ns -o=name | grep "cars" ))
0

Maybe something like can help to generate the list:

for (( i=1; i<=${#array[@]}; i++ ))
do
  echo $i ${array[$i]}
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.