Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

Getting Graphical at the Command Line 475

Getting Graphical at

the Command Line

Linux works with several graphical toolkits, but three are worth special mention. Depending on which desktop environment you like to work in, you’ll find one or more of the toolkits installed.

Getting graphical in GNOME

The zenity command displays GTK dialogs (GTK, or the GIMP Toolkit, is part of GNOME) and can return information to the shell script.

If you work in a GNOME desktop environment, you’ll likely find the zenity toolkit installed on your system. If it’s not installed on your system, you can add it in a snap by installing the gnome-utils package from your distribution disc.

You can also find information about zenity through the Help Browser in GNOME utilities. Find the listing for zenity (see Figure 62-1).

Error, info, and warning dialogs

File selection dialogs

List boxes

Progress indicators

Question dialogs (prompting for user input)

zenity is fairly easy to use. For example, to display a calendar widget that looks like Figure 62-2, you would execute the following command:

$ choice=$(zenity --calendar)

Figure 62-1: The Zenity Manual.

zenity can display

Calendar widgets

Text entry widgets

Figure 62-2: The zenity calendar.

After the user selects a date, the $choice variable contains a value, such as 03/11/2004. (You can control the format of the date with a simple command line option.)

You can incorporate zenity into shell scripts to gather information from a user or to report progress. Listing 62-1 shows a simple shell script that uses zenity to burn a CD.

476 Technique 62: Getting Graphical with Shell Scripts

LISTING 62-1: BURNING A CD WITH ZENITY

1 #!/bin/bash

2

3 mkdir /tmp/$$ || (echo “can’t create working dir /tmp/$$” ; exit ) 4 echo “Created staging area in /tmp/$$”

5 echo “Select the files you want to burn, press Cancel when complete” 6

7 while $(true)

8 do

9 src=$(zenity --title “Select Files” --file-selection) 10

11if [ “x$src” = “x” ]

12then

13break;

14fi

15

16 echo “Selected: $src”

17

18 ln -s $src /tmp/$$/ 19

20 srcs=$srcs:$src

21

22 done

23

24 if [ “x$srcs” = “x” ]

25then

26echo “No files selected”

27exit

28fi

29

30 mkisofs -f -R -r -l -J -o /tmp/$$.iso /tmp/$$ || (echo “mkisofs failed” ; exit) 31 rm -rf /tmp/$$

32

33 MESSAGE=”$(du -sh /tmp/$$.iso|cut -f 1) selected Burn CD now?” 34

35 zenity --title “File selections complete” --question --text $MESSAGE || exit 36

37 echo “Burning CD”

38

39 cdrecord speed=8 dev=0,0,0 /tmp/$$.iso

40 rm /tmp/$$.iso

Here are a couple of highlights from Listing 62-1 that show how to use zenity in a shell script:

File selection dialog: The command at line 9 displays a file selection dialog (see Figure 62-3). zenity stores the user’s choices in the variable $src. If the user clicks Cancel instead of OK, $src is empty.

Question dialog: The command at line 35 displays a question dialog (see Figure 62-4). zenity waits for the user to click Cancel or OK. If the user clicks Cancel, the zenity command returns 0 (or false); otherwise, zenity returns 1 (or true).

Getting Graphical at the Command Line 477

Figure 62-3: The zenity file selection dialog.

Figure 62-4: The zenity question dialog.

You can find more information about zenity at the man page: $ man zenity.

See Technique 10 for more information about the rest of the script in Listing 62-1. It’s not a perfect example, but it gets the job done.

Getting graphical with KDE

If you prefer to work in the KDE desktop environment, you can install and use zenity, or use the KDEflavored graphical toolkit, kdialog.

If you’re already running KDE, kdialog is most likely installed on your system. If it’s not, install the

kdebase RPM package from your distribution discs to add kdialog to your installation. kdialog is included in the kdebase RPM package.

The kdialog command is similar to zenity, but it has even more options. kdialog offers the following forms:

Yes/no dialog

Yes/no/cancel dialog

Warning yes/no dialog

Warning continue/cancel dialog

Warning yes/no/cancel dialog

“Sorry” error message dialog

“Error” error message dialog

Message box dialog

Input box dialog (prompting user for information)

Password dialog

Text box dialog

Combo box (similar to a list box)

Menu

Checklist

Radio list

Get open filename (file selection box labeled Open)

Get save filename (file selection box labeled Save To)

Get directory name

Open a URL

Save to URL

478 Technique 62: Getting Graphical with Shell Scripts

Use kdialog in your scripts the same way that you would use zenity.

One drawback to choosing kdialog is the lack of documentation. You can find some help by opening a terminal window and entering the following command:

$ kdialog --help-all | more

The command line options for kdialog are displayed, along with a brief explanation. If you want more in-depth help, we can recommend visiting the KDE Developers Corner and checking out the tutorial at

developer.kde.org/documentation/tutorials/

kdialog/t1.html

Staying desktop neutral

One other optional toolkit worthy of mention is dialog. dialog is included with the Fedora and SuSE Linux distributions, and you can install it from the distribution media with the following command:

# rpm -Uhv dialog-version.i386.rpm

dialog offers the following forms for use in your shell scripts:

Calendar

Checklist

Form

File selector

Gauge

Information box

Input box

Input menu

Message box

Password input box

Radio list

Text box

Time box

Yes/No box

After it’s installed, dialog functions very much like either zenity or kdialog. The man page, found at

$ man dialog, has a good listing of the various options and forms that work with dialog.

Соседние файлы в предмете Операционные системы