Added tracer image 1x1 pixel http://tille.soti.org/images/blank-bash.png, added textobjects for all pictures, fixed wrong links in index, made signal list more clear, added front cover from fultus publishing, index, more markup in screen sections (sort of forgot chapters 6-end previously).

This commit is contained in:
tille 2004-07-09 05:39:30 +00:00
parent ad2f3e4160
commit 24e7bd1f9a
14 changed files with 650 additions and 14 deletions

View File

@ -38,7 +38,7 @@
</author>
</authorgroup>
<edition>Version 1.1 Last updated 20040615</edition>
<edition>Version 1.3 Last updated 20040709</edition>
<keywordset>
<keyword>Linux</keyword>
<keyword>Scripts</keyword>
@ -64,6 +64,14 @@
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_02">
@ -83,18 +91,56 @@
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_03">
<title>New versions of this guide</title>
<title>New versions and availability</title>
<para>The most recent edition can be found at <ulink url="http://tille.soti.org/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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_04">
<title>Revision History</title>
<para>
<revhistory>
<revision>
<revnumber>1.3</revnumber>
<date>2004-07-09</date>
<authorinitials>MG</authorinitials>
<revremark>Added tracer image 1x1 pixel http://tille.soti.org/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>
@ -124,6 +170,14 @@
</revhistory>
</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_05">
@ -140,6 +194,14 @@ Thanks to all the friends who helped (or tried to) and to my husband; your encou
<listitem><para>#linux.be, for keeping my feet on the ground</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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_06">
@ -150,6 +212,14 @@ Missing information, missing links, missing characters, remarks? Mail it to
<address><email>tille@coresequence.com</email></address>
the maintainer of this document.
</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_07">
@ -161,6 +231,14 @@ 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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_08">
@ -168,6 +246,13 @@ their respective owners.</para>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_09">
<title>Conventions used in this document</title>
@ -259,6 +344,13 @@ within this guide.</entry>
</tgroup>
</table>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>
<section id="intro_10">
<title>Organization of this document</title>
@ -307,6 +399,13 @@ next chapter.</para>
</listitem>
</itemizedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</section>

View File

@ -51,6 +51,14 @@ in the active terminal. The system finds the directory where the name occurs us
[mia@post21 ~]$
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_02"><title>Advantages of the Bourne Again SHell</title>
@ -243,6 +251,14 @@ the shell expansions or by the <command>let</command> built-in.</para>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_03"><title>Executing commands</title>
@ -278,6 +294,13 @@ 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.soti.org/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>
@ -347,6 +370,14 @@ the shell expansions or by the <command>let</command> built-in.</para>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_05"><title>Developing good scripts</title>
@ -505,6 +536,14 @@ exit 0
</screen>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_06"><title>Summary</title>
@ -517,6 +556,14 @@ exit 0
<para>Bash behaves different when in interactive mode and also has a POSIX compliant and a restricted mode.</para>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_01_07"><title>Exercises</title>
@ -544,6 +591,14 @@ exit 0
<listitem><para>How many processes are currently running on your system? Use <command>ps</command> and <command>wc</command>, the first line of output of <command>ps</command> is not a process!</para></listitem>
<listitem><para>How to display the system hostname? Only the name, nothing more!</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>

View File

