Interacting with the User in Shell Scripts





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

This short shell script performs a simple function, so dissecting it is fairly easy:

  • Line 1 begins with the standard header for a shell script file. This header is required for a shell script to work.

  • Line 3 uses xmessage to display a dialog box with Yes and No buttons. The message is simply the words Remove file. This message is followed by the first argument supplied by the user, which is substituted for $1.

  • Line 4 saves the value of the special variable $?, which contains the exit status of xmessage, to the variable RESULT. If the first button is clicked, the value of RESULT is 101. If the second button is clicked, its value is 102.

  • Lines 68 check to see whether the value of RESULT is 101. If so, it means that the user clicked Yes, so the file is deleted. If not, no action is performed; the script simply reaches its end, so it exits without deleting the file.

To try out this script, use the chmod command to make myrm executable:

[[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.


Clicking the Yes button at this point causes the file to be deleted. Clicking the No button leaves the file intact.

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

This script is also relatively short, but its operation is somewhat more complex than the script in the first sample:

  • Line 1 begins with the standard header for a shell script file. Again, the header is required for a shell script to work as expected.

  • Line 3 uses xmessage and command substitution to display a dialog box with one button listing each .tex file in the current working directory. This is done with a call to the ls command using the -m option, which causes listed files to be separated by commas (see the ls manual page for details).

  • Line 4 saves the value of the special variable $?, which contains the exit status of xmessage, to the variable RESULT. If the first button is clicked, the value of RESULT is 101. If the second button is clicked, its value is 102.

  • Line 6 creates a variable that the script uses to count through the buttons one by one. Because clicking the first button results in the value 101, the counter begins at 100.

  • Lines 712 create a for..do loop; the commands between for and done are executed once for each .tex file in the current working directory; each time through, the variable TEXFILE contains the name of a .tex filename in the current directory.

  • Line 8 adds 1 to the value of INDEX for each time through the loop, meaning that for the first value of TEXFILE, the value of INDEX is 101; when the value of TEXFILE changes the second time through the loop, the value of INDEX changes once more to 102, and so on.

  • Lines 911 watch the value of INDEX. When it is the same as the value of RESULT, meaning that the value of TEXFILE is the same as the button the user clicked, the emacs editor is started using TEXFILE as an argument.

Now let's look at the script in action. After you save the file ~/bin/texedit, remember to use the chmod command to mark it as executable so that you can call it at the command line:

[[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.


Clicking one of the filename buttons causes the file in question to be loaded into a new emacs editor application window.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows