Results 1 to 10 of 16

Thread: Catching whiptail checklist results in bash script

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Beans
    7

    Catching whiptail checklist results in bash script

    I have googled the heck out of this, and read dozens of examples, but cant quite figure it out...

    I understand how the yes/no buttons work, and can catch those no problem, but I want to give several options to the user, and catch the output in my script, and do something with it.

    Code:
    #! /bin/bash
    
    whiptail --title "Test" --checklist "Choose:" 20 78 15 \
    "John" "" on \
    "Glen" "" off \
    "Adam" "" off \
    
    
    # If "John" is selected, run this function:
    function john {
    echo "You chose John"
    }
    
    # If "Glen" is selected, run this function:
    function glen {
    echo "You chose Glen"
    }
    
    # If "Adam" is selected, run this function:
    function adam {
    echo "You chose Glen"
    }
    
    exit
    I can see the output at the command line when the above script ends, but how can I use it inside the script?

    Also, using the option "--seperate-output" seems to totally suppress the output, rather than seperate it on different lines like it is meant to.

    I would appreciate any help

  2. #2
    Join Date
    Jun 2005
    Location
    Toronto, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Catching whiptail checklist results in bash script

    Checklist sends its output to stderr, so you need to catch it somehow. Not sure if this is the best way, but here is one method:
    Code:
    #!/bin/bash
    
    whiptail --title "Test" --checklist --separate-output "Choose:" 20 78 15 \
    "John" "" on \
    "Glen" "" off \
    "Adam" "" off 2>results
    
    while read choice
    do
    	case $choice in
    		John) echo "You chose John"
    		;;
    		Glen) echo "You chose Glen"
    		;;
    		Adam) echo "You chose Adam"
    		;;
    		*)
    		;;
    	esac
    done < results

  3. #3
    Join Date
    Dec 2012
    Beans
    7

    Re: Catching whiptail checklist results in bash script

    Thanks so much for your help. When I get a chance to test I'll come back and mark as solved.

    Cheers.

  4. #4
    Join Date
    Dec 2012
    Beans
    7

    Re: Catching whiptail checklist results in bash script

    Thanks.

    Your code worked great, and I really appreciate your help.

    Whiptail menus are the final touch on a multiinstaller program similar to Tasksel, but greatly expanded, that I am writing primarily for Raspbian, but will work on any Debian based system, so will test it on Ubuntu too, and make it available.

    I tweaked a little to suit my needs, and heres the working sample code:

    Code:
    #!/bin/bash
    
    function firstroutine {
     echo "running johns function"
    }
    
    function secondroutine {
     echo "running glens function"
    }
    
    
    function thirdroutine {
     echo "running adams function"
    }
    
    whiptail --title "Test" --checklist --separate-output "Choose:" 20 78 15 \
    "John" "" on \
    "Glen" "" off \
    "Adam" "" off 2>results
    
    while read choice
    do
            case $choice in
                    John) firstroutine
                    ;;
                    Glen) secondroutine
                    ;;
                    Adam) thirdroutine
                    ;;
                    *)
                    ;;
            esac
    done < results

  5. #5
    Join Date
    Dec 2012
    Beans
    7

    Re: Catching whiptail checklist results in bash script

    The Thread Tools menu does not contain a 'Solved' function for me to set.

    Can someone educate me, or if I simply don't have access because I'm a n00bie, can someone please set as solved.

    Thanks for all the fish.

  6. #6
    BlinkinCat is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Aug 2011
    Beans
    Hidden!

    Re: Catching whiptail checklist results in bash script

    Quote Originally Posted by sixfootseven View Post
    The Thread Tools menu does not contain a 'Solved' function for me to set.

    Can someone educate me, or if I simply don't have access because I'm a n00bie, can someone please set as solved.

    Thanks for all the fish.
    Hi,

    You as the OP have the ability of marking as solved.
    Simply use the Thread Tools Menu at top of page and select "Mark as Solved"

    Cheers -

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Catching whiptail checklist results in bash script

    Quote Originally Posted by sixfootseven View Post
    I tweaked a little to suit my needs, and heres the working sample code:
    This method creates a temp file (results). Don't forget to delete it.

    The basic method of dealing with temp files is something like:

    Code:
    ...
    tempfile=$(mktemp)
    trap 'rm -f "$tempfile"' EXIT
    
    whiptail ... 2> "$tempfile"
    
    while read -r line
    do
        ...
    done < "$tempfile"
    ...
    But, in this case there is no need for a temp file. You can pipe directly the stderr of whiptail to the while loop.

    Code:
    whiptail ... 2>&1 | while read -r choice ...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •