Home > Enterprise >  Compress bash script to one line
Compress bash script to one line

Time:03-30

I'm trying to get GCP region information and based on that picking a GCS bucket to pull data from. The script looks like this.

#!/bin/bash
set -xe

GCP_REGION=$(curl -sf -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/zone | cut -d/ -f 4 | sed 's/.\{2\}$//')
BUCKET_NAME="foo-bar-"

case $GCP_REGION in
  us*)      BUCKET_NAME ="us";;
  asia*)    BUCKET_NAME ="asia";;
  europe*)  BUCKET_NAME ="eu";;
  *)        echo -n "unknown GCP region"; exit 1;;
esac

echo -n $BUCKET_NAME

Is there a way to compress it to one-liner statement?

CodePudding user response:

Get rid of the shebang, add a ; at the end of every non-empty line that doesn't end in ;; or in, then join all the lines together:

set -xe; GCP_REGION=$(curl -sf -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/zone | cut -d/ -f 4 | sed 's/.\{2\}$//'); BUCKET_NAME="foo-bar-"; case $GCP_REGION in us*)      BUCKET_NAME ="us";; asia*)    BUCKET_NAME ="asia";; europe*)  BUCKET_NAME ="eu";; *)        echo -n "unknown GCP region"; exit 1;; esac; echo -n $BUCKET_NAME;

We'll have to agree to disagree on the "look neat" aspect :-). Good luck!

  • Related