36.7.2. Widgets. Comtrol DeviceMaster Software Development Kit
Oglas
Oglas
Advanced Bash-Scripting Guide read inner_variable <$TMPFILE # Read back shell variable.
rm -f "$TMPFILE" # Get rid of temp file.
echo "$inner_variable" # It's an ugly kludge, but it works.
•
The run-parts command is handy for running a set of command scripts in a particular sequence,
especially in combination with cron or at.
•
For doing multiple revisions on a complex script, use the rcs Revision Control System package.
Among other benefits of this is automatically updated ID header tags. The co command in rcs does a parameter replacement of certain reserved key words, for example, replacing
# $Id$
in a script with something like:
# $Id: hello-world.sh,v 1.1 2004/10/16 02:43:05 bozo Exp $
36.7.2. Widgets
It would be nice to be able to invoke X-Windows widgets from a shell script. There happen to exist several packages that purport to do so, namely Xscript, Xmenu, and widtools. The first two of these no longer seem to be maintained. Fortunately, it is still possible to obtain widtools here.
The widtools (widget tools) package requires the XForms library to be installed. Additionally, the
three of the six widgets offered do not work (and, in fact, segfault).
The dialog family of tools offers a method of calling "dialog" widgets from a shell script. The original dialog utility works in a text console, but its successors, gdialog, Xdialog, and kdialog use X-Windows-based widget sets.
Example 36-22. Widgets invoked from a shell script
#!/bin/bash
# dialog.sh: Using 'gdialog' widgets.
# Must have 'gdialog' installed on your system to run this script.
# Or, you can replace all instance of 'gdialog' below with 'kdialog' ...
# Version 1.1 (corrected 04/05/05)
# This script was inspired by the following article.
# "Scripting for X Productivity," by Marco Fioretti,
# LINUX JOURNAL, Issue 113, September 2003, pp. 86-9.
# Thank you, all you good people at LJ.
# Input error in dialog box.
E_INPUT=85
# Dimensions of display, input widgets.
HEIGHT=50
WIDTH=60
# Output file name (constructed out of script name).
OUTFILE=$0.output
Chapter 36. Miscellany 539
Advanced Bash-Scripting Guide
# Display this script in a text widget.
gdialog --title "Displaying: $0" --textbox $0 $HEIGHT $WIDTH
# Now, we'll try saving input in a file.
echo -n "VARIABLE=" > $OUTFILE gdialog --title "User Input" --inputbox "Enter variable, please:" \
$HEIGHT $WIDTH 2>> $OUTFILE if [ "$?" -eq 0 ]
# It's good practice to check exit status.
then
echo "Executed \"dialog box\" without errors." else
echo "Error(s) in \"dialog box\" execution."
# Or, clicked on "Cancel", instead of "OK" button.
rm $OUTFILE
exit $E_INPUT fi
# Now, we'll retrieve and display the saved variable.
. $OUTFILE # 'Source' the saved file.
echo "The variable input in the \"input box\" was: "$VARIABLE"" rm $OUTFILE # Clean up by removing the temp file.
# Some applications may need to retain this file.
exit $?
# Exercise: Rewrite this script using the 'zenity' widget set.
The xmessage command is a simple method of popping up a message/query window. For example:
xmessage Fatal error in script! -button exit
and it works very nicely within a script.
get_info ()
{
zenity --entry # Pops up query window . . .
#+ and prints user entry to stdout.
# Also try the --calendar and --scale options.
} answer=$( get_info ) # Capture stdout in $answer variable.
echo "User entered: "$answer""
For other methods of scripting with widgets, try Tk or wish (Tcl derivatives), PerlTk (Perl with Tk extensions), tksh (ksh with Tk extensions), XForms4Perl (Perl with XForms extensions), Gtk-Perl (Perl with
Gtk extensions), or PyQt (Python with Qt extensions).
Chapter 36. Miscellany 540
Prenesi
Oglas