@ -74,7 +74,7 @@ declare -- OTHERVAR="blah"
<para>As soon as you restrict assignment of values to a variable, it can only hold that type of data. Possible restrictions are either integer, constant or array.</para>
<para>See the Bash info pages for information on return status.</para>
</sect2>
<sect2 id="sect10_01_03"><title>Constants</title>
<sect2 id="sect_10_01_03"><title>Constants</title>
<para>In Bash, constants are created by making a variable read-only. The <command>readonly</command> built-in marks each specified variable as unchangeable. The syntax is:</para>
<cmdsynopsis><command>readonly <option>OPTION</option> <varname>VARIABLE(s)</varname></command></cmdsynopsis>
<para>The values of these variables can then no longer be changed by subsequent assignment. If the <option>-f</option> option is given, each variable refers to a shell function; see <xref linkend="chap_11" />. If <option>-a</option> is specified, each variable refers to an array of variables. If no arguments are given, or if <option>-p</option> is supplied, a list of all read-only variables is displayed. Using the <option>-p</option> option, the output can be reused as input.</para>
@ -86,6 +86,14 @@ declare -- OTHERVAR="blah"
bash: TUX: readonly variable
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -148,6 +156,9 @@ one three four
# $Id$
# $Log$
# Revision 1.5 2004/06/24 14:02:48 tille
# dded tracer image
#
# Revision 1.4 2004/06/15 08:47:12 tille
# more markup, index
#
@ -258,6 +269,14 @@ Reminder: the list of demos is here:
EOF
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -399,11 +418,27 @@ thisisaverylongstring
<para>More information can be found in the Bash info pages.</para>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/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>
@ -425,5 +460,12 @@ 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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -100,6 +100,14 @@ dir ()
}
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -217,10 +225,26 @@ echo -e "Remote backup `date` SUCCESS\n----------" &gt;&gt; "$LOGFILE"
<para>is equivalent to</para>
<cmdsynopsis><command>command &gt; <filename>file</filename> 2&gt;&amp;1</command></cmdsynopsis>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_11_04"><title>Exercises</title>
@ -231,6 +255,14 @@ echo -e "Remote backup `date` SUCCESS\n----------" &gt;&gt; "$LOGFILE"
</listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -34,11 +34,9 @@
<row><entry>Standard key combination</entry><entry>Meaning</entry></row>
</thead>
<tbody>
<row><entry><keycap>Ctrl</keycap>+<keycap>C</keycap></entry><entry>The interrupt signal.</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>S</keycap></entry><entry>Suspend output (XOFF)</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>Q</keycap></entry><entry>Resume output (XON)</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>Y</keycap></entry><entry>The <emphasis>delayed suspend</emphasis> character. Causes a running process to be stopped when it attempts to read input from the terminal. Control is returned to the shell, the user can foreground, background or kill the process.</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>Z</keycap></entry><entry>The <emphasis>suspend</emphasis> character. Stops a running program and returns control to the shell.</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>C</keycap></entry><entry>The interrupt signal, sends SIGINT to the job running in the foreground.</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>Y</keycap></entry><entry>The <emphasis>delayed suspend</emphasis> character. Causes a running process to be stopped when it attempts to read input from the terminal. Control is returned to the shell, the user can foreground, background or kill the process. Delayed suspend is only available on operating systems supporting this feature.</entry></row>
<row><entry><keycap>Ctrl</keycap>+<keycap>Z</keycap></entry><entry>The <emphasis>suspend</emphasis> signal, sends a <emphasis>SIGTSTP</emphasis> to a running program, thus stopping it and returning control to the shell.</entry></row>
</tbody>
</tgroup>
</table>
@ -72,7 +70,8 @@
<note><title>SIGKILL and SIGSTOP</title>
<para><emphasis>SIGKILL</emphasis> and <emphasis>SIGSTOP</emphasis> can not be caught, blocked or ignored.</para>
</note>
<para>When killing a process or series of processes, it is common sense to start trying with the least dangerous signal, <emphasis>SIGTERM</emphasis>. If that does not work, use the <emphasis>INT</emphasis> or<emphasis>KILL</emphasis> signals. For instance, when a process does not die using <keycap>Ctrl</keycap>+<keycap>C</keycap>, it is best to use the <command>kill <option>-9</option></command> on that process ID:</para>
<para>When killing a process or series of processes, it is common sense to start trying with the least dangerous signal, <emphasis>SIGTERM</emphasis>. That way, programs that care about an orderly shutdown get the chance to follow the procedures that they have been designed to execute when getting the <emphasis>SIGTERM</emphasis> signal, such as cleaning up and closing open files. If you send a <emphasis>SIGKILL</emphasis> to a process, you remove any chance for the process to do a tidy cleanup and shutdown, which might have unfortunate consequences.</para>
<para>But if a clean termination does not work, the <emphasis>INT</emphasis> or<emphasis>KILL</emphasis> signals might be the only way. For instance, when a process does not die using <keycap>Ctrl</keycap>+<keycap>C</keycap>, it is best to use the <command>kill <option>-9</option></command> on that process ID:</para>
<screen>
<prompt>maud: ~&gt;</prompt> <command>ps <option>-ef</option> | grep <parameter>stuck_process</parameter></command>
maud 5607 2214 0 20:05 pts/5 00:00:02 stuck_process
@ -86,6 +85,13 @@ 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.soti.org/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>
@ -144,10 +150,26 @@ exit 0
</screen>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/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>
@ -155,5 +177,13 @@ exit 0
<listitem><para>Create a script that writes a boot image to a diskette using the <command>dd</command> utility. If the user tries to interrupt the script using <keycap>Ctrl</keycap>+<keycap>C</keycap>, display a message that this action will make the diskette unusable.</para></listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -30,6 +30,9 @@
<imagedata fileref="images/script1.sh.eps" format="EPS"></imagedata></imageobject><imageobject>
<imagedata fileref="images/script1.sh.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>Example script using statements like "echo hello", "echo hello $USER" and "VARIABLE=value".</phrase>
</textobject>
</mediaobject>
</figure>
@ -103,6 +106,14 @@ I'm giving you back your prompt now.
<prompt>willy:~/scripts&gt;</prompt>
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -147,6 +158,13 @@ echo
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_02_03"><title>Debugging Bash scripts</title>
@ -280,11 +298,27 @@ echo "debug message: now attempting to start w command"; w
echo "Variable VARNAME is now set to $VARNAME."
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/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>
@ -296,5 +330,13 @@ echo "Variable VARNAME is now set to $VARNAME."
<listitem><para>Run the script in normal mode and in debug mode. It should run without errors.</para></listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -222,6 +222,9 @@ clear
<imageobject>
<imagedata fileref="images/prompt.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>Prompt 1 is "franky ~&gt;; prompt is then changed with "export PS1="\[\033[1;42m\]$USER is in \w\[\033[0m\] " resulting in a green reverse video prompt displaying username and present working directory in humanly readable form. Prompt for root is set using a similar PS1 configuration to be bright red reverse video.</phrase>
</textobject>
</mediaobject>
</figure>
<para>Most shell scripts execute in a private environment: variables are not inherited by child processes unless they are exported by the parent shell. Sourcing a file containing shell commands is a way of applying changes to your own environment and setting variables in the current shell.</para>
@ -238,6 +241,14 @@ export PS1="[some value]"
echo "PS1 is now set to $PS1"
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_02"><title>Variables</title>
@ -709,6 +720,14 @@ echo backup succeeded &gt; $LOGFILE
<para>The above is purely an example that everybody can understand, using a small directory and a host on the same subnet. Depending on your bandwidth, the size of the directory and the location of the remote server, it can take an awful lot of time to make backups using this mechanism. For larger directories and lower bandwidth, use <command>rsync</command> to keep the directories at both ends synchronized.</para>
</note>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_03"><title>Quoting characters</title>
@ -766,6 +785,14 @@ I'd say: "Go for it!"
<para>A double-quoted string preceded by a dollar sign will cause the string to be translated according to the current locale. If the current locale is <quote>C</quote> or <quote>POSIX</quote>, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_04"><title>Shell expansion</title>
@ -907,6 +934,14 @@ leading <quote>0x</quote> or <quote>0X</quote> denotes hexadecimal. Otherwise,
<para>When a pattern is used for file name generation, the character <quote>.</quote> at the start of a file name or immediately following a slash must be matched explicitly, unless the shell option <option>dotglob</option> is set. When matching a file name, the slash character must always be matched explicitly. In other cases, the <quote>.</quote> character is not treated specially.</para>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_03_05"><title>Aliases</title>
@ -984,6 +1019,14 @@ bash: dh: command not found
<tip><title>Functions are faster</title><para>Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose.</para></tip>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -1060,6 +1103,14 @@ bash: VAR: unbound variable
-rw-rw-r-- 1 willy willy 0 Feb 27 13:37 *
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
@ -1069,6 +1120,14 @@ bash: VAR: unbound variable
<para>Except for the reserved Bourne shell, Bash and special parameters, variable names can be chosen more or less freely.</para>
<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.soti.org/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>
@ -1086,5 +1145,13 @@ bash: VAR: unbound variable
<listitem><para>Write a script in which you assign two integer values to two variables. The script should calculate the surface of a rectangle which has these proportions. It should be aired with comments and generate elegant output.</para></listitem>
</orderedlist>
<para>Don't forget to <command>chmod</command> your scripts!</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -52,6 +52,13 @@
<para>Check in your system documentation whether commands using regular expressions support extended expressions.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_04_02"><title>Examples using grep</title>
@ -178,6 +185,14 @@ for i in /etc/profile.d/*.sh ; do
</screen>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -218,11 +233,26 @@ drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/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>
@ -241,6 +271,14 @@ drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/
<listitem><para>Make a script that checks whether a user exists in <filename>/etc/passwd</filename>. For now, you can specify the user name in the script, you don't have to work with arguments and conditionals at this stage.</para></listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -63,6 +63,13 @@
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -222,6 +229,14 @@ This is the final line.
<cmdsynopsis><command>grep <parameter>sed</parameter> <filename>*</filename></command></cmdsynopsis>
</tip>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -296,10 +311,26 @@ line3
<note><title>Easy sed</title>
<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.soti.org/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.soti.org/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>
@ -313,5 +344,13 @@ line3
<listitem><para>A long listing of the root directory, <filename>/</filename>, is used for input. Create a file holding <command>sed</command> commands that check for symbolic links and plain files. If a file is a symbolic link, precede it with a line like <quote>--This is a symlink--</quote>. If the file is a plain file, add a string on the same line, adding a comment like <quote>&lt;--- this is a plain file</quote>.</para></listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -30,6 +30,13 @@ 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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_02"><title>The print program</title>
@ -45,6 +52,9 @@ contain function definitions, loops, conditions and other programming constructs
<imageobject>
<imagedata fileref="images/awk.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>Fields as interpreted by awk: first column is $1, second column is $2 and so on.</phrase>
</textobject>
</mediaobject>
</figure>
<para>In the output of <command>ls <option>-l</option></command>, there are 9 columns. The <command>print</command> statement uses these fields as follows:</para>
@ -184,6 +194,14 @@ Partition /usr : 97% full!
<note><title>Syntax highlighting</title>
<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.soti.org/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>
@ -330,11 +348,26 @@ END { print "&lt;/pre&gt;\n&lt;/body&gt;\n&lt;/html&lt;" }
<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.soti.org/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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
<sect1 id="sect_06_05"><title>Exercises</title>
@ -398,5 +431,12 @@ looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line, but
</listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -227,6 +227,13 @@ Pleasure to meet you, Madame.
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -268,6 +275,9 @@ your account is managed from the local /etc/passwd file
<imagedata fileref="images/penguin.sh.eps" format="EPS"></imagedata></imageobject><imageobject>
<imagedata fileref="images/penguin.sh.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>Simple if/then/else/fi construct: if [ "$1" == fish ]; then echo "Tux likes this"; else echo "Tux wants fish!"; fi</phrase>
</textobject>
</mediaobject>
</figure>
<para>Here's another example, using two arguments:</para>
@ -424,6 +434,9 @@ This is not a leap year.
<imagedata fileref="images/leaptest.sh.eps" format="EPS"></imagedata></imageobject><imageobject>
<imagedata fileref="images/leaptest.sh.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>year=`date +%Y`; if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") &amp;&amp; ("$year" % 100 != "0") )); then echo "this is a leap year."; else echo "not a leap year"; fi</phrase>
</textobject>
</mediaobject>
</figure>
<para>We use the double brackets for testing an arithmetic expression, see <xref linkend="sect_03_04_05" />. This is equivalent to the <command>let</command> statement. You will get stuck using angular brackets here, if you try something like <command>$[$year % 400]</command>, because here, the angular brackets don't represent an actual command by themselves.</para>
@ -502,6 +515,13 @@ 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.soti.org/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>
@ -598,11 +618,26 @@ 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.soti.org/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.soti.org/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>
@ -618,5 +653,13 @@ esac
<para>The script should also check for available diskspace. Keep in mind that at any given moment you could have the data in your home directory, the data in the <filename>.tar</filename> file and the data in the compressed archive all together on your disk. If there is not enough diskspace, exit with an error message in the log file.</para>
<para>The script should clean up the compressed archive before it exits.</para></listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -152,6 +152,14 @@ Usage of the feed script: ./feed.sh food-on-menu animal-name
</table>
<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.soti.org/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>
@ -569,10 +577,26 @@ Transaction(s) Complete
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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.soti.org/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>
@ -604,5 +628,13 @@ Transaction(s) Complete
</listitem>
<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.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -55,6 +55,14 @@ done
<para>Since we don't do a line count here, there is no way of knowing the line number from which to start deleting lines until reaching the end. The problem is solved using <command>tac</command>, which reverses the lines in a file.</para>
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -89,6 +97,8 @@ done
<imagedata fileref="images/picturesort.sh.eps" format="EPS"></imagedata></imageobject><imageobject>
<imagedata fileref="images/picturesort.sh.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>PICDIR=/home/carol/pics; WEBDIR=/var/www/carol/webcam; while true; do DATE=`date +%Y%m%d`; HOUR=`date +%H`; mkdir $WEBDIR/"$DATE"; while [ $HOUR -le 23 ]; do DESTDIR=$WEBDIR/"$DATE"/"$HOUR"; mkdir "$DESTDIR"; mv $PICDIR/*.jpg "$DESTDIR"/; sleep 3600; HOUR=`date +%H`; done; done</phrase></textobject>
</mediaobject>
</figure>
<para>Note the use of the <command>true</command> statement. This means: continue execution until we are forcibly interrupted (with <command>kill</command> or <keycap>Ctrl</keycap>+<keycap>C</keycap>).</para>
@ -177,6 +187,14 @@ echo "Exiting."
</sect3>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -194,10 +212,21 @@ echo "Exiting."
<imagedata fileref="images/safesort.sh.eps" format="EPS"></imagedata></imageobject><imageobject>
<imagedata fileref="images/safesort.sh.png" format="PNG"></imagedata>
</imageobject>
<textobject>
<phrase>while true; do DISKFUL=$(df -h $WEBDIR | grep -v File | awk '{print $5 }' | cut -d "%" -f1 -); until [ $DISKFUL -ge "90" ]; do stuff from picturesort.sh; done</phrase>
</textobject>
</mediaobject>
</figure>
<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.soti.org/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>
@ -230,6 +259,14 @@ done
</screen>
<para>Files are compressed before they are moved into the archive directory.</para>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
@ -369,6 +406,14 @@ new name for TEST is test
</screen>
<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.soti.org/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>
@ -437,6 +482,14 @@ rm "$QUIT"
<para>Any statement within a <command>select</command> construct can be another <command>select</command> loop, enabling (a) submenu(s) within a menu.</para>
<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.soti.org/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>
@ -487,12 +540,28 @@ while (($#)); do
done
</screen>
</sect2>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/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>
<para>Loops can be interrupted or reiterated using the <command>break</command> and <command>continue</command> statements.</para>
<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.soti.org/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>
@ -513,5 +582,13 @@ done
</itemizedlist>
</listitem>
</orderedlist>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="http://tille.soti.org/images/blank-bash.png" format="PNG" />
</imageobject>
</mediaobject>
</informalfigure>
</sect1>
</chapter>

View File

@ -139,7 +139,7 @@
<glossentry id="charclass">
<glossterm>character classes</glossterm>
<glossdef>
<para><xref linkend="sect_04_02_02_02" />, <xref linkend="sect_04_03_02" /></para>
<para><xref linkend="sect_04_02_02_03" />, <xref linkend="sect_04_03_02" /></para>
</glossdef>
</glossentry>
@ -255,7 +255,7 @@
<glossentry id="else">
<glossterm>else</glossterm>
<glossdef>
<para><xref linkend="Sect_07_02_01" /></para>
<para><xref linkend="sect_07_02_01" /></para>
</glossdef>
</glossentry>
@ -338,7 +338,7 @@
<glossentry id="exit">
<glossterm>exit</glossterm>
<glossdef>
<para><xref linkend="sect_07__02_06" /></para>
<para><xref linkend="sect_07_02_05" /></para>
</glossdef>
</glossentry>
<glossentry id="exitstatus">
@ -380,7 +380,7 @@
<glossentry id="fileexpand">
<glossterm>file name expansion</glossterm>
<glossdef>
<para><xref linkend="sect_03_04_09" /></para>
<para><xref linkend="sect_03_04_08" /></para>
</glossdef>
</glossentry>
<glossentry id="findreplace">
@ -948,7 +948,7 @@
<glossentry id="stdout">
<glossterm>standard output</glossterm>
<glossdef>
<para><xref linkend="sect_08_02_03_0" /></para>
<para><xref linkend="sect_08_02_03_01" /></para>
</glossdef>
</glossentry>