Search code examples
bashshellvariablesfor-loopwhiptail

Define a variable and input at the same time?


Excuse the confusing question. I'm not sure exactly how to phrase it?

I need to define a variable:

variable = "Description$count"

While at the same time using it as an input.. if that makes any sense?: --inputbox "$(Description$count)

for ((count=$(ovftool vco.ovf | grep -i -A 60 Properties: | grep Description: | wc -l); count > -1; count--))
do

        declare "DiagRes$Count"=$(whiptail --inputbox "$(Description$count)" 8 110 --title "$Label1" 3>&1 1>&2 2>&3)
done

Any help much appreciated


Solution

  • Rather than trying to build up variable names as something like "\$description$count", this seems like a good place to use arrays.

    Just define your array elements as follows:

    description[0]="First description"
    description[1]="Second description"
    

    And then in your loop, assign your results in an array, and extract your descriptions from the first array:

    diag_res[$i]=$(whiptail --inputbox "${description[$i]}" ...)
    

    Note that arrays are a Bash-specific feature, and aren't supported by all POSIX shells.