Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Cooper M.Advanced bash-scripting guide.2002.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
916.67 Кб
Скачать
# Script recursively spawns a new instance of itself.
# Each child script does the same, until #+ a generated $i equals $MAXVAL.

Advanced Bash−Scripting Guide

exit 0

Using the double parentheses construct, it is possible to use C−like syntax for setting and incrementing variables and in for and while loops. See Example 10−11 and Example 10−16.

The run−parts command is handy for running a set of command scripts in sequence, particularly in combination with cron or at.

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 Makefile needs some judicious editing before the package will build on a typical Linux system. Finally, three of the six widgets offered do not work (and, in fact, segfault).

For more effective 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).

34.6. Oddities

Can a script recursively call itself? Indeed.

Example 34−6. A script that recursively calls itself

#!/bin/bash

#recurse.sh

#Can a script recursively call itself?

#Yes, but this is of little or no practical use #+ except perhaps as a "proof of concept".

RANGE=10

MAXVAL=9

i=$RANDOM

let "i %= $RANGE" # Generate a random number between 0 and $MAXVAL.

if [ "$i" −lt "$MAXVAL" ] then

echo "i = $i"

./$0

fi

#Using a "while" loop instead of an "if/then" test causes problems.

#Exercise for the reader: Explain why.

exit 0

34.6. Oddities

307

Advanced Bash−Scripting Guide

Too many levels of recursion can exhaust the script's stack space, causing a segfault.

34.7. Portability Issues

This book deals specifically with Bash scripting on a GNU/Linux system. All the same, users of sh and ksh will find much of value here.

As it happens, many of the various shells and scripting languages seem to be converging toward the POSIX 1003.2 standard. Invoking Bash with the −−posix option or inserting a set −o posix at the head of a script causes Bash to conform very closely to this standard. Even lacking this measure, most Bash scripts will run as−is under ksh, and vice−versa, since Chet Ramey has been busily porting ksh features to the latest versions of Bash.

On a commercial UNIX machine, scripts using GNU−specific features of standard commands may not work. This has become less of a problem in the last few years, as the GNU utilities have pretty much displaced their proprietary counterparts even on "big−iron" UNIX. Caldera's recent release of the source to many of the original UNIX utilities will only accelerate the trend.

34.8. Shell Scripting Under Windows

Even users running that other OS can run UNIX−like shell scripts, and therefore benefit from many of the lessons of this book. The Cygwin package from Cygnus and the MKS utilities from Mortice Kern Associates add shell scripting capabilities to Windows.

34.7. Portability Issues

308