Linux – curl command works as expected on CLI but not in a shell script

bashcurllinux

I have a series of curl commands I use to warm varnish using a script. The individual commands work just fine on the command line but when I put those same commands in a script they don't work.

#!/bin/bash
curl -o /dev/null --insecure -X PURGE -H 'X-Magento-Tags-Pattern: ((^|,)cat_p_123456(,|$))' https://www.example1.com/page1.html --resolve www.example1.com:443:127.0.0.1
curl -o /dev/null --insecure -I -X GET 'https://www.example1.com/page1.html'

On the command line I can run these commands individually and they work as expected. The first command purges the cache, the second warms the cache. When I visit the page after running these on the command line I get the expected hit cached.

If I put these in a shell script, make it executable and run with ./script.sh or even /bin/bash script.sh the purge works but the get doesn't. I get a cache miss.

Best Answer

Try this variant:

#!/bin/bash
curl -o /dev/null --insecure -X PURGE -H 'X-Magento-Tags-Pattern: ((^|,)cat_p_123456(,|$))' https://www.example1.com/page1.html --resolve www.example1.com:443:127.0.0.1 && curl -o /dev/null --insecure -I -X GET 'https://www.example1.com/page1.html'