An input dialog box

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← A yes/no dialog boxHomeA password box →
  • An input box is useful when you want to ask questions that require the user to input a data as the answer via the keyboard.
  • When entering the data, the backspace, delete and cursor keys can be used to correct typing errors.
  • If the input data is longer than can fit in the dialog box, the input field will be scrolled.
  • On exit, the input string will be printed on dialog’s output. This can be redirected to a text file.

Example

  • Create a shell script called yesnobox.sh:
#!/bin/bash
# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"
 
# create empty file
>$OUTPUT
 
# Purpose - say hello to user 
#  $1 -> name (set default to 'anonymous person')
function sayhello(){
	local n=${@-"anonymous person"}
	#display it
	dialog --title "Hello" --clear --msgbox "Hello ${n}, let us be friends!" 10 41
}
 
# cleanup  - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM
 
# show an inputbox
dialog --title "Inputbox - To take input from you" \
--backtitle "Linux Shell Script Tutorial Example" \
--inputbox "Enter your name " 8 60 2>$OUTPUT
 
# get respose
respose=$?
 
# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)
 
# make a decsion 
case $respose in
  0) 
  	sayhello ${name} 
  	;;
  1) 
  	echo "Cancel pressed." 
  	;;
  255) 
   echo "[ESC] key pressed."
esac
 
# remove $OUTPUT file
rm $OUTPUT

Save and close the file. Run it as follows:

chmod +x yesnobox.sh
./yesnobox.sh

Sample outputs:


See also

← A yes/no dialog boxHomeA password box →