I want to issue a bash command similar to this:
whiptail --title 'Select Database' --radiolist 'Select Database:' 10 80 2 \
1 production off \
2 localhost on
Whiptail is rather particular about how the radio list values are specified. They must be provided each on their own line, as shown. Here is a good article on this question.
The list of databases is available in a variables called DBS
, and ACTIVE_DB
is the radiolist item to highlight in the whiptail dialog.
Here is my current effort for building the command line. It is probably way too convoluted.
DBS="production localhost"
ACTIVE_DB="localhost"
DB_COUNT="$( echo "$DBS" | wc -w )"
DB_LIST="$(
I=1
echo ""
for DB in $DBS; do
SELECTED="$( if [ "$DB" == "$ACTIVE_DB" ]; then echo " on"; else echo " off"; fi )"
SLASH="$( if (( $I < $DB_COUNT )); then echo \\; fi )"
echo " $I $DB $SELECTED $SLASH"
echo ""
I=$(( I + 1 ))
done
)"
OPERATION="whiptail \
--title \"Select Database\" \
--radiolist \
\"Select Database:\" \
10 80 $DB_COUNT \"${DB_LIST[@]}\""
eval "${OPERATION}"
I get fairly close. As you can see, the expansion contains single quotes that mess things up, and backslashes are missing at some EOLs:
set -xv
++ whiptail --title 'Select Database' --radiolist 'Select Database:' 10 80 2 '
1 production off
2 localhost on '
The solution needs to provide a way to somehow know which selection the user made, or if they pressed ESC. ESC usually sets the return code to 255, so that should not be difficult, however this problem gets really messy when trying to retrieve the value of the user-selected radiolist item.