2021-12-03

Split string and extract variables with shell script

Question

Given this single-line string:

PG_USER=postgres PG_PORT=1234 PG_PASS=icontain=and*symbols

What would be the right way to assign each value to its designated variable so that I can use it afterward?


Context

I'm parsing the context of a k8s secret within a CronJob so that I can periodically call a Stored Procedure in our Postgres database.

To do so, I plan on using:

PG_OUTPUT_VALUE=$(PGPASSWORD=$PG_PASSWD psql -qtAX -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DATABASE -c $PG_TR_CLEANUP_QUERY)

echo $PG_OUTPUT_VALUE

The actual entire helm chart I'm currently trying to fix looks like this:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: -tr-cleanup-cronjob
spec:
  concurrencyPolicy: Forbid
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
          - name: postgres
            secret:
              secretName: -postgres
          containers:
          - name: -tr-cleanup-pod
            image: postgres:12-alpine
            imagePullPolicy: Always
            env:
              - name: PG_PROPS
                valueFrom:
                  secretKeyRef:
                    name: -postgres
                    key: postgres.properties
            command:
              - /bin/sh
              - -c
              - echo "props:" && echo $PG_PROPS && PG_USER=$(grep "^PG_USER=" | cut -d"=" -f2-) && echo $PG_USER && PG_TR_CLEANUP_QUERY="SELECT something FROM public.somewhere;" && echo $PG_TR_CLEANUP_QUERY && PG_OUTPUT_VALUE=$(PGPASSWORD=$PG_PASSWD psql -qtAX -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DATABASE -c $PG_TR_CLEANUP_QUERY) && echo PG_OUTPUT_VALUE
            volumeMounts:
              - name: postgres
                mountPath: /etc/secrets/postgres

Current approach

As you can see, I'm currently using:

PG_USER=$(grep "^PG_USER=" | cut -d"=" -f2-)

That is because I initially thought the secret would be output on multiple lines, but it turns out that I was wrong. The echo $PG_USER displays an empty string.



from Recent Questions - Stack Overflow https://ift.tt/3Ek0A3E
https://ift.tt/eA8V8J

No comments:

Post a Comment