LDP/LDP/guide/docbook/Bash-Beginners-Guide/abook.xml

407 lines
18 KiB
XML

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
[
<!ENTITY chap1 SYSTEM "chap1.xml">
<!ENTITY chap2 SYSTEM "chap2.xml">
<!ENTITY chap3 SYSTEM "chap3.xml">
<!ENTITY chap4 SYSTEM "chap4.xml">
<!ENTITY chap5 SYSTEM "chap5.xml">
<!ENTITY chap6 SYSTEM "chap6.xml">
<!ENTITY chap7 SYSTEM "chap7.xml">
<!ENTITY chap8 SYSTEM "chap8.xml">
<!ENTITY chap9 SYSTEM "chap9.xml">
<!ENTITY chap10 SYSTEM "chap10.xml">
<!ENTITY chap11 SYSTEM "chap11.xml">
<!ENTITY chap12 SYSTEM "chap12.xml">
<!ENTITY app1 SYSTEM "app1.xml">
<!ENTITY gloss SYSTEM "gloss.xml">
<!ENTITY index-gloss SYSTEM "index-gloss.xml">
]>
<book id="Bash-Beginners-Guide">
<bookinfo>
<title>Bash Guide for Beginners</title>
<authorgroup>
<author>
<firstname>Machtelt</firstname>
<surname>Garrels</surname>
<affiliation>
<orgname>Garrels BVBA
</orgname>
<address>
<email>tille wants no spam _at_ garrels dot be</email>
</address>
</affiliation>
</author>
</authorgroup>
<edition>Version 1.11 Last updated 20081227</edition>
<keywordset>
<keyword>Linux</keyword>
<keyword>Scripts</keyword>
<keyword>linux</keyword>
<keyword>Bash</keyword>
<keyword>guide</keyword>
<keyword>Guide</keyword>
<keyword>Exercises</keyword>
<keyword>exercises</keyword>
<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>
<toc></toc>
<preface>
<title>Introduction</title>
<section id="intro_01">
<title>Why this guide?</title>
<para>The primary reason for writing this document is that a lot of readers feel the existing <ulink url="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html">HOWTO</ulink> to be too short and incomplete, while the <ulink url="http://tldp.org/LDP/abs/html/">Bash Scripting</ulink> guide is too much of a reference work. There is nothing in between these two extremes. I also wrote this guide on the general principle that not enough free basic courses are available, though they should be.</para>
<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 user-friendly, 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>
</section>
<section id="intro_02">
<title>Who should read this book?</title>
<para>Everybody working on a UNIX or UNIX-like system who wants to make life easier on themselves, power users and sysadmins alike, can benefit from reading this book. Readers who already have a grasp of working the system using the command line will learn the ins and outs of shell scripting that ease execution of daily tasks. System administration relies a great deal on shell scripting; common tasks are often automated using simple scripts. This document is full of examples that will encourage you to write your own and that will inspire you to improve on existing scripts.</para>
<para>Prerequisites/not in this course:</para>
<itemizedlist>
<listitem><para>You should be an experienced UNIX or Linux user, familiar with basic commands, man pages and documentation</para></listitem>
<listitem><para>Being able to use a text editor</para></listitem>
<listitem><para>Understand system boot and shutdown processes, init and initscripts</para></listitem>
<listitem><para>Create users and groups, set passwords</para></listitem>
<listitem><para>Permissions, special modes</para></listitem>
<listitem><para>Understand naming conventions for devices, partitioning, mounting/unmounting file systems</para></listitem>
<listitem><para>Adding/removing software on your system</para></listitem>
</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>
</section>
<section id="intro_03">
<title>New versions, translations and availability</title>
<para>The most recent edition can be found at <ulink url="http://tille.garrels.be/training/bash/" />. You should find the same version at <ulink url="http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html" />.</para>
<para>This guide is available in print from <ulink url="http://store.fultus.com/product_info.php?products_id=66">Fultus.com</ulink>.</para>
<figure><title>Bash Guide for Beginners front cover</title>
<mediaobject>
<imageobject>
<imagedata fileref="images/bgb.jpg" format="JPG"></imagedata>
</imageobject>
<imageobject>
<imagedata fileref="images/bgb.eps" format="EPS"></imagedata>
</imageobject>
<textobject>
<phrase>Front cover of the Bash guide, red highlighted code on blue background.</phrase>
</textobject>
</mediaobject>
</figure>
<para>This guide has been translated:</para>
<itemizedlist>
<listitem><para>Chinese translation at <ulink url="http://xiaowang.net/bgb-cn/" />, by Wang Wei.</para></listitem>
<listitem><para>Ukrainian translation at <ulink url="http://docs.linux.org.ua/index.php/LDP:Bash_beginners_guide" />, by Yaroslav Fedevych and his team.</para></listitem>
</itemizedlist>
<para>A french translation is in the making and will be linked to as soon as it is finished.</para>
</section>
<section id="intro_04">
<title>Revision History</title>
<para>
<revhistory id="revhistory">
<revision>
<revnumber>1.11</revnumber>
<date>2008-12-27</date>
<authorinitials>MG</authorinitials>
<revremark>Processed input from readers.</revremark>
</revision>
<revision>
<revnumber>1.10</revnumber>
<date>2008-06-06</date>
<authorinitials>MG</authorinitials>
<revremark>address change</revremark>
</revision>
<revision>
<revnumber>1.9</revnumber>
<date>2006-10-10</date>
<authorinitials>MG</authorinitials>
<revremark>Incorporated reader remarks, added index using DocBook tags.</revremark>
</revision>
<revision>
<revnumber>1.8</revnumber>
<date>2006-03-15</date>
<authorinitials>MG</authorinitials>
<revremark>clarified example in Chap4, corrected here doc in chap9, general checks and correction of typos, added link to Chinese and Ukrainian translation, note and stuff to know about awk in chap6.</revremark>
</revision>
<revision>
<revnumber>1.7</revnumber>
<date>2005-09-05</date>
<authorinitials>MG</authorinitials>
<revremark>Corrected typos in chapter 3, 6 and 7, incorporated user remarks, added a note in chap7.</revremark>
</revision>
<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.0</revnumber>
<date>2004-04-27</date>
<authorinitials>TM</authorinitials>
<revremark>Initial release for LDP; more exercises, more markup, less errors and abuse; added glossary.</revremark>
</revision>
<revision>
<revnumber>1.0-beta</revnumber>
<date>2003-04-20</date>
<authorinitials>MG</authorinitials>
<revremark>Pre-release</revremark>
</revision>
</revhistory>
</para>
</section>
<section id="intro_05">
<title>Contributions</title>
<para>
Thanks to all the friends who helped (or tried to) and to my husband; your encouraging words made this work possible. Thanks to all the people who submitted bug reports, examples and remarks - among many, many others:</para>
<itemizedlist>
<listitem><para>Hans Bol, one of the groupies</para></listitem>
<listitem><para>Mike Sim, remarks on style</para></listitem>
<listitem><para>Dan Richter, for array examples</para></listitem>
<listitem><para>Gerg Ferguson, for ideas on the title</para></listitem>
<listitem><para>Mendel Leo Cooper, for making room</para></listitem>
<listitem><para>#linux.be, for keeping my feet on the ground</para></listitem>
<listitem><para>Frank Wang, for his detailed remarks on all the things I did wrong ;-)</para></listitem>
</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>
</section>
<section id="intro_06">
<title>Feedback</title>
<para>
Missing information, missing links, missing characters, remarks? Mail it to
<address><email>tille wants no spam _at_ garrels dot be</email></address>
the maintainer of this document.
</para>
</section>
<section id="intro_07">
<title>Copyright information</title>
<screen>
* Copyright (c) 2002-2007, Machtelt Garrels
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author, Machtelt Garrels, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</screen>
<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>
<para>The logos, trademarks and symbols used in this book are the properties of
their respective owners.</para>
</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>
</section>
<section id="intro_09">
<title>Conventions used in this document</title>
<para>The following typographic and usage conventions occur in this text:
</para>
<table id="conventions" frame="all"><title>Typographic and usage conventions</title>
<tgroup cols="2" align="left" colsep="1" rowsep="1">
<thead>
<row><entry>Text type</entry><entry>Meaning</entry></row>
</thead>
<tbody>
<row>
<entry><quote>Quoted text</quote></entry>
<entry>Quotes from people, quoted computer output.</entry>
</row>
<row>
<entry><screen>terminal view</screen></entry>
<entry>Literal computer input and output captured from the terminal,
usually rendered with a light grey background.</entry>
</row>
<row>
<entry><command>command</command></entry>
<entry>Name of a command that can be entered on the command line.</entry>
</row>
<row>
<entry><varname>VARIABLE</varname></entry>
<entry>Name of a variable or pointer to content of a variable, as in
<varname>$VARNAME</varname>.</entry>
</row>
<row>
<entry><option>option</option></entry>
<entry>Option to a command, as in <quote>the <option>-a</option> option to the
<command>ls</command> command</quote>.</entry>
</row>
<row>
<entry><parameter>argument</parameter></entry>
<entry>Argument to a command, as in <quote>read
<command>man <parameter>ls</parameter></command></quote>.</entry>
</row>
<row>
<entry><cmdsynopsis><command>command <option>options</option>
<parameter>arguments</parameter></command></cmdsynopsis></entry>
<entry>Command synopsis or general usage, on a separated line.</entry>
</row>
<row>
<entry><filename>filename</filename></entry>
<entry>Name of a file or directory, for example <quote>Change to the
<filename>/usr/bin</filename> directory.</quote></entry>
</row>
<row>
<entry><keycap>Key</keycap></entry>
<entry>Keys to hit on the keyboard, such as <quote>type <keycap>Q</keycap>
to quit</quote>.</entry>
</row>
<row>
<entry><guibutton>Button</guibutton></entry>
<entry>Graphical button to click, like the <guibutton>OK</guibutton>
button.</entry>
</row>
<row>
<entry><menuchoice><guimenu>Menu</guimenu><guimenuitem>Choice</guimenuitem>
</menuchoice></entry>
<entry>Choice to select from a graphical menu, for instance: <quote>Select
<menuchoice><guimenu>Help</guimenu><guimenuitem>About Mozilla</guimenuitem>
</menuchoice> in your browser.</quote></entry>
</row>
<row>
<entry><emphasis>Terminology</emphasis></entry>
<entry>Important term or concept: <quote>The Linux <emphasis>kernel</emphasis>
is the heart of the system.</quote></entry>
</row>
<row>
<entry><screen>\</screen></entry>
<entry>The backslash in a terminal view or command synopsis indicates an unfinished line. In other words, if you see a long command that is cut into multiple lines, \ means <quote>Don't press <keycap>Enter</keycap> yet!</quote></entry>
</row>
<row>
<entry>See <xref linkend="chap_01" /></entry><entry>link to related subject
within this guide.</entry>
</row>
<row>
<entry><ulink url="http://tille.garrels.be">The author</ulink></entry>
<entry>Clickable link to an external web resource.</entry>
</row>
</tbody>
</tgroup>
</table>
</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>
<para>Chapters four to six are discussions of basic tools that are commonly used in shell scripts.</para>
<para>Chapters eight to twelve discuss the most common constructs in shell scripts.</para>
<para>All chapters come with exercises that will test your preparedness for the
next chapter.</para>
<itemizedlist>
<listitem>
<para><xref linkend="chap_01" />: Bash basics: why Bash is so good, building blocks, first guidelines on developing good scripts.</para>
</listitem>
<listitem>
<para><xref linkend="chap_02" />: Script basics: writing and debugging.</para>
</listitem>
<listitem>
<para><xref linkend="chap_03" />: The Bash Environment: initialization files, variables, quoting characters, shell expansion order, aliases, options.</para>
</listitem>
<listitem>
<para><xref linkend="chap_04" />: Regular expressions: an introduction.</para>
</listitem>
<listitem>
<para><xref linkend="chap_05" />: Sed: an introduction to the sed line editor.</para>
</listitem>
<listitem>
<para><xref linkend="chap_06" />:Awk: introduction to the awk programming language.</para>
</listitem>
<listitem>
<para><xref linkend="chap_07" />: Conditional statements: constructs used in Bash to test conditions.</para>
</listitem>
<listitem>
<para><xref linkend="chap_08" />: Interactive scripts: making scripts user-friendly, catching user input.</para>
</listitem>
<listitem>
<para><xref linkend="chap_09" />: Executing commands repetitively: constructs used in Bash to automate command execution.</para>
</listitem>
<listitem>
<para><xref linkend="chap_10" />: Advanced variables: specifying variable types, introduction to arrays of variables, operations on variables.</para>
</listitem>
<listitem>
<para><xref linkend="chap_11" />: Functions: an introduction.</para>
</listitem>
<listitem>
<para><xref linkend="chap_12" />: Catching signals: introduction to process signalling, trapping user-sent signals.</para>
</listitem>
</itemizedlist>
</section>
</preface>
<toc></toc>
&chap1;
&chap2;
&chap3;
&chap4;
&chap5;
&chap6;
&chap7;
&chap8;
&chap9;
&chap10;
&chap11;
&chap12;
&app1;
&gloss;
&index-gloss;
</book>