1

I am trying to create one loop that will iterate through many Array Variables by increasing the number in the Array name dynamically (i.e Array_1, Array_2, Array_...) by using integer variable (i.e Array_$c) that I can increase (++) inside for loop?

Thank you!

#!/bin/bash

declare -a Array_1=("Google", "www.google.com")
declare -a Array_2=("Facebook", "www.facebook.com")
daclare -a Array_3=("Gmail", "www.gmail.com")

c=1

for i in "${Array_$c[@]}";

  do

     #Print name of the Website ($Array_1[0])
     #Open link in FF ($Array_1[1]) 
     #Increase the $c, to iterate through the second Array using same loop
     (c=$c+1)

  done
0

3 Answers 3

2

based on this usage, you don't even need arrays. I would write this way instead

$ while read site url; 
  do echo "site:" $site; 
     echo "url:" $url;   # do something with url
  done << EOF
google www.google.com
facebook www.facebook.com
gmail www.gmail.com
EOF



site: google
url: www.google.com
site: facebook
url: www.facebook.com
site: gmail
url: www.gmail.com
Sign up to request clarification or add additional context in comments.

Comments

1

Of course, it is possible to do it with the structure you propose:

#!/bin/bash

declare -a Array_1=("Google" "www.google.com")
declare -a Array_2=("Facebook" "www.facebook.com")
declare -a Array_3=("Gmail" "www.gmail.com")

for u in "${!Array_@}"; do
    onev=${u}[0]
    twov=${u}[1]
    echo "$u=${!u}"
    echo "name of the Website (${!onev})"
    echo "Link (${!twov})" 
done

But there is no need to make it complex with all of this:

List of variables:    "${!Array_@}"
Access to one index:  "onev=${u}[0]"
Indirection:          "$u=${!u}"

This script is simpler (and performs about the same job):

#!/bin/bash
while IFS=$' \t\n' read -r name link; do
    echo "name of the Website ($name)"
    echo "               Link ($link)" 
done <<_list_of_sites_
Google      www.google.com
Facebook    www.facebook.com
Gmail       www.gmail.com
_list_of_sites_

2 Comments

Thanks @BinaryZebra ! I have used your first example.
0

You're almost there.

The usual method for making variable names out of other variables is to use eval. There will be people who describe eval as "evil", and something to be avoided, but there are situations where it can be safe, and some situations where it is inevitable.

Here's something based on your script excerpt:

#!/bin/bash

declare -a Array_1=("Google" "www.google.com")
declare -a Array_2=("Facebook" "www.facebook.com")
declare -a Array_3=("Gmail" "www.gmail.com")

for i in {1..3}; do

    eval printf \"Web site: %s\\n\" \"\${Array_$i[0]}\"
    eval /usr/local/bin/firefox \"\${Array_$i[1]}\"

done

But I think this is probably the wrong approach to whatever it is you're trying to achieve. (What are you trying to achieve, anyway? :) )

3 Comments

Thanks @ghoti for the answer. I finally got the simple shell script. Basically I have to open many many many web-based tools every morning at work. So I decided to write a single shell script that will update/upgrade my system, and open a lot of different URLs. I had more than 15 different URLs (more than 15 variable). I had a script that was very static. I wanted to create only one for loop that not only traverse through all the indexes in the arrays but also dynamicaly switch to a next variable :) I am not an experience coder, probably there is a waaay better approach to this script :)
@Impact, I feel your pain, but I'd go with a different approach. In a Chrome bookmarks toolbar, you can right-click it and select "Open All Bookmarks In New Window". In Firefox, the equivalent option is "Open All In Tabs".
Thanks@ghoti. Good to know, never noticed that option. However,I will be adding more functionality to this script one I get more time. I want it to open different VPN tunnels, connect to bastian server,etc. I called this script goodMorning.sh :) so hopefully this script will do everything for me in the morning while I will be having my coffee.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.