Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop through and manipulate array of strings in bash?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading