User friendly shell scripts with Dialog
Shell scripts are text files that contains list of commands to perform a specific task, frequently a repetitive task. A very good example of a shell script is the following bash script that batch resize images:
#!/bin/bash
# Batch resize jpg images.
# User must define size when using this script, e.g.
# ./batchresize 800x600
dir="resize-$1"
mkdir -p $dir
for file in `ls *.jpg`; do
echo "Processing $file"
convert -resize $1 $file $dir/$file
done
As you can see in the comment of the script the user must define the desired image size when running the script. That’s really simple, isn’t it? Yes, it’s simple for the users who know how they should run the script, but it isn’t for all those regular users who didn’t bothered to read the script source. What we need to do is making our scripts more User Friendly!
Now, how on earth could we do that? … Using Dialog, a utility used to create dialog boxes from within shell scripts, so now we can have more interactive shell scripts. Let’s start the User Friendliness writting down the dialog version of “Hello, world!”. Fire up your favorite text editor and write the file helloworld:
#!/bin/bash
# Hello, world! script with "dialog"
dialog --title "Message Box" \
--msgbox "Hello, world!" 6 25
The dialog command used in the script will draw a message box 6 lines high and 25 columns wide on the screen, containing nothing but the message “Hello, world !” and an OK button, that when it’s selected, ends the script. Save the file, make it executable (chmod u+x helloworld) and run it to see what happens (./helloworld):

With dialog we can also have text boxes, they are just like message boxes but used to display text files. The following script, named showcode, shows it’s source on a text box:
#!/bin/bash
# showcode script with "dialog"
# It shows it's very own source on a text box
dialog --title "Text Box" \
--textbox ./showcode 12 50
This example draws a text box 12 lines high and 50 columns wide, displaying the text on the file “showcode” and nothing more than a Exit button:

And how about an input box? Sure, why not, with dialog we can have some input boxes so the user can give some feedback.
#!/bin/bash
# inputbox script with "dialog"
# Shows an input box, and nothing more...
dialog --title "Input Box" \
--inputbox "Hello there!" 8 40 "Here you can share your feelings"

Now let’s make something interactive. This script ask the user’s name and displays a greeting message:
#!/bin/bash
dialog --title "Hi there" \
--inputbox "Please, tell me your name" 8 40 2>hithere.ans
name=$(cat hithere.ans)
rm hithere.ans
dialog --title "Hi there" \
--infobox "\nHi $name, keep it metal!" 8 40
There are a few things new here. First, the last dialog is an infobox, this is like a message box but it’s just informative, it doesn’t have any Ok button. Other new thing is that “2>hithere.asn” on the first dialog command, that is used to save the input of the inputbox to a text file named hithere.ans. The contents of the text file are then passed to the name variable and then the text file is deleted. We are doing that in order of getting the users input to show users name on the infobox. Try that one yourself and see what happens.
Now, remember that first batch resize script? Let’s make it user friendly and let the user select the desired resolution from a list. Check the code:
#!/bin/bash
# This batch resize script let the user
# select the resize resolution from a list
# using dialog's radiolist
dialog --backtitle "Batch Resize jpg files" \
--radiolist "Select the desired resolution:" \
10 40 3 \
1 640x480 on \
2 800x600 off \
3 1024x768 off \
2>radiolist.ans
ans=$(cat radiolist.ans)
rm radiolist.ans
if [ $ans = 1 ]; then
size="640x480"
elif [ $ans = 2 ]; then
size="800x600"
else
size="1024x760"
fi
dir="resized-$size"
mkdir -p $dir
for file in `ls *.jpg`; do
convert -resize $size $file $dir/$file
done

This examples show just a little preview of what can be done using Dialog. For more advanced information please refer to the dialog’s man pages or help.
0 comments
