Issue
In a script, I have two commands with basically the same flags, and I'd like to avoid repetition.
if ...
gcloud pubsub subscriptions create mysub \
--topic mytopic
--push-endpoint="$SUBSCRIPTION_ENDPOINT" \
--ack-deadline=$ACK_DEADLINE \
--max-delivery-attempts=$MAX_DELIVERY_ATTEMPTS \
--dead-letter-topic=$DEADLETTER_TOPIC \
--min-retry-delay=$MIN_RETRY \
--max-retry-delay=$MAX_RETRY
else
gcloud pubsub subscriptions update mysub
-push-endpoint="$SUBSCRIPTION_ENDPOINT" \
--ack-deadline=$ACK_DEADLINE \
--max-delivery-attempts=$MAX_DELIVERY_ATTEMPTS \
--dead-letter-topic=$DEADLETTER_TOPIC \
--min-retry-delay=$MIN_RETRY \
--max-retry-delay=$MAX_RETRY
fi
FLAGS="--push-endpoint=$SUBSCRIPTION_ENDPOINT \
--ack-deadline=$ACK_DEADLINE \
--max-delivery-attempts=$MAX_DELIVERY_ATTEMPTS \
--dead-letter-topic=$DEADLETTER_TOPIC \
--min-retry-delay=$MIN_RETRY \
--max-retry-delay=$MAX_RETRY"
But in that case, this is a single string, not multiple flags. I guess I need to split the string somehow (taking into account newlines and variable replacement).
What is the simplest way to do that?
Solution
if ...; then
COMMAND_PREFIX=(gcloud pubsub subscriptions create mysub)
else
COMMAND_PREFIX=(gcloud pubsub subscriptions update mysub)
fi
COMMAND=(
"${COMMAND_PREFIX[@]}"
"--push-endpoint=$SUBSCRIPTION_ENDPOINT"
"--ack-deadline=$ACK_DEADLINE"
"--max-delivery-attempts=$MAX_DELIVERY_ATTEMPTS"
"--dead-letter-topic=$DEADLETTER_TOPIC"
"--min-retry-delay=$MIN_RETRY"
"--max-retry-delay=$MAX_RETRY")
"${COMMAND[@]}"
Answered By - Andrej Podzimek