0

I'm trying to make a simple script to switch GCP Projects with GCLOUD in a similar vein to KUBECTX\NS does with KUBECTL.

Thus, simply to take a positional script parameter ($1) value to access the project-id value from a predefined variable.

so executing such as: > sh gcp-proj.sh dev

e.g.

#!/bin/bash    
DEV="google-project-dev"
TEST="google-project-test"
gcloud config set [this would be one of the above values, but using the variable name referenced from $1]

How do I map $1 (dev) to the DEV variable and put the variable value into the above placeholder?

1
  • The target i'm trying to achieve is: user parameter can be: dev, test, prod the script will have variables of dev, test, prod with values such as "gcp-project-dev", "gcp-project-test", "gcp-project-prod" respectively. Thus, when the user inputs "dev", the $1 parameter will match to the variable name and obtain the value of "gcp-project-dev" and populate into the command "gcloud config set gcp-project-dev". Whilst useful for other use cases, I don't believe the answer below reflects that functionality. Many thanks. Commented Jun 1, 2022 at 9:27

1 Answer 1

0

Following your placeholder, this would look like this

#!/bin/bash

DEV=${1:-google-project-dev}
gcloud config set $DEV

i.e: first argument or google-project-dev

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

3 Comments

Should than not be set $DEV ?
@Zak Good catch. You are right.
Thanks for the reply. I don't want to be able to use the actual first argument of the command line (so "dev" would not be a valid project name, but you substitute "dev" with the value in the "dev" variable when executing the gcloud command. Futhermore, $DEV could point to any of the other variables; in the example TEST.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.