Search code examples
bashinputprompt

Bash shell script to prompt user for input with a (CLI GUI) dialog box


Hi and first of all thanks to all those that give helpful answers/pointers!!

I'm writing a Bash script that at times may need to ask the user for input. I'm looking for a tool to present a input box (think GUI pop up message box) at runlevel 3 (no X Windows) that provides a selection list and option for manual input. It should provide a radio button next to each option.

For example if it were asking the user for a name it would present a list like this


(*) Johnny

( ) Ben

( ) Hillary.

( ) <manual input>


Where the <manual input> line would be a text box to allow the user to enter free text. The output should be to standard out and should be the name e.g. "Hillary".

In summary the requirements are:

1) It should pop up and be a bright color to grab the user's attention. Flat text is not suitable

2) Preferably allow selection using the cursor or a mouse

3) Provide a radio button selection list

4) Also allow for free text input

5) Write the selection to standard out

6) I don't want to wipe the status/log messages already on the screen

Bash's read command is not "flashy" enough, it doesn't grab the user's attention.

The tool dialog wipes the text already on the screen as it re-draws the whole screen and does not allow for both a selection list and a free text input box in one screen.

Thanks again

MC


Solution

  • as sugested above dialog commands would work

    Create a dialog with --keep-tite to keep your screen after input and using --checklist makes it posible to have radio select box. Place all the answers into a array and have a while loop echo each array item.

    ${#arrayName[@]} --> number of items in array or number of selection ${arrayName[@]} --> outputs all array items

    #!/bin/bash
    
    result() {
    i=0
    echo there are ${#arrayName[@]} options selected
    while (( i < ${#arrayName[@]} ))
    do
      echo $i ${arrayName[$i]} 
      i=$(( $i + 1 ))
    done
    }
    
    cmd=(dialog --separate-output --keep-tite --checklist "Select options:" 22 76 4)
    options=(1 "Johnny" off
             2 "Ben" off
             3 "Hillary" off
             4 "User Input" off
    )
    
    choice=$("${cmd[@]}" "${options[@]}" 2>&1 > /dev/tty )
    
    for answer in $choice
    do
    # make decsion 
    case $answer in
        1) 
           arrayNum=${#arrayName[@]}  # arrayNum is the amount of items in arrayName 
           arrayName[$arrayNum]="Johnny" # if selected put Johnny in arrayName
           ;;
        2) 
           arrayNum=${#arrayName[@]}  # If johnny is selected ${#arrayName[@]} outputs 1 if not selected 0 
           arrayName[$arrayNum]="Ben" # If selected add to array
           ;;
        3) 
           arrayNum=${#arrayName[@]}
           arrayName[$arrayNum]="Hillary"
           ;;
        4) # If User Input is selected create an new dialog inputbox 
    
        user_input=$(\
        dialog --keep-tite --title "Enter Your Name" \
            --inputbox "Enter name:" 8 40 \
        3>&1 1>&2 2>&3 3>&- \
        )
           arrayNum=${#arrayName[@]}
           arrayName[$arrayNum]="$user_input"
           ;;
    esac
    done
    result