1

I have command I am running with works for one enviornment in a bash script e.g.

so for example

ZONE_ID=prod

#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'

What i would like to do is change the data item if possible for a different environment depending on the zone_id

ZONE_ID=prod ZONE_ID=UAT

for example UAT would be

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'

So what I kind of want to do is the following, but this doesnt work

#!/bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
if [$ZONE_ID -q prod]; then
--data '{"purge_everything":true}'
else
--data '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'

beccamor
  • 15
  • 3

2 Answers2

0

I would recommend set variable before use it in curl command

example as follow

DATA=""
if [ "$ZONE_ID" = "prod" ]; then
DATA='{"purge_everything":true}'
else
DATA='{"files":["http://www.example.com/css/styles.css",{"url":"http://www.examp
le.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPC
ountry":"US","CF-Device-Type":"desktop"}}]}'
fi
 
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache"
 \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: AUTH_KEY" \
-H "Content-Type: application/json" \
--data $DATA
  • 2
    You should double-quote the variable (i.e. `--data "$DATA"`) to avoid problems if it contains spaces or certain other funny characters. Also, I recommend using lower- or mixed-case variable names to avoid conflicts with the many all-caps names with special meanings. – Gordon Davisson Feb 10 '22 at 18:29
  • Agreed for double quotes, but i will prefer all caps for variables – Kalpesh Chavan Feb 10 '22 at 18:33
  • 1
    See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375). – pjh Feb 10 '22 at 19:11
0

One possible way to do it is:

#! /bin/bash

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
    -H "X-Auth-Email: EMAIL" \
    -H "X-Auth-Key: AUTH_KEY" \
    -H "Content-Type: application/json" \
    --data   "$(if [[ $ZONE_ID == prod ]]; then
                    echo '{"purge_everything":true}'
                else
                    echo '{"files":["http://www.example.com/css/styles.css",{"url":"http://www.example.com/cat_picture.jpg","headers":{"Origin":"https://www.cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}'
                fi)"

However, a much better option (for clarity and maintainability) is to put the --data argument in a variable.

pjh
  • 6,388
  • 2
  • 16
  • 17