0

In the "hostmenu" function below I want the concatenated strings $site and $system to be used to identify which array will be used to build the next menu. The functions "sitemenu" and "sysmenu" set the two required variables needed to set $sitesystem. All arrays are defined outside of any functions. The problem I am having is that "$sitesystem" ends up empty when select goes to use it. An echo of "$sitesystem shows the positional parameters "site""system" (example: "siteasysb") instead of the strings defined in the array "$siteasysb". How can I have have select successfully use the "$sitesystem" variable?

#!/bin/bash
main ()
{
    sitemenu
    sysmenu
    hostmenu
}
## functions omitted which set $site and $system ##
hostmenu ()
{
    PS3="Please choose a $site $system device to connect to.."
    clear
    sitesystem=("$site""$system")
    echo $sitesystem
    select opt in "${sitesystem[@]}"
    do
    case $opt in
## Omitted selected host section ##
;;
"Return to previous system menu")
sysmenu
;;
"Return to site menu")
sitemenu
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
}
siteoptions=("sitea" "siteb" "Quit")
sysoptions=("sysa" "sysb" "Return to site menu" "Quit")
siteasysa=("a" "b" "Return to previous system menu" "Return to site menu" "Quit")
siteasysb=("a" "b" "Return to previous system menu" "Return to site menu" "Quit")

`main "$@"
zeeohsix
  • 3
  • 3

1 Answers1

0

To use the value of a variable as the name of another variable, put ! at the beginning of the variable name to indirect. So do:

Since you want the whole array with [@], you need to include that in $sitesystem

sitesystem="$site$system[@]"
select opt in "${!sitesystem}"

See How to iterate over an array using indirect reference?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks! It works like a charm. I need to read up on the logic of why it works still but that definitely did the trick. – zeeohsix Nov 10 '16 at 23:17