LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-30-2019, 04:13 PM   #1
michaelsavage
Member
 
Registered: Apr 2019
Distribution: Linux Mint
Posts: 59

Rep: Reputation: 11
Select which software to install via Dialog


I would like to select various software to install on my Linux Mint system via a bash script. I would like to use dailog --checklist because it shows a simple GUI that newbies can use. I can get the GUI to show the selection, but I do not know how to install the selected items. Below is an example of some software to install.

#!/bin/bash

dialog --checklist "Select needed software" 20 75 5 \
"Evince" "PDF Viewer" on \
"Pinta" "MS-Paint like Software " off \
"htop" "Shows running processes" off \
"vim" "txt editor" off

Thank you for your time and help.
 
Old 12-30-2019, 06:51 PM   #2
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
For you to install the software you need to add something like a case statement for each of the software you wish to have in the list for installation. I would go with something like this without the dialog as I have no clue as to what variable it returns for a selection.

Code:
#!/bin/bash


# Echo options to select
echo "Select Software Desied  1) Evince - PDF Viewer  2) Pinta - MS-Paint like Software  3) htop - Shows running processes  4) vim - txt editor  5) Quit "

# Read the input
read n

# Case statement to process according to input selected.
case $n in
       
               # Install Evince
              1) 
                        sudo apt install evince
              ;;

              # Install Pinta
             2) 
                        sudo apt install pinta
              ;;

              # Install htop
             3) 
                        sudo apt install htop
              ;;

              # Install vim
             4) 
                        sudo apt install vim
             ;;

              # Quit
             5) 
                        exit 0

# End case statement done processing
 esac
You could use an if then type of logic for then install too but you need to have something similar to my above to get it installed.

Last edited by HappyTux; 12-30-2019 at 06:53 PM.
 
Old 12-30-2019, 08:32 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I believe the bash return variable is how dialog returns the choice.

$?

Recommend you select various options from your list and check the return value to verify this.
 
Old 12-31-2019, 09:54 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
About not using dialog for this:
bash also has the "select" builtin, makes a nice numbered list.
Code:
help select
 
Old 12-31-2019, 11:12 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I do not believe the results from HappyTux and ondoho are GUI.

I've never used dialog, so I experimented a bit and did some web searching, because it's a really weird to use command.

Seems as if you can use the dialog specified, the output result goes to stderr. Which is file handle 2, you can redirect that to somewhere.

The answer there is really weird, to me. I guess it depends how well a person knows I/O redirection, I'm not really good at it.

See the top voted answer in this link, https://askubuntu.com/questions/4915...ariable#704616

They also explain a great deal how the I/O redirection actions occur.

This example code is along the lines of their recommendation:
Code:
exec 3>&1;
result=$(dialog --checklist "Select needed software" 20 75 5 \
"Evince" "PDF Viewer" on \
"Pinta" "MS-Paint like Software " off \
"htop" "Shows running processes" off \
"vim" "txt editor" off 2>&1 1>&3);
exitcode=$?;
exec 3>&-;
echo $result $exitcode;
The variable "result" will contain strings for each of the selected options. Checking the variable "exitcode" to determine that is is zero will indicate if there was a valid return from the dialog call.

A suggestion is to give those numbers to simplify your compare and then use a switch statement, and you do not need 5 options if you only have 4:
Code:
exec 3>&1;
result=$(dialog --checklist "Select needed software" 20 75 4 \
1 "Evince - PDF Viewer" on \
2 "Pinta - MS-Paint like Software" off \
3 "htop - Shows running processes" off \
4 "vim - txt editor" off 2>&1 1>&3);
exitcode=$?;
exec 3>&-;
echo $result $exitcode;
The variable result, when you select all choices becomes "1 2 3 4", and given bash's string processing capabilities, I'm sure you can treat this as an array or list and process each element in it, as I suggest, using a switch case, like what is shown in HappyTux's post.
 
Old 12-31-2019, 01:53 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
What happens if apt wants to ask for confirmation regarding install of dependencies?
What happens if an error occurs?

sudo won't work inside a Bash script - unless you are running the script as root already.

Sometimes it's easier to just type a single line with the selected package(s) on it into a CLI.
 
Old 12-31-2019, 03:30 PM   #7
michaelsavage
Member
 
Registered: Apr 2019
Distribution: Linux Mint
Posts: 59

Original Poster
Rep: Reputation: 11
Smile

With all of your help and guidance and a little searching on the interwebs, I've found the answer I was looking for. Please see the answer below.
Thank you all for your help!

Code:
#!/bin/bash

cmd=(dialog --separate-output --checklist "Select needed software:" 20 75 5)
options=(1 "Evince is a PDF Viewer" off 
         2 "Pinta is a MS-Paint like Software " off 
         3 "htop shows running processes" off
         4 "vim is a txt editor" off)

# get choices
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)

# Clear the screen
clear

# For statment
for choice in $choices
do
    case $choice in
    
    # Install Evince
    1) 
             sudo apt install -y evince
    ;;

    # Install Pinta
    2) 
            sudo apt install -y pinta
    ;;

    # Install htop
    3) 
            sudo apt install -y htop
    ;;

    # Install vim
    4) 
             sudo apt install -y vim
    ;;
    esac
done

Last edited by michaelsavage; 12-31-2019 at 04:56 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Dialog -returning to a previous dialog ArneVanP Linux - Newbie 5 12-28-2017 03:10 AM
Reproducing Alt-F2 run dialog effect (focus dialog box, dim and disable rest of desktop) in LinuxMint Cinnamon in other applications. kevin77v Linux - Desktop 0 01-20-2016 12:44 PM
Dialog: How to select files and copy files Manoel Júnior Programming 4 01-17-2016 09:32 AM
"Open" (select a file) dialog - single click vs. double click taylorkh Ubuntu 3 10-18-2011 03:00 AM
Keyboard model select dialog ?? watchdog Linux - Newbie 2 05-02-2004 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration