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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 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. – Daemon Jester Jun 01 '22 at 09:27

1 Answers1

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

c3b5aw
  • 61
  • 4
  • Should than not be `set $DEV` ? – Zak May 31 '22 at 16:24
  • @Zak Good catch. You are right. – c3b5aw May 31 '22 at 16:27
  • 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. – Daemon Jester May 31 '22 at 17:28