removed blank tracer images, added info on future debugging features, more keywords, minor corrections.

This commit is contained in:
tille 2005-03-01 19:39:20 +00:00
parent 659411da71
commit f44fc012e4
13 changed files with 24 additions and 546 deletions

View File

@ -38,7 +38,7 @@
</author>
</authorgroup>
<edition>Version 1.5 Last updated 20041206</edition>
<edition>Version 1.6 Last updated 20050301</edition>
<keywordset>
<keyword>Linux</keyword>
<keyword>Scripts</keyword>
@ -51,6 +51,12 @@
<keyword>bash</keyword>
<keyword>scripting</keyword>
<keyword>Scripting</keyword>
<keyword>awk</keyword>
<keyword>sed</keyword>
<keyword>variables</keyword>
<keyword>functions</keyword>
<keyword>loops</keyword>
<keyword>conditionals</keyword>
</keywordset>
</bookinfo>
@ -65,13 +71,6 @@
<para>This is a practical guide which, while not always being too serious, tries to give real-life instead of theoretical examples. I partly wrote it because I don't get excited with stripped down and over-simplified examples written by people who know what they are talking about, showing some really cool Bash feature so much out of its context that you cannot ever use it in practical circumstances. You can read that sort of stuff after finishing this book, which contains exercises and examples that will help you survive in the real world.</para>
<para>From my experience as UNIX/Linux user, system administrator and trainer, I know that people can have years of daily interaction with their systems, without having the slightest knowledge of task automation. Thus they often think that UNIX is not userfriendly, and even worse, they get the impression that it is slow and old-fashioned. This problem is another one that can be remedied by this guide.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_02">
@ -92,15 +91,7 @@
</itemizedlist>
<para>See <ulink url="http://tldp.org/LDP/intro-linux/html/">Introduction to Linux</ulink> (or your local <ulink url="http://www.tldp.org/mirrors.html">TLDP mirror</ulink>) if you haven't mastered one or more of these topics. Additional information can be found in your system documentation (man and info pages), or at <ulink url="http://tldp.org">the Linux Documentation Project</ulink>.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_03">
<title>New versions and availability</title>
@ -122,19 +113,17 @@
</mediaobject>
</figure>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_04">
<title>Revision History</title>
<para>
<revhistory>
<revision>
<revnumber>1.6</revnumber>
<date>2005-03-01</date>
<authorinitials>MG</authorinitials>
<revremark>Minor debugging, added more keywords, info about new Bash 3.0, took out blank image.</revremark>
</revision>
<revision>
<revnumber>1.5</revnumber>
<date>2004-12-06</date>
@ -182,14 +171,6 @@
</revhistory>
</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_05">
@ -208,15 +189,7 @@ Thanks to all the friends who helped (or tried to) and to my husband; your encou
</itemizedlist>
<para>Special thanks to Tabatha Marshall, who volunteered to do a complete review and spell and grammar check. We make a great team: she works when I sleep. And vice versa ;-)</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_06">
<title>Feedback</title>
@ -226,18 +199,10 @@ Missing information, missing links, missing characters, remarks? Mail it to
the maintainer of this document.
</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_07">
<title>Copyright information</title>
<para>Copyright &copy; 2003 Machtelt Garrels.</para>
<para>Copyright &#169; 2003-2005 Machtelt Garrels.</para>
<para> Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being "New versions of this document", "Contributions", "Feedback" and "Copyright information", with no Front-Cover Texts and no Back-Cover Texts. A copy of the license is included in <xref linkend="app_01" /> entitled "GNU Free Documentation License".</para>
<para>The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is offered without warranty, either express or implied. Neither the author nor the publisher nor any dealer or distributor will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.</para>
@ -245,27 +210,11 @@ the maintainer of this document.
<para>The logos, trademarks and symbols used in this book are the properties of
their respective owners.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_08">
<title>What do you need?</title>
<para><command>bash</command>, available from <ulink url="http://www.gnu.org/directory/GNU/">http://www.gnu.org/directory/GNU/</ulink>. The Bash shell is available on nearly every Linux system, and can these days be found on a wide variety of UNIX systems.</para>
<para>Compiles easily if you need to make your own, tested on a wide variety of UNIX, Linux, MS Windows and other systems.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_09">
<title>Conventions used in this document</title>
@ -356,15 +305,7 @@ within this guide.</entry>
</tgroup>
</table>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
</section>
<section id="intro_10">
<title>Organization of this document</title>
<para>This guide discusses concepts useful in the daily life of the serious Bash user. While a basic knowledge of the usage of the shell is required, we start with a discussion of the basic shell components and practices in the first three chapters.</para>
@ -411,15 +352,7 @@ next chapter.</para>
<para><xref linkend="chap_12" />: Catching signals: introduction to process signalling, trapping user-sent signals.</para>
</listitem>
</itemizedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
</section>

View File

@ -52,13 +52,6 @@ in the active terminal. The system finds the directory where the name occurs us
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_02"><title>Advantages of the Bourne Again SHell</title>
@ -252,13 +245,6 @@ the shell expansions or by the <command>let</command> built-in.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_03"><title>Executing commands</title>
@ -294,13 +280,6 @@ the shell expansions or by the <command>let</command> built-in.</para>
<para>While the subshell processes each line of the script, the parent shell waits for its child process to finish. When there are no more lines in the shell script to read, the subshell terminates. The parent shell awakes and displays a new prompt.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_04"><title>Building blocks</title>
<sect2 id="sect_01_04_01"><title>Shell building blocks</title>
@ -371,13 +350,6 @@ the shell expansions or by the <command>let</command> built-in.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_05"><title>Developing good scripts</title>
@ -537,13 +509,6 @@ exit 0
<para>The <command>case</command> statement often used in this kind of script is described in <xref linkend="sect_07_02_05" />.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_06"><title>Summary</title>
@ -557,13 +522,6 @@ exit 0
<para>Shell commands can be split up in three groups: the shell functions, shell built-ins and existing commands in a directory on your system. Bash supports additional built-ins not found in the plain Bourne shell.</para>
<para>Shell scripts consist of these commands arranged as shell syntax dictates. Scripts are read and executed line per line and should have a logical structure.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_07"><title>Exercises</title>
@ -592,13 +550,6 @@ exit 0
<listitem><para>How to display the system hostname? Only the name, nothing more!</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>

View File

