I am trying to check redirects for a bunch of my URLs via the following command:
curl -v -L http://<my-url> 2>&1 | grep -i "^< location:" | head -n 1
and it works as expected by returning the following output:
< Location: https://<my-url>:443/
Since I have a bunch of URLs I want to check, I wrote a bash script:
#!/bin/bash
declare -a domains=("http://my-url1.com" "http://my-url2.com" "http://my-url3.com")
for i in "${domains[@]}"
do
curl -v -L http://${i} 2>&1 | grep -i "^< location:" | head -n 1
done
But when I run the script via ./my-script.sh I am not seeing the output like < Location: https://<my-url>:443/
How can I get the following output?
< Location: https://<my-url1>:443/
< Location: https://<my-url2>:443/
< Location: https://<my-url2>:443/
and so on
>Solution :
You’ve got http:// in both http://${i} and the array of domains. When ${i} is expanded it results in invalid URLs such as http://http://my-url1.com.