0

I'm trying to let the user pick the array name because the arrays are sets of domain names.

while getopts h:c: option
do
      case "${option}"
      in
      c) client=${OPTARG};
      h) usage;;
esac
done

So like I want client=one of the customer arrays

customer1=(my_custdomain.com, my_custdomain2.com...)
custmoer2=(my1_custdomain.com, my1c_custdomain2.com...)

for i in client
do
      func_name
done

by now I'm really confused about expansions/quotes.

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
doc1623
  • 1,119
  • 1
  • 7
  • 5
  • Have a look at https://stackoverflow.com/questions/16553089/bash-dynamic-variable-names. – 4ae1e1 Nov 11 '15 at 03:19
  • 1
    It isn't clear to me what you're trying to do. You seem to have: a function `func_name` which is passed no arguments; a list of items containing just `client` that you're iterating over with a variable `i` that is never used; it isn't clear what you are storing in `$client` (is it a digit, or the name of the array). I assume that `custmoer2` is a typo; maybe you meant to use `for i in $client`. Maybe you're simply looking for the `${!var}` notation for indirect expansion. Maybe you need to read the [Bash manual](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion). – Jonathan Leffler Nov 11 '15 at 04:21
  • 1
    Please take a look: http://www.shellcheck.net/ – Cyrus Nov 11 '15 at 05:40

1 Answers1

2

You need indirect parameter expansion.

# No commas
customer1=(my_custdomain.com my_custdomain2.com)
customer2=(my1_custdomain.com my1c_custdomain2.com)

arrayref=$client[@]
for i in "${!arrayref}"; do
do
      func_name
done
chepner
  • 497,756
  • 71
  • 530
  • 681