@ -87,13 +87,6 @@ bash: TUX: readonly variable
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_10_02"><title>Array variables</title>
<sect2 id="sect_10_02_01"><title>Creating arrays</title>
@ -156,6 +149,9 @@ one three four
# $Id$
# $Log$
# Revision 1.5 2004/12/06 12:27:09 tille
# changes for new domainname, minor corrections
#
# Revision 1.6 2004/10/18 18:58:06 tille
# debugging, typos removed, replaced screenshots in chap9 with screen sections.
#
@ -273,13 +269,6 @@ EOF
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_10_03"><title>Operations on variables</title>
<sect2 id="sect_10_03_01"><title>Arithmetic on variables</title>
@ -422,26 +411,12 @@ thisisaverylongstring
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_10_04"><title>Summary</title>
<para>Normally, a variable can hold any type of data, unless variables are declared explicitly. Constant variables are set using the <command>readonly</command> built-in command.</para>
<para>An array holds a set of variables. If a type of data is declared, then all elements in the array will be set to hold only this type of data.</para>
<para>Bash features allow for substitution and transformation of variables <quote>on the fly</quote>. Standard operations include calculating the length of a variable, arithmetic on variables, substituting variable content and substituting part of the content.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_10_05"><title>Exercises</title>
<para>Here are some brain crackers:</para>
@ -463,12 +438,5 @@ thisisaverylongstring
<listitem><para>Write a script that implements a simple web browser (in text mode), using <command>wget</command> and <command>links <option>-dump</option></command> to display HTML pages to the user. The user has 3 choices: enter a URL, enter <keycap>b</keycap> for back and <keycap>q</keycap> to quit. The last 10 URLs entered by the user are stored in an array, from which the user can restore the URL by using the <emphasis>back</emphasis> functionality.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -101,13 +101,6 @@ dir ()
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_11_02"><title>Examples of functions in scripts</title>
<sect2 id="sect_11_02_01"><title>Recycling</title>
@ -226,25 +219,11 @@ echo -e "Remote backup `date` SUCCESS\n----------" &gt;&gt; "$LOGFILE"
<cmdsynopsis><command>command &gt; <filename>file</filename> 2&gt;&amp;1</command></cmdsynopsis>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_11_03"><title>Summary</title>
<para>Functions provide an easy way of grouping commands that you need to execute repetitively. When a function is running, the positional parameters are changed to those of the function. When it stops, they are reset to those of the calling program. Functions are like mini-scripts, and just like a script, they generate exit or return codes.</para>
<para>While this was a short chapter, it contains important knowledge needed for achieving the ultimate state of laziness that is the typical goal of any system administrator.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_11_04"><title>Exercises</title>
@ -256,13 +235,6 @@ echo -e "Remote backup `date` SUCCESS\n----------" &gt;&gt; "$LOGFILE"
<listitem><para>Create a subdirectory in your home directory in which you can store function definitions. Put a couple of functions in that directory. Useful functions might be, amongs others, that you have the same commands as on DOS or a commercial UNIX when working with Linux, or vice versa. These functions should then be imported in your shell environment when <filename>~/.bashrc</filename> is read.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -85,13 +85,6 @@ maud 5614 2214 0 20:15 pts/5 00:00:00 grep stuck_process
<para>When a process starts up several instances, <command>killall</command> might be easier. It takes the same option as the <command>kill</command> command, but applies on all instances of a given process. Test this command before using it in a production environment, since it might not work as expected on some of the commercial Unices.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_12_02"><title>Traps</title>
<sect2 id="sect_12_02_01"><title>General</title>
@ -151,25 +144,11 @@ exit 0
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_12_03"><title>Summary</title>
<para>Signals can be sent to your programs using the <command>kill</command> command or keyboard shortcuts. These signals can be caught, upon which action can be performed, using the <command>trap</command> statement.</para>
<para>Some programs ignore signals. The only signal that no program can ignore is the <emphasis>KILL</emphasis> signal.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_12_04"><title>Exercises</title>
<para>A couple of practical examples:</para>
@ -178,12 +157,5 @@ exit 0
<listitem><para>Write a script that automates the installation of a third-party package of your choice. The package must be downloaded from the Internet. It must be decompressed, unarchived and compiled if these actions are appropriate. Only the actual installation of the package should be uninterruptable.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -107,13 +107,6 @@ I'm giving you back your prompt now.
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_02_02"><title>Script basics</title>
<sect2 id="sect_02_02_01"><title>Which shell will run the script?</title>
@ -158,13 +151,6 @@ echo
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_02_03"><title>Debugging Bash scripts</title>
@ -208,6 +194,10 @@ And this is a number:
I'm giving you back your prompt now.
+ echo
</screen>
<note><title>Future Bash Features</title>
<para>There is now a full-fledged debugger for Bash, available at <ulink url="http://bashdb.sourceforge.net">SourceForge</ulink>. Right now however, you need a patched version of bash-2.05. The new debugging features might become available in bash-3.0.</para>
</note>
</sect2>
<sect2 id="sect_02_03_02"><title>Debugging on part(s) of the script</title>
<para>Using the <command>set</command> Bash built-in you can run in normal mode those portions of the script of which you are sure they are without fault, and display debugging information only for troublesome zones. Say we are not sure what the <command>w</command> command will do in the example <filename>commented-script1.sh</filename>, then we could enclose it in the script like this:</para>
@ -299,26 +289,12 @@ echo "Variable VARNAME is now set to $VARNAME."
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_02_05"><title>Summary</title>
<para>A shell script is a reusable series of commands put in an executable text file. Any text editor can be used to write scripts.</para>
<para>Scripts start with <emphasis>#!</emphasis> followed by the path to the shell executing the commands from the script. Comments are added to a script for your own future reference, and also to make it understandable for other users. It is better to have too many explanations than not enough.</para>
<para>Debugging a script can be done using shell options. Shell options can be used for partial debugging or for analyzing the entire script. Inserting <command>echo</command> commands at strategic locations is also a common troubleshooting technique.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_02_06"><title>Exercises</title>
<para>This exercise will help you to create your first script.</para>
@ -331,12 +307,5 @@ echo "Variable VARNAME is now set to $VARNAME."
<listitem><para>Make errors in your script: see what happens if you misspell commands, if you leave out the first line or put something unintelligible there, or if you misspell shell variable names or write them in lower case characters after they have been declared in capitals. Check what the debug comments say about this.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -242,13 +242,6 @@ echo "PS1 is now set to $PS1"
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_02"><title>Variables</title>
@ -721,13 +714,6 @@ echo backup succeeded &gt; $LOGFILE
</note>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_03"><title>Quoting characters</title>
@ -786,13 +772,6 @@ I'd say: "Go for it!"
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_04"><title>Shell expansion</title>
@ -935,13 +914,6 @@ leading <quote>0x</quote> or <quote>0X</quote> denotes hexadecimal. Otherwise,
<para>The <varname>GLOBIGNORE</varname> shell variable may be used to restrict the set of file names matching a pattern. If <varname>GLOBIGNORE</varname> is set, each matching file name that also matches one of the patterns in <varname>GLOBIGNORE</varname> is removed from the list of matches. The file names <filename>.</filename> and <filename>..</filename> are always ignored, even when <varname>GLOBIGNORE</varname> is set. However, setting <varname>GLOBIGNORE</varname> has the effect of enabling the <option>dotglob</option> shell option, so all other file names beginning with a <quote>.</quote> will match. To get the old behavior of ignoring file names beginning with a <quote>.</quote>, make <quote>.*</quote> one of the patterns in <varname>GLOBIGNORE</varname>. The <option>dotglob</option> option is disabled when <varname>GLOBIGNORE</varname> is unset.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_05"><title>Aliases</title>
@ -1020,13 +992,6 @@ bash: dh: command not found
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_06"><title>More Bash options</title>
<sect2 id="sect_03_06_01"><title>Displaying options</title>
@ -1104,13 +1069,6 @@ bash: VAR: unbound variable
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
@ -1121,13 +1079,6 @@ bash: VAR: unbound variable
<para>Because a lot of characters have double or even triple meanings, depending on the environment, Bash uses a system of quoting to take away special meaning from one or multiple characters when special treatment is not wanted.</para>
<para>Bash uses various methods of expanding command line entries in order to determine which commands to execute.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_08"><title>Exercises</title>
<para>For this exercise, you will need to read the <command>useradd</command> man pages, because we are going to use the <filename>/etc/skel</filename> directory to hold default shell configuration files, which are copied to the home directory of each newly added user.</para>
@ -1146,12 +1097,5 @@ bash: VAR: unbound variable
</orderedlist>
<para>Don't forget to <command>chmod</command> your scripts!</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -52,13 +52,6 @@
<para>Check in your system documentation whether commands using regular expressions support extended expressions.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_04_02"><title>Examples using grep</title>
@ -186,13 +179,6 @@ for i in /etc/profile.d/*.sh ; do
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_04_03"><title>Pattern matching using Bash features</title>
<sect2 id="sect_04_03_01"><title>Character ranges</title>
@ -233,26 +219,12 @@ drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_04_04"><title>Summary</title>
<para>Regular expressions are powerful tools for selecting particular lines from files or output. A lot of UNIX commands use regular expressions: <command>vim</command>, <command>perl</command>, the <application>PostgreSQL</application> database and so on. They can be made available in any language or application using external libraries, and they even found their way to non-UNIX systems. For instance, regular expressions are used in the Excell spreadsheet that comes with the MicroSoft Windows Office suite. In this chapter we got the feel of the <command>grep</command> command, which is indispensable in any UNIX environment.</para>
<note><para>The <command>grep</command> command can do much more than the few tasks we discussed here; we only used it as an example for regular expressions. The GNU <command>grep</command> version comes with plenty of documentation, which you are strongly advised to read!</para></note>
<para>Bash has built-in features for matching patterns and can recognize character classes and ranges.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_04_05"><title>Exercises</title>
<para>These exercises will help you master regular expressions.</para>
@ -272,13 +244,6 @@ drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/
<listitem><para>Display configuration files in <filename>/etc</filename> that contain numbers in their names.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -63,13 +63,6 @@
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_05_02"><title>Interactive editing</title>
<sect2 id="sect_05_02_01"><title>Printing lines containing a pattern</title>
@ -230,13 +223,6 @@ This is the final line.
</tip>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_05_03"><title>Non-interactive editing</title>
<sect2 id="sect_05_03_01"><title>Reading sed commands from a file</title>
@ -312,25 +298,11 @@ line3
<para>Advanced editors, supporting syntax highlighting, can recognize <command>sed</command> syntax. This can be a great help if you tend to forget backslashes and such.</para></note>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_05_04"><title>Summary</title>
<para>The <command>sed</command> stream editor is a powerful command line tool, which can handle streams of data: it can take input lines from a pipe. This makes it fit for non-interactive use. The <command>sed</command> editor uses <command>vi</command>-like commands and accepts regular expressions.</para>
<para>The <command>sed</command> tool can read commands from the command line or from a script. It is often used to perform find-and-replace actions on lines containing a pattern.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_05_05"><title>Exercises</title>
<para>These exercises are meant to further demonstrate what <command>sed</command> can do.</para>
@ -345,12 +317,5 @@ line3
<listitem><para>Create a script that shows lines containing trailing white spaces from a file. This script should use a <command>sed</command> script and show sensible information to the user.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -30,13 +30,6 @@ contain function definitions, loops, conditions and other programming constructs
<cmdsynopsis><command>awk <option>-f</option> <filename>PROGRAM-FILE</filename> <filename>inputfile(s)</filename></command></cmdsynopsis>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_02"><title>The print program</title>
@ -195,13 +188,6 @@ Partition /usr : 97% full!
<para>Awk is a programming language. Its syntax is recognized by most editors that can do syntax highlighting for other languages, such as C, Bash, HTML, etc.</para></note>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_03"><title>Gawk variables</title>
<para>As <command>awk</command> is processing the input file, it uses several variables. Some are editable, some are read-only.</para>
@ -348,26 +334,12 @@ END { print "&lt;/pre&gt;\n&lt;/body&gt;\n&lt;/html&gt;" }
<para>The syntax is the same as for the C-language <command>printf</command> statement; see your C introduction guide. The <command>gawk</command> info pages contain full explanations.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_04"><title>Summary</title>
<para>The <command>gawk</command> utility interprets a special-purpose programming language, handling simple data-reformatting jobs with just a few lines of code. It is the free version of the general UNIX <command>awk</command> command.</para>
<para>This tools reads lines of input data and can easily recognize columned output. The <command>print</command> program is the most common for filtering and formatting defined fields.</para>
<para>On-the-fly variable declaration is straightforward and allows for simple calculation of sums, statistics and other operations on the processed input stream. Variables and commands can be put in <command>awk</command> scripts for background processing.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_05"><title>Exercises</title>
@ -431,12 +403,5 @@ looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line, but
</listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -227,13 +227,6 @@ Pleasure to meet you, Madame.
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_07_02"><title>More advanced if usage</title>
<sect2 id="sect_07_02_01"><title>if/then/else constructs</title>
@ -371,7 +364,7 @@ fi
<cmdsynopsis><command>CONSEQUENT-COMMANDS;</command></cmdsynopsis>
<cmdsynopsis><command>elif MORE-TEST-COMMANDS; then</command></cmdsynopsis>
<cmdsynopsis><command>MORE-CONSEQUENT-COMMANDS;</command></cmdsynopsis>
<cmdsynopsis><command>else ALTERNATE-CONSEQUENT-COMMANDS; then</command></cmdsynopsis>
<cmdsynopsis><command>else ALTERNATE-CONSEQUENT-COMMANDS;</command></cmdsynopsis>
<cmdsynopsis><command>fi</command></cmdsynopsis>
<para>The <command>TEST-COMMANDS</command> list is executed, and if its return status is zero, the <command>CONSEQUENT-COMMANDS</command> list is executed. If
<command>TEST-COMMANDS</command> returns a non-zero status, each <command>elif</command> list is executed in turn, and if its exit status is zero, the corresponding <command>MORE-CONSEQUENT-COMMANDS</command> is executed and the command completes. If <command>else</command> is followed by an <command>ALTERNATE-CONSEQUENT-COMMANDS</command> list, and the final command in the final <command>if</command> or <command>elif</command> clause has a non-zero exit status, then <command>ALTERNATE-CONSEQUENT-COMMANDS</command> is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.</para>
@ -515,13 +508,6 @@ Guard: You'd better give'm a fish, less they get violent...
<para>As you can see, exit status codes can be chosen freely. Existing commands usually have a series of defined codes; see the programmer's manual for each command for more information.</para></sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_07_03"><title>Using case statements</title>
<sect2 id="sect_07_03_01"><title>Simplified conditions</title>
@ -618,26 +604,12 @@ esac
<para>The tasks to execute in each case, such as stopping and starting the daemon, are defined in functions, which are partially sourced from the <filename>/etc/rc.d/init.d/functions</filename> file. See <xref linkend="chap_11" /> for more explanation.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_07_04"><title>Summary</title>
<para>In this chapter we learned how to build conditions into our scripts so that different actions can be undertaken upon success or failure of a command. The actions can be determined using the <command>if</command> statement. This allows you to perform arithmetic and string comparisons, and testing of exit code, input and files needed by the script.</para>
<para>A simple <command>if/then/fi</command> test often preceeds commands in a shell script in order to prevent output generation, so that the script can easily be run in the background or through the <application>cron</application> facility. More complex definitions of conditions are usually put in a <command>case</command> statement.</para>
<para>Upon successful condition testing, the script can explicitly inform the parent using the <command>exit 0</command> status. Upon failure, any other number may be returned. Based on the return code, the parent program can take appropriate action.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_07_05"><title>Exercises</title>
<para>Here are some ideas to get you started using <command>if</command> in scripts:</para>
@ -654,12 +626,5 @@ esac
<para>The script should clean up the compressed archive before it exits.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -153,13 +153,6 @@ Usage of the feed script: ./feed.sh food-on-menu animal-name
<para>For more information about the <command>printf</command> command and the way it allows you to format output, see the Bash info pages.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_08_02"><title>Catching user input</title>
<sect2 id="sect_08_02_01"><title>Using the read built-in command</title>
@ -578,25 +571,11 @@ Transaction(s) Complete
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_08_03"><title>Summary</title>
<para>In this chapter, we learned how to provide user comments and how to prompt for user input. This is usually done using the <command>echo</command>/<command>read</command> combination. We also discussed how files can be used as input and output using file descriptors and redirection, and how this can be combined with getting input from the user.</para>
<para>We stressed the importance of providing ample message for the users of our scripts. As always when others use your scripts, it is better to give too much information than not enough. <emphasis>Here</emphasis> documents is a type of shell construct that allows creation of lists, holding choices for the users. This construct can also be used to execute otherwise interactive tasks in the background, without intervention.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_08_04"><title>Exercises</title>
<para>These exercises are practical applications of the constructs discussed in this chapter. When writing the scripts, you may test by using a test directory that does not contain too much data. Write each step, then test that portion of code, rather than writing everything at once.</para>
@ -629,12 +608,5 @@ Transaction(s) Complete
<listitem><para>Rewrite the script from <xref linkend="sect_07_02_01_04" /> so that it reads input from the user instead of taking it from the first argument.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -56,13 +56,6 @@ done
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_02"><title>The while loop</title>
<sect2 id="sect_09_02_01"><title>What is it?</title>
@ -206,13 +199,6 @@ echo "Exiting."
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_03"><title>The until loop</title>
<sect2 id="sect_09_03_01"><title>What is it?</title>
@ -261,13 +247,6 @@ done
<para>Note the initialization of the <varname>HOUR</varname> and <varname>DISKFULL</varname> variables and the use of options with <command>ls</command> and <command>date</command> in order to obtain a correct listing for <varname>TOREMOVE</varname>.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_04"><title>I/0 redirection and loops</title>
<sect2 id="sect_09_04_01"><title>Input redirection</title>
@ -301,13 +280,6 @@ done
<para>Files are compressed before they are moved into the archive directory.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_05"><title>Break and continue</title>
<sect2 id="sect_09_05_01"><title>The break built-in</title>
@ -448,13 +420,6 @@ new name for TEST is test
<para>The <command>tr</command> is part of the <emphasis>textutils</emphasis> package; it can perform all kinds of character transformations.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_06"><title>Making menus with the select built-in</title>
<sect2 id="sect_09_06_01"><title>General</title>
@ -524,13 +489,6 @@ rm "$QUIT"
<para>By default, the <varname>PS3</varname> variable is not changed when entering a nested <command>select</command> loop. If you want a different prompt in the submenu, be sure to set it at the appropriate time(s).</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_07"><title>The shift built-in</title>
<sect2 id="sect_09_07_01"><title>What does it do?</title>
@ -586,13 +544,6 @@ done
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_08"><title>Summary</title>
<para>In this chapter, we discussed how repetitive commands can be incorporated in loop constructs. Most common loops are built using the <command>for</command>, <command>while</command> or <command>until</command> statements, or a combination of these commands. The <command>for</command> loop executes a task a defined number of times. If you don't know how many times a command should execute, use either <command>until</command> or <command>while</command> to specify when the loop should end.</para>
@ -600,13 +551,6 @@ done
<para>A file can be used as input for a loop using the input redirection operator, loops can also read output from commands that is fed into the loop using a pipe.</para>
<para>The <command>select</command> construct is used for printing menus in interactive scripts. Looping through the command line arguments to a script can be done using the <command>shift</command> statement.</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_09_09"><title>Exercises</title>
<para>Remember: when building scripts, work in steps and test each step before incorporating it in your script.</para>
@ -628,12 +572,5 @@ done
</listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>