0

I have a shell script with 3 Parameters:

#!/bin/bash

VHOST=$1
EXCHANGE=$2
DELAY=$3

RABBIT_CMD="docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=$VHOST"
COMPLEX_CMD="$RABBIT_CMD declare queue name=${EXCHANGE}-delay"
COMPLEX_CMD="$COMPLEX_CMD arguments="

COMPLEX_CMD="${COMPLEX_CMD}'{\"x-message-ttl\":$DELAY,\"x-dead-letter-exchange\":\"$EXCHANGE\", \"x-dead-letter-routing-key\":\"worker\"}'"
echo $COMPLEX_CMD
$COMPLEX_CMD

Now I call this script

./script.sh rdb blah 5000

The second last line echo $COMPLEX_CMD outputs the following line:

docker exec rabbit rabbitmqadmin -u rabbit -p rabbit --vhost=rdb declare queue name=blah-delay arguments='{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'

When I copy-paste this into my bash and execute it, it works without any problems. But when I want to execute this in the script (last line $COMPLEX_CMD), I get the following error:

ERROR: Could not parse JSON:
  '{"x-message-ttl":5000,"x-dead-letter-exchange":"blah","x-dead-letter-routing-key":"worker"}'

How do I have to escape my strings within the JSON in the right manner?

Torsten Fehre
  • 567
  • 2
  • 7
  • Possible duplicate of [Why does shell ignore quotes in arguments passed to it through variables?](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables) – oguz ismail Nov 29 '19 at 11:13

1 Answers1

-1

Found the solution: Just put eval before the $COMPLEX_CMD, so change the last line to

eval $COMPLEX_CMD
Torsten Fehre
  • 567
  • 2
  • 7