0

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?

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
  • https://stackoverflow.com/a/44055875/9072753 – KamilCuk Oct 18 '21 at 14:31
  • 1
    Just use an array. – Andrej Podzimek Oct 18 '21 at 14:56
  • 1
    @AndrejPodzimek Could you clarify. Do you mean the flags should be an array rather than a single string, as in this example: `args=(-s "$subject" --flag "arg with spaces") mail "${args[@]}"` http://mywiki.wooledge.org/BashFAQ/050 – Joshua Fox Oct 18 '21 at 15:36
  • @JoshuaFox, yes; the flags should be an array rather than a single string. – Charles Duffy Oct 18 '21 at 15:40
  • See also [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050) – Charles Duffy Oct 18 '21 at 15:41
  • (btw, note that all-caps variable names are used for variables that are meaningful to the shell and other OS-provided tools, whereas the namespace of variables with at least one lowercase character is reserved for application use; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a shell variable modifies any like-named environment variable). – Charles Duffy Oct 18 '21 at 15:44

1 Answers1

1
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[@]}"
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
Andrej Podzimek
  • 2,409
  • 9
  • 12
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section _Answer Well-Asked Questions_, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Oct 18 '21 at 15:43