Home > OS >  Can you use an if else statement with Curl command in bash script
Can you use an if else statement with Curl command in bash script

Time:02-11

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"}}]}'

CodePudding user response:

You cannot place an if statement within arguments to a command. However, you can use a variable instead. You can set different value to a variable based on a condition. Example:

#!/bin/bash

if [[ "$ZONE_ID" = 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"}}]}'
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"

CodePudding user response:

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

CodePudding user response:

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.

  • Related