4

I have the following dialog radiolist, which works fine.

#!/bin/bash

ch1a="1"
ch1b="Fri, 20/3/15"
ch2a="2"
ch2b="Sun, 21/6/15"
ch1="\"$ch1a\" \"$ch1b\""

dialog --title "Equinoxes and Solistices"  \
--radiolist "When is the Winter Solictice?" 15 60 4 \
"$ch1a" "$ch1b" ON \
"$ch2a" "$ch2b" OFF \
"3" "Wed, 23/9/15" OFF \
'4' 'Mon, 21/12/15' OFF 2>/tmp/menu.sh.$$

However, if I substitute the first choice with "$ch1" or "${ch1[@]}", even with different definitions of ch1, I get no result.

My final goal is to create a dynamic radiolist, with all the choices included is a string or an array. I have already tried this solution, but it does not work when the choices contain blanks.

Theo
  • 255

2 Answers2

4

The --radiolist needs two variables, not one. As explained in man dialog:

--radiolist text height width list-height  [ tag item status ] ...
      A  radiolist  box is similar to a menu box.  The only difference is
      that you can indicate which entry is currently selected, by setting 
      its status to on.

As you can see above, it needs both tag and item, as well as status. That's why it fails when you only give it one (even if that one is expanded to an array).

You could still use an array, but you'll need to do something like this:

#!/bin/bash

ch=( "1" "Fri, 20/3/15" "2" "Sun, 21/6/15" "3" "Wed, 23/9/15" 
     "4" "Mon, 21/12/15")

dialog --title "Equinoxes and Solistices"  \
--radiolist "When is the Winter Solictice?" 15 60 4 \
"${ch[0]}" "${ch[1]}" ON \
"${ch[2]}" "${ch[3]}" OFF \
"${ch[4]}" "${ch[5]}" OFF \
"${ch[6]}" "${ch[7]}" OFF 2>/tmp/menu.sh.$$
terdon
  • 242,166
  • Thank you for the clarification! I thought that an array with two elements is two strings (from the radiolist point of view).

    Is there any way to add more choices, using a for-loop, or by using the length of the array as a maximum of choices?

    – Theo Apr 21 '15 at 14:02
  • @Theo I don't know, but maybe not. I tried a few things and none of them worked. – terdon Apr 21 '15 at 14:31
2

I have been just working on a slightly more twisted case.

As Theo asks above, I was in the case of auto-filling the options in a loop. Shell escaping spaces can be tricky so this is what I did.

local LIST=()                                                                                                                                                                                                                                
for item in *.sh; do
  local DESC="$( grep "DESCRIPTION=" $item | sed 's|^DESCRIPTION=||' )"
  LIST+=( $( basename $item .sh ) "$DESC" off )
done
local script
script=$( dialog --backtitle "NextCloudPi configuration" \
                 --radiolist "Select program to configure and activate:" 20 80 10 \
                 "${LIST[@]}" \
          3>&1 1>&2 2>&3 )
echo "$script was selected"

$DESC is a shell variable that typically contains spaces and this is what makes this example complicated. How can I make dialog tell apart the spaces in $DESC from the spaces to separate its parameters?

I decided to use arrays, as they are a more natural approach to avoid this kind of problem anyways.

You can see the complete code and result in

https://ownyourbits.com/2017/03/05/generic-software-installer-for-raspbian/

See more about arrays versus space splitting at

http://zsh.sourceforge.net/FAQ/zshfaq03.html