Bash - Getting Input From the User

Every once in a while you might need to write a bash script that needs to prompt user for some input (for example name, path, search keyword, etc..) and then process it. The easiest way to do this is to use the read command:

#!/bin/bash
read -p "What is your username? " -e input
echo Your name is $input

Sometimes however you want to be more fancy than this and use something more graphical to pretend that you are user friendly. The most basic application here is the ncurses based dialog:

#!/bin/bash
dialog --inputbox \
"What is your username?" 0 0 2> /tmp/inputbox.tmp.$$
retval=$?
input=`cat /tmp/inputbox.tmp.$$`
rm -f /tmp/inputbox.tmp.$$
case $retval in
0)
echo "Your username is '$input'";;
1)
echo "Cancel pressed.";;
esac

Dialog sends the user input into STDERR instead of STDOUT so you can’t easily pipe it, or capture it. Best way to deal with this is to redirect it into a file, and then read it in. Not the most elegant solution but it works. You can test the exit status (in bash stored in $) to see which button was pressed.

The output looks like this:

Dialog
click to enlarge

Xdialog is a GTK based drop-in replacement to dialog. We can easily reuse the code above and have a nice graphical window popup:

#!/bin/bash
Xdialog --title "INPUT BOX" --inputbox \
"What is your username?" 0 0 2> /tmp/inputbox.tmp.$$
retval=$?
input=`cat /tmp/inputbox.tmp.$$`
rm -f /tmp/inputbox.tmp.$$
case $retval in
0)
echo "Input string is '$input'";;
1)
echo "Cancel pressed.";;
255)
echo "Box closed.";;
esac

It uses the same kludgy logic, but the output is way prettier:

XDialog
click to enlarge

If you want something more elegant, you want to use zenity. It also uses GTK to produce a nice looking dialog box, but unlike the previous two examples it sends the output to STDERR so you don’t have to screw around with the files:

#!/bin/bash
input=$(zenity --text "What is your username?" --entry)
retval=$?
case $retval in
0)
echo "Input string is '$input'";;
1)
echo "Cancel pressed.";;
esac

The dialog itself is similar to that of Xdialog, but I think the ease of use makes zenity a much more desirable option:

Zenity
click to enlarge

In most cases zenity would be your best bet. But in cases where you know that X may not be running you should probably use dialog. The plain old read command may be the simplest choice, but it might confuse some users.

Related Posts:

  • Posting Twitter Updates via Curl
  • War on Terror using Bash
  • How to build a Crawler
  • Writing a Shell
  • Mount Remote Drives on KDE Startup With a Zenity Dialog
  • Bash Lotto Lookup
  • The Denoobization Script
  • Linux: Quick and Dirty Way to Take Screenshots
  • Time Logging Script
  • Few Useful Netcat Tricks

  • Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]