noober noober - 5 months ago 17
Bash Question

bash - using getopts with an associative array in a bash script

I'm using

along with an
associate array
in my code below.
What I can't figure out is how to correctly call my values, given that I'm asking for (2) alias(s) using the same flag.

Here's what I have so far:


set -e

usage () {
echo "Usage: $0 -c alias1 -c alias2"

while getopts ":c:" opt; do
case $opt in
c) alias="$OPTARG";;
*) echo "Error unknown option -$OPTARG"
exit 1

# Testing use off array
declare -A alias=( [alias1] \
[alias2] \
[alias3] \
[alias4] )

echo "This is my source host:${alias}"
echo "This is my target host:${alias}"

This is how I would like to execute it (or better suggeested way):

-bash-4.1$ ./ -c alias1 -c alias4
This is my source host:
This is my target host:

Obviously, I'm not getting my expected result which would be this:

This is my source host:
This is my target host:

How can I do this? I want to pass in (2) aliases which uses my alias mapping (associative array) to grab the correct key value pair or maybe my approach is wrong and I can't use
twice? Thanks.

jil jil

You could use an array to store the aliases e.g.

while getopts ":c:" opt; do
    case $opt in
        c) aliases+=( "$OPTARG" );;
    # ...

And then you use the this array to index your associative array:

echo "This is my source host:${alias_to_host[${aliases[0]}]}"
echo "This is my target host:${alias_to_host[${aliases[1]}]}"

But if the input options are representing aliases for source and destination host and there always is exactly two hosts, it would be much cleaner to use separate options (e.g. -s and -d) and separate variables for these than reusing the switch -c:

# ...
    case $opt in
        s) source=$OPTARG;;
        d) destination=$OPTARG;;
        # ...
# ...

echo "This is my source host:${alias_to_host[$source]}"
echo "This is my target host:${alias_to_host[$destination]}"