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.

3
  • Have a look at stackoverflow.com/questions/16553089/…. Commented Nov 11, 2015 at 3: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. Commented Nov 11, 2015 at 4:21
  • 1
    Please take a look: shellcheck.net Commented Nov 11, 2015 at 5:40

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, Worked great. Thank U. Thank U. Thank U.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.