0

I'm using the following script to purge cache from cdn,

#!/bin/bash

##  API keys ##
zone_id=""
api_key=""
login_id=""
akamai_crd=""

## URL ##

urls="$1"
[ "$urls" == "" ] && { echo "Usage: $0 url"; exit 1; }

echo "Purging $urls..."

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \
     -H "X-Auth-Email: ${login_id}" \
     -H "X-Auth-Key: ${api_key}" \
     -H "Content-Type: application/json" \
     --data "{\"files\":[\"${urls}\"]}"

#echo "CF is done now purging from Akamai ..."

echo  "..."

curl -v -s https://api.ccu.akamai.com/ccu/v2/queues/default -H "Content-Type:application/json" -d '{"objects":["$urls"]}' -u $akamai_crd

The 1st part for cloudflare is working fine the 2nd part when I pass it to Akamai

["$urls"]

I keep getting an error and it's passing the url as an argument it returns the variable itself ($urls) not the arg value.

I ran the script as following:

sh +x  script.sh  url

Any advise here?

6
  • I would suggest to this: SCRIPTNAME=$(basename "$0") if [ $# != 1 ] then echo "Usage: $SCRIPTNAME url" exit fi $urls="$2" Commented Jun 19, 2017 at 9:51
  • its working fine for the 1st curl , in the 2nd curl its not Commented Jun 19, 2017 at 9:54
  • Then change your second curl-command like this: --data "{\"files\":[\"${urls}\"]}" Commented Jun 19, 2017 at 10:19
  • thats worked for me thanks Commented Jun 19, 2017 at 10:34
  • please mark my answer. THX :) Commented Jun 19, 2017 at 10:39

2 Answers 2

1

First i would change this:

SCRIPTNAME=$(basename "$0") 
...
if [ $# != 1 ] then 
    echo "Usage: $SCRIPTNAME url" 
    exit 
fi 

$urls="$1"

Change your second curl command as the following (you need to escape the quotes):

--data "{\"files\":[\"${urls}\"]}"
Sign up to request clarification or add additional context in comments.

3 Comments

$2 was an fast copy and paste failure from me.
basename "$0" -> you get the scriptname wihtout "./"
FWIW, common convention is to print usage with a raw $0. See ls --help versus /bin/ls --help, for example.
1

Avoid creating JSON by hand like this; you can't guarantee that the resulting JSON is properly escaped. Use a tool like jq instead.

#!/bin/bash

##  API keys ##
zone_id=""
api_key=""
login_id=""
akamai_crd=""

## URL ##

url=${1:?Usage: $0 url}

headers=(
  -H "X-Auth-Email: $login_id"
  -H "X-Auth-Key: $api_key"
  -H "Content-Type: application/json"
)

purge_endpoint="https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache"

echo "Purging $url..."

jq -n --arg url "$url" '{files: [$url]}' | 
  curl -X DELETE "$purge_endpoing" "${headers[@]}" --data @-

#echo "CF is done now purging from Akamai ..."

echo  "..."

jq -n --arg url "$url" '{objects: [$url]}' | 
  curl -v -s -H "Content-Type:application/json" -d @- -u "$akamai_crd"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.