March 27, 2011, 3:49 p.m.
posted by homene
Interacting with the User in Shell Scripts
In Chapter 25, "Harnessing the Power of the Shell," you learned to write shell scripts to perform sequences of commands without user intervention. Sometimes it is also helpful to be able to get user input as a shell script is runningto allow the user of the script to make choices about how tasks will be performed, for example.
The xmessage command can be used in shell scripts to enable the scripts to interact easily with the user via the mouse.
Using xmessage in Shell Scripts
To use xmessage in your shell scripts, you must call it from within a script by using the -buttons option and two arguments: a comma-separated list of buttons and the message that should be displayed to the user above the buttons. The format looks like this:
xmessage -buttons "button1,button2,button3" "My Message"
This command causes a dialog box with buttons labeled button1, button2, and button3 to appear below the message My Message. For example, the following call to xmessage displays the dialog box shown in Figure:
xmessage -buttons "Yes,No" "Would you like some ice cream?"
Figure. A dialog box created by calling the xmessage command.
When the user clicks one of the buttons beneath the message, the dialog box disappears and returns a value to the script, which is assigned to the special shell variable $?. If the first button is clicked, the value 101 is assigned to the variable. If the second button is clicked, the value 102 is assigned to the variable, and so on.
Sample xmessage Script
To illustrate the use of xmessage, let's study some sample scripts. These samples use techniques you learned in Chapter 25, so if you find yourself confused by the explanations, you should review Chapter 25 before continuing.
Suppose you want to create your own replacement for the rm command (used to remove files) that prompts the user with a confirmation dialog box before actually deleting a file. Listing 26.1 shows what a script called myrm, which does just this, might look like. Start an editor, create a file called ~/bin/myrm, and enter the listing. Then let's go over it line by line to see how it works.
Listing 26.1. Sample Script for myrm Command
1: #!/bin/sh 2: 3: xmessage -buttons "Yes,No" "Remove file $1?" 4: RESULT=$? 5: 6: if [ $RESULT -eq 101 ]; then 7: rm "$1" 8: fi
[[email protected] ~]$ chmod u+x ~/bin/myrm [[email protected] ~]$
Then try using the script to delete a file like myfile.tex. Entering the following command causes the dialog box shown in Figure to be displayed:
[[email protected] ~]$ myrm myfile.tex
Figure. The myrm command displays a dialog box asking whether you really want to delete the file you've supplied as an argument.
Second Sample xmessage Script
One more example might help illustrate a more dynamic use of the xmessage command. Suppose you have a set of LaTeX files in your home directory. If you don't, create some empty LaTeX files now for purposes of illustration using the touch command:
[[email protected] ~]$ touch myfile.tex yourfile.tex oldfile.tex newfile.tex [[email protected] ~]$
Now suppose you want to create a shell script that enables you to edit any one of your LaTeX files in an emacs window by typing texedit and selecting the name of your file using an xmessage dialog. To create this script, start a text editor window and enter Listing 26.2 as ~/bin/texedit. Then let's go over it line by line.
Listing 26.2. Sample Script for texedit Command
1: #!/bin/sh 2: 3: xmessage -buttons "$(ls -m *.tex)" "Which LaTeX file?" 4: RESULT=$? 5: 6: INDEX=100 7: for TEXFILE in *.tex; do 8: INDEX=$(expr $INDEX + 1) 9: if [ $RESULT = $INDEX ]; then 10: emacs $TEXFILE & 11: fi 12: done
[[email protected] ~]$ chmod u+x ~/bin/texedit [[email protected] ~]$
The dialog box in Figure appears after you enter the texedit command at the command prompt:
[[email protected] ~]$ texedit
Figure. The xmessage dialog box enables the user to choose between all the .tex files in the current working directory.