1

So, I am trying to use whiptail command to give users option to choose different things they want to install in their system. I use the whiptail --checklist command as below:

name =$(whiptail --title "Tools to install" --checklist 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)

Now, if my users choose Perl and python, this will output "Perl" and "Python". What I am looking for is to be able to convert these outputs into an array because ultimately I need to loop through this array. I need to feed these outputs as inputs to next command I am using.

Any help or lead will be highly appreciated!

Romeo Ninov
  • 16,541
  • 5
  • 32
  • 44
BishwashK
  • 23
  • 7

1 Answers1

2

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