Mostly small changes. Removed an example. I no longer show the output of a

script using an <example> element.  Still not ready for publication.
This commit is contained in:
franl 2002-11-13 18:28:39 +00:00
parent 2593e7469d
commit 8d8ec39b13
1 changed files with 84 additions and 59 deletions

View File

@ -8,7 +8,8 @@
-->
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://docbook.org/xml/4.2/docbookx.dtd" [
"http://docbook.org/xml/4.2/docbookx.dtd"
[
<!ENTITY ldp "Linux Documentation Project">
<!ENTITY ldpurl "http://www.tldp.org/">
<!ENTITY thisurl "&ldpurl;HOWTO/Intro-Bash-Scripting-HOWTO.html">
@ -314,12 +315,12 @@ echo Hello world</programlisting>
Create this script by typing those two lines into a file using your
favorite text editor. The name of the file can be anything you choose,
but you should <emphasis>not</emphasis> name the file
"<filename>test</filename>", because there is a built-in
<filename>test</filename>, because there is a built-in
<computeroutput><command>test</command></computeroutput> command in almost
every shell, and it's too easy to accidentally run the built-in
<computeroutput><command>test</command></computeroutput> command instead
of your own script. I suggest that you name this script
"<filename>hello</filename>".
<filename>hello</filename>.
</para>
<para>
@ -445,17 +446,16 @@ Hello world</screen>
<listitem>
<para>
List the full pathname of that
"<filename>bin</filename>" directory in the value of
your <envar>PATH</envar> environment variable. You can do this by
putting the following line into your interactive Bash startup script
(which is the file <filename>.bashrc</filename> in your home directory):
List the full pathname of that "<filename>bin</filename>" directory in
the value of your <envar>PATH</envar> environment variable (We cover
variables in more detail in <xref linkend="sect-variables"/>). You
can do this by putting the following line into your interactive Bash
startup script (which is the file <filename>.bashrc</filename> in your
home directory):
<blockquote>
<computeroutput>PATH="$HOME/bin:$PATH"</computeroutput>
</blockquote>
We cover variables in more detail in <xref linkend="sect-variables"/>.
<blockquote>
<computeroutput>PATH="$HOME/bin:$PATH"</computeroutput>
</blockquote>
</para>
</listitem>
@ -463,7 +463,7 @@ Hello world</screen>
<para>
Terminate your shell, and start a new one. You have to do this
because the changes to your <filename>.bashrc</filename> file only take
effect in shells started after you save the changes to the file.
effect in shells started after you save the changes.
</para>
</listitem>
</orderedlist>
@ -548,9 +548,9 @@ Hello world</screen>
variables (we'll cover variables in <xref linkend="sect-variables"/>). In
a Bash script, the
<computeroutput><command>echo</command></computeroutput> command is the
general purpose mechanism for producing output. It simply outputs it's
arguments with a single space between each one to the terminal on which
the script is running.
general purpose mechanism for producing output. It simply outputs its
arguments to the terminal on which the script is running. A single space
is output betwen each argument.
</para>
<para>
@ -699,30 +699,21 @@ echo Hello #world</programlisting>
"<computeroutput>#</computeroutput>" character, but there is
<emphasis>no</emphasis> comment on that line, because the
"<computeroutput>#</computeroutput>" does not occur at the beginning of a
word. <xref linkend="ex-comments2"/> shows the output produced when this
script executes (assuming it is in an file named
<filename>comments</filename>).
</para>
<para>
The line that reads
word. The line that reads
"<computeroutput>echo&nbsp;Hello&nbsp;#world</computeroutput>" contains a
comment, because the "<computeroutput>#</computeroutput>" character occurs
at the beginning of a word. As you can see from the output of this
script, that line outputs only the text
"<computeroutput>Hello</computeroutput>".
"<computeroutput>Hello</computeroutput>". When the above script executes,
it produces this output:
</para>
<para>
<blockquote>
<example id="ex-comments2">
<title>Using Comments</title>
<screen>
bash$ <command>./comments</command>
<screen>
Hello world
Hello#world
Hello</screen>
</example>
</blockquote>
</para>
@ -731,8 +722,8 @@ Hello</screen>
Professional software engineers know this is not true. Software is hard
to understand if you are not the author. Even if you are the author, it
can be hard to understand long afterwards, when it isn't fresh in your
mind. You should aim to have between 20% and 50% of the content of your
scripts be comments.
mind. Between 20% and 50% of the content of your scripts should be
comments.
</para>
</sect2>
</sect1>
@ -829,22 +820,22 @@ two lines"</programlisting>
</para>
<para>
When you press the <keysym>ENTER</keysym> key after entering the first
line of that command, your interactive shell doesn't execute the command.
Instead, it notices that you have not typed the matching double quote for
the first double quote, and prompts you to continue entering the command.
Only when a complete command (with a balanced set of double quotes) has
been entered, will pressing the <keysym>ENTER</keysym> key execute the
command. You can write the above command in a shell script too, and it
will behave the same way.
When you press the <keysym>ENTER</keysym> key after typing the first line
of that command, your interactive shell doesn't execute the command.
Instead, it notices that you have not typed a closing double quote, and
prompts you to continue entering the command. Only when a complete
command (with a balanced set of double quotes) has been entered, will
pressing the <keysym>ENTER</keysym> key execute the command. You can
write the above command in a shell script too, and it will behave the same
way.
</para>
<para>
One common mistake in shell scripts is to leave out a closing quote on one
command, and as a result you end up quoting the entire remainder of the
script &ndash; perhaps dozens or hundreds of lines of code! The error is
detected by Bash only when it reaches the end of the script and fails to
find the matching quote. The error message will look like this:
When writing a script, one common mistake is to leave out a closing quote.
As a result, you end up quoting the remainder of the script &ndash;
perhaps dozens or hundreds of lines of code! Bash detects the error only
when it reaches the end of the script and fails to find the matching
quote. The error message will look like this:
</para>
<para>
@ -892,17 +883,11 @@ echo "*"</programlisting>
</para>
<para>
Try the two <computeroutput><command>echo</command></computeroutput>
commands shown above in an interactive shell to see how they behave
differently. From now on, we'll use the more technical term
From now on, we'll use the more technical term
<firstterm>escape</firstterm> instead of <firstterm>quote</firstterm> to
refer to using a metacharacter that removes the special meaning of another
metacharacter.
</para>
<para>
Let's be clear about exactly what double quotes do. Double quotes escape
the following Bash metacharacters.
metacharacter. Let's be clear about exactly what double quotes do.
Double quotes escape the following Bash metacharacters.
</para>
<itemizedlist>
@ -936,7 +921,7 @@ echo "*"</programlisting>
<para>
From this list, you can see that the double quote metacharacter does not
escape itself. What if you want to output some text surrounded by double
quotes from a Bash script? In other words, how would write a script so
quotes from a Bash script? In other words, how would you write a script so
that the output of the script includes double quote characters? A command
of this form doesn't work:
</para>
@ -949,7 +934,7 @@ echo Now type "start-backup".</programlisting>
</para>
<para>
Try the above command from an interactive Bash shell. The double quotes
Try the above command in an interactive Bash shell. The double quotes
do not appear, because Bash removes them before executing the
<computeroutput><command>echo</command></computeroutput> command.
Instead, you must escape the double quotes. That is, you must make Bash
@ -1017,9 +1002,49 @@ Welcome to Frodo's Place.</screen>
</para>
<para>
The above example shows that you need two different quote characters so that
they can be used to quote each other, but there's another important
difference between double and single quotes.
The above example shows that you need two different quote characters so
that they can be used to quote each other, but there's another important
difference between double and single quotes. Single quotes escape more
metacharacters than double quotes do. In fact, single quotes escape
absolutely every metacharacter, except themselves. <xref
linkend="ex-quote-differences"/> shows how double quotes and single quotes
escape metacharacters differently.
</para>
<para>
<blockquote>
<example id="ex-quote-differences">
<title>Differences Between Single and Double Quotes</title>
<programlisting>
#!/bin/bash
echo "My home directory is $HOME"
echo 'My home directory is $HOME'
echo "*"
echo '*'</programlisting>
</example>
</blockquote>
</para>
<para>
The output of the above script is:
</para>
<para>
<blockquote>
<screen>
My home directory is /home/franl
My home directory is $HOME
*
*</screen>
</blockquote>
</para>
<para>
As you can see, both single and double quotes escape the
"<computeroutput>*</computeroutput>" metacharacter, but only single quotes
are powerful enough to escape the "<computeroutput>$</computeroutput>"
metacharacter (which is used to expand variables &ndash; we'll cover
variables in <xref linkend="sect-variables"/>).
</para>
<para>*** UNDER CONSTRUCTION ***</para>