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!

4
  • Is the readarray section of the bash man page helpful? linux.die.net/man/1/bash
    – Jim L.
    Jul 12, 2022 at 16:40
  • Says 404 not found for that link
    – BishwashK
    Jul 12, 2022 at 16:41
  • It's an easy Google.
    – Jim L.
    Jul 12, 2022 at 16:42
  • It could have helped but it looks like it needs output as strings but whiptail gives output as "string". so, instead of perl, output is "perl". It did not work.
    – BishwashK
    Jul 12, 2022 at 16:49

1 Answer 1

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
2
  • Perfect! very well explained and this takes care of my issue. Much appreciated!
    – BishwashK
    Jul 12, 2022 at 17:57
  • If this answer helped out, consider checking the checkmark to the left of the question to mark it "answered". Jul 12, 2022 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .