In a simple case, you would do something like this:
CHOICES=( $(whiptail --title "Tools to install" \
--checklist "Choose something" 20 78 4 \
"NTP" "NTP setup" OFF \
"Perl" "Perl install" OFF \
"Ruby" "Ruby install" OFF \
"Python" "Python install" OFF 3>&1 1>&2 2>&3) )
However, because the values returned by whiptail
are quoted, this won't work. For example, this script:
CHOICES=( $(whiptail --title "Tools to install" \
--checklist "Choose something" 20 78 4 \
"NTP" "NTP setup" OFF \
"Perl" "Perl install" OFF \
"Ruby" "Ruby install" OFF \
"Python" "Python install" OFF 3>&1 1>&2 2>&3) )
for choice in "${CHOICES[@]}"; do
echo "choice: $choice"
done
Will output (if I choose Ruby and Python):
choice: "Ruby"
choice: "Python"
And if one of your values contains whitespace, it will fall apart. For example, if I modify the command line like this:
CHOICES=( $(whiptail --title "Tools to install" \
--checklist "Choose something" 20 78 4 \
"NTP Setup" "NTP setup" OFF \
"Perl install" "Perl install" OFF \
"Ruby" "Ruby install" OFF \
"Python Install" "Python install" OFF 3>&1 1>&2 2>&3) )
for choice in "${CHOICES[@]}"; do
echo "choice: $choice"
done
We get:
choice: "NTP
choice: Setup"
choice: "Perl
choice: install"
You can add an eval
to the mix to take care of both the above problems:
eval CHOICES=( $(whiptail --title "Tools to install" \
--checklist "Choose something" 20 78 4 \
"NTP Setup" "NTP setup" OFF \
"Perl install" "Perl install" OFF \
"Ruby" "Ruby install" OFF \
"Python Install" "Python install" OFF 3>&1 1>&2 2>&3) )
for choice in "${CHOICES[@]}"; do
echo "choice: $choice"
done
Given the same selections as the previous example, this will output:
choice: NTP Setup
choice: Perl install
readarray
section of thebash
man page helpful? linux.die.net/man/1/bash