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

446 lines
18 KiB
XML

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE book PUBLIC "-//OASIS/DTD DocBook XML V4.1.2//EN"
"http://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 app2 SYSTEM "app2.xml">
<!ENTITY gloss SYSTEM "gloss.xml">
<!ENTITY index-gloss SYSTEM "index-gloss.xml">
]>
<book>
<bookinfo>
<title>Bash Guide for Beginners</title>
<authorgroup>
<author>
<firstname>Machtelt</firstname>
<surname>Garrels</surname>
<affiliation>
<orgname>Xalasys.com
</orgname>
<address>
<email>tille wants no spam _at_ xalasys dot com</email>
</address>
</affiliation>
</author>
</authorgroup>
<edition>Version 1.5 Last updated 20041206</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>
</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 principal 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 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">
<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>
<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>
<para>The most recent edition can be found at <ulink url="http://tille.xalasys.com/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>
<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.5</revnumber>
<date>2004-12-06</date>
<authorinitials>MG</authorinitials>
<revremark>Changes because of new domain, minor corrections.</revremark>
</revision>
<revision>
<revnumber>1.4</revnumber>
<date>2004-10-18</date>
<authorinitials>MG</authorinitials>
<revremark>Debugging, added a couple of notes in chap9, replaced screenshots with screen sections. Corrected some typos.</revremark>
</revision>
<revision>
<revnumber>1.3</revnumber>
<date>2004-07-09</date>
<authorinitials>MG</authorinitials>
<revremark>Added tracer image 1x1 pixel http://tille.xalasys.com/images/blank-bash.png, added textobjects for all pictures, fixed wrong links in index, made signal list more clear.</revremark>
</revision>
<revision>
<revnumber>1.2</revnumber>
<date>2004-06-15</date>
<authorinitials>MG</authorinitials>
<revremark>Added index, more markup in screen sections.</revremark>
</revision>
<revision>
<revnumber>1.1</revnumber>
<date>2004-05-22</date>
<authorinitials>MG</authorinitials>
<revremark>Last read-through before going to press, added more examples, checked summaries, exercises, cleaned up introduction.</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>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</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>
<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>
<para>
Missing information, missing links, missing characters, remarks? Mail it to
<address><email>tille wants no spam _at_ xalasys dot com</email></address>
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> 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>
<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>
<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.xalasys.com">The author</ulink></entry>
<entry>Clickable link to an external web resource.</entry>
</row>
</tbody>
</tgroup>
</table>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</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>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.xalasys.com/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
</preface>
<toc></toc>
&chap1;
&chap2;
&chap3;
&chap4;
&chap5;
&chap6;
&chap7;
&chap8;
&chap9;
&chap10;
&chap11;
&chap12;
&app1;
&app2;
&gloss;
&index-gloss;
</book>