27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

# agram.sh: Playing games with anagrams.<br />

# Find anagrams of...<br />

LETTERSET=etaoinshrdlu<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

anagram "$LETTERSET" | # Find all anagrams of the letterset...<br />

grep '.......' | # With at least 7 letters,<br />

grep '^is' |<br />

# starting with 'is'<br />

grep −v 's$' |<br />

# no plurals<br />

grep −v 'ed$'<br />

# no past tense verbs<br />

# Uses "anagram" utility<br />

#+ that is part of the author's "yawl" word list package.<br />

# http://ibiblio.org/pub/Linux/libs/yawl−0.2.tar.gz<br />

exit 0<br />

# End of code.<br />

bash$ sh agram.sh<br />

islander<br />

isolate<br />

isolead<br />

isotheral<br />

See also Example 28−2, Example 12−18, and Example A−10.<br />

• Use "anonymous here documents" to comment out blocks of code, to save having to individually<br />

comment out each line with a #. See Example 17−10.<br />

• Running a script on a machine that relies on a command that might not be installed is dangerous. Use<br />

whatis to avoid potential problems with this.<br />

CMD=command1<br />

PlanB=command2<br />

# First choice.<br />

# Fallback option.<br />

command_test=$(whatis "$CMD" | grep 'nothing appropriate')<br />

# If 'command1' not found on system , 'whatis' will return<br />

#+ "command1: nothing appropriate."<br />

if [[ −z "$command_test" ]] # Check whether command present.<br />

then<br />

$CMD option1 option2 # Run command1 with options.<br />

else<br />

# Otherwise,<br />

$PlanB<br />

#+ run command2.<br />

fi<br />

• The run−parts command is handy for running a set of command scripts in sequence, particularly in<br />

combination with cron or at.<br />

• It would be nice to be able to invoke X−Windows widgets from a shell script. There happen to exist<br />

several packages that purport to do so, namely Xscript, Xmenu, and widtools. The first two of these no<br />

longer seem to be maintained. Fortunately, it is still possible to obtain widtools here.<br />

The widtools (widget tools) package requires the XForms library to be installed.<br />

Additionally, the Makefile needs some judicious editing before the package will build<br />

on a typical Linux system. Finally, three of the six widgets offered do not work (and,<br />

in fact, segfault).<br />

For more effective scripting with widgets, try Tk or wish (Tcl derivatives), PerlTk (Perl with Tk<br />

extensions), tksh (ksh with Tk extensions), XForms4Perl (Perl with XForms extensions), Gtk−Perl<br />

(Perl with Gtk extensions), or PyQt (Python with Qt extensions).<br />

Chapter 34. Miscellany 353

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!