mirror of https://github.com/tLDP/LDP
Removed some <para> tags from within <listitem> elements. Shortened the text in
some <screen> and <programlisting> elements.
This commit is contained in:
parent
a796880ad6
commit
803a0b1e57
|
@ -5,18 +5,17 @@ IMPORTANT! Before doing any CVS commits, change the DTD location from:
|
|||
|
||||
"file:///cygdrive/c/franl/work/ldp/dtd-4.2/docbookx.dtd"
|
||||
to:
|
||||
"http://docbook.org/xml/4.3/docbookx.dtd"
|
||||
"http://docbook.org/xml/4.2/docbookx.dtd"
|
||||
-->
|
||||
|
||||
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
|
||||
"http://docbook.org/xml/4.3/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">
|
||||
<!ENTITY gfdlurl "http://www.gnu.org/copyleft/fdl.html">
|
||||
]
|
||||
>
|
||||
]>
|
||||
|
||||
<!--
|
||||
************************************************************************
|
||||
|
@ -103,7 +102,7 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
You can always find the latest version of this HOWTO at the <ulink
|
||||
url="&ldpurl;">Linux Documentation Project</ulink> at this location:
|
||||
</para>
|
||||
|
||||
|
||||
<blockquote>
|
||||
<para><ulink url="&thisurl;">&thisurl;</ulink>.</para>
|
||||
</blockquote>
|
||||
|
@ -120,7 +119,7 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
containing commands that are executed by Bash. This HOWTO is an
|
||||
introduction to writing Bash scripts.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
This HOWTO is written for anyone who wants to begin writing Bash scripts
|
||||
but who has no prior scripting experience. You do not have to be a
|
||||
|
@ -159,11 +158,11 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
|
||||
<!-- Please scramble addresses; help prevent spam/email harvesting -->
|
||||
<itemizedlist spacing="compact">
|
||||
<listitem><para>Nathan Hurst</para></listitem>
|
||||
<listitem><para>Jon Abbott</para></listitem>
|
||||
<listitem><para>Felix Hudson</para></listitem>
|
||||
<listitem><para>Kees van den Broek</para></listitem>
|
||||
<listitem><para>Andreas Beck</para></listitem>
|
||||
<listitem>Nathan Hurst</listitem>
|
||||
<listitem>Jon Abbott</listitem>
|
||||
<listitem>Felix Hudson</listitem>
|
||||
<listitem>Kees van den Broek</listitem>
|
||||
<listitem>Andreas Beck</listitem>
|
||||
</itemizedlist>
|
||||
</sect2>
|
||||
|
||||
|
@ -206,7 +205,7 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
|
||||
<sect1 id="sect-getting-started">
|
||||
<title>Getting Started</title>
|
||||
|
||||
|
||||
<sect2 id="sect-what-you-should-know">
|
||||
<title>What You Should Already Know</title>
|
||||
|
||||
|
@ -214,44 +213,36 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
This HOWTO assumes you know the following things about using Bash
|
||||
interactively.
|
||||
</para>
|
||||
|
||||
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
You understand the purpose of the Bash startup files
|
||||
<filename>.bash_profile</filename> and <filename>.bashrc</filename>
|
||||
in your home directory, even if you have not editted them yourself.
|
||||
</para>
|
||||
You understand the purpose of the Bash startup files
|
||||
<filename>.bash_profile</filename> and <filename>.bashrc</filename>
|
||||
in your home directory, even if you have not editted them yourself.
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
You understand the purpose of the environment variables
|
||||
<envar>HOME</envar> and <envar>PATH</envar>.
|
||||
</para>
|
||||
You understand the purpose of the environment variables
|
||||
<envar>HOME</envar> and <envar>PATH</envar>.
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
You understand the purpose of basic shell metacharacters, such as
|
||||
"<computeroutput>*</computeroutput>",
|
||||
"<computeroutput>?</computeroutput>", and
|
||||
"<computeroutput>~</computeroutput>".
|
||||
</para>
|
||||
You understand the purpose of basic shell metacharacters, such as
|
||||
"<computeroutput>*</computeroutput>",
|
||||
"<computeroutput>?</computeroutput>", and
|
||||
"<computeroutput>~</computeroutput>".
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
You understand how to run commands in the background by appending a
|
||||
"<computeroutput>&</computeroutput>" to the end of a command.
|
||||
</para>
|
||||
You understand how to run commands in the background by appending a
|
||||
"<computeroutput>&</computeroutput>" to the end of a command.
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="sect-what-is-bash">
|
||||
<title>What and Where is Bash?</title>
|
||||
|
||||
|
||||
<para>
|
||||
Bash is a <firstterm>shell</firstterm>, which is a program that reads
|
||||
commands and executes them. Sometimes, the commands that Bash reads
|
||||
|
@ -266,7 +257,7 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
each line of the script file from top to bottom, executing each command
|
||||
as if it had been typed on the keyboard.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
Where is the Bash shell? On GNU/Linux systems, you'll always find
|
||||
Bash in <filename>/bin/bash</filename>. On commerical UNIX systems, such
|
||||
|
@ -275,7 +266,7 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
directly, but you need to know where Bash is located on your system so
|
||||
that you can write the very first line of your Bash scripts correctly.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
Often, you can find where Bash is located on a UNIX system by typing
|
||||
"<computeroutput><command>which
|
||||
|
@ -285,13 +276,13 @@ Maintainers: Francis Litterio <franl@world.std.com>,
|
|||
that doesn't work, try this
|
||||
<computeroutput><command>find</command></computeroutput> command:
|
||||
</para>
|
||||
|
||||
|
||||
<blockquote>
|
||||
<para>
|
||||
<computeroutput>find / -type f -name bash 2>/dev/null</computeroutput>
|
||||
</para>
|
||||
</blockquote>
|
||||
|
||||
|
||||
<para>
|
||||
If that doesn't work, ask your system administrator or local guru for
|
||||
help. It's possible that you do not have Bash installed on your system.
|
||||
|
@ -416,25 +407,21 @@ Hello world
|
|||
|
||||
<orderedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
Change the permissions on the script to allow you to execute it.
|
||||
This can be done with the command
|
||||
"<computeroutput><command>chmod u+x hello</command></computeroutput>".
|
||||
You don't have to grant execute permission just to yourself. The
|
||||
command
|
||||
"<computeroutput><command>chmod ugo+x hello</command></computeroutput>"
|
||||
allows everyone to execute your script. See the man page for the
|
||||
<computeroutput><command>chmod</command></computeroutput> command
|
||||
for more information.
|
||||
</para>
|
||||
Change the permissions on the script to allow you to execute it.
|
||||
This can be done with the command
|
||||
"<computeroutput><command>chmod u+x hello</command></computeroutput>".
|
||||
You don't have to grant execute permission just to yourself. The
|
||||
command
|
||||
"<computeroutput><command>chmod ugo+x hello</command></computeroutput>"
|
||||
allows everyone to execute your script. See the man page for the
|
||||
<computeroutput><command>chmod</command></computeroutput> command
|
||||
for more information.
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem id="item-dir-path">
|
||||
<para>
|
||||
Make sure that the directory containing the script is one of the
|
||||
directories listed in the value of your <envar>PATH</envar>
|
||||
environment variable.
|
||||
</para>
|
||||
Make sure that the directory containing the script is one of the
|
||||
directories listed in the value of your <envar>PATH</envar>
|
||||
environment variable.
|
||||
</listitem>
|
||||
</orderedlist>
|
||||
|
||||
|
@ -442,46 +429,38 @@ Hello world
|
|||
A good way to satisfy the requirement in item #<xref
|
||||
linkend="item-dir-path"/> above is to do this:
|
||||
</para>
|
||||
|
||||
|
||||
<orderedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
Create a directory named "<filename>bin</filename>" under your home
|
||||
directory. You can do this with the command
|
||||
"<computeroutput><command>mkdir $HOME/bin</command></computeroutput>".
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
Move the script to that directory. You can do this with the command
|
||||
"<computeroutput><command>mv hello $HOME/bin</command></computeroutput>".
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
List the full pathname of that 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>
|
||||
<para>
|
||||
<computeroutput>PATH="$HOME/bin:$PATH"</computeroutput>
|
||||
</para>
|
||||
</blockquote>
|
||||
</para>
|
||||
Create a directory named "<filename>bin</filename>" under your home
|
||||
directory. You can do this with the command
|
||||
"<computeroutput><command>mkdir $HOME/bin</command></computeroutput>".
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
<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.
|
||||
</para>
|
||||
Move the script to that directory. You can do this with the command
|
||||
"<computeroutput><command>mv hello $HOME/bin</command></computeroutput>".
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
List the full pathname of that 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>
|
||||
<para>
|
||||
<computeroutput>PATH="$HOME/bin:$PATH"</computeroutput>
|
||||
</para>
|
||||
</blockquote>
|
||||
</listitem>
|
||||
|
||||
<listitem>
|
||||
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.
|
||||
</listitem>
|
||||
</orderedlist>
|
||||
|
||||
|
@ -547,7 +526,7 @@ Hello world
|
|||
good analogy is to think of a chef cooking a meal by reading a recipe. In
|
||||
this case, your script is the recipe, and Bash is the chef.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
The first line of your script must specify the full pathname of the Bash
|
||||
shell immediately after the characters
|
||||
|
@ -584,7 +563,7 @@ Hello world
|
|||
(<computeroutput>#!/bin/bash</computeroutput>)? What happens when Bash
|
||||
reads and executes that line?
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
The answer is: nothing. The text between a
|
||||
"<computeroutput>#</computeroutput>" character and the end of the same
|
||||
|
@ -690,7 +669,7 @@ Hello world
|
|||
ignores blank lines, so you can use them to make your script more
|
||||
readable.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<example id="ex-comments">
|
||||
|
@ -698,14 +677,15 @@ Hello world
|
|||
<programlisting>
|
||||
#!/bin/bash
|
||||
# This script is an example of using comments.
|
||||
echo Hello world # This comment shares a line with a command!
|
||||
echo Hello world # This comment shares a line with a command!
|
||||
|
||||
# The next line does _not_ contain a comment! Do you see why?
|
||||
# The next line does _not_ contain a comment!
|
||||
echo Hello#world
|
||||
|
||||
# This is also a comment. The "#" doesn't have to be in the leftmost
|
||||
# column. Compare the next echo command with the previous echo command.
|
||||
# One little space can make a big difference!
|
||||
# This is also a comment. The "#" doesn't have to be
|
||||
# in the leftmost column. Compare the next echo
|
||||
# command with the previous echo command. One little
|
||||
# space can make a big difference!
|
||||
echo Hello #world
|
||||
</programlisting>
|
||||
</example>
|
||||
|
@ -784,7 +764,7 @@ Hello
|
|||
<para>
|
||||
Let's look at each one in turn.
|
||||
</para>
|
||||
|
||||
|
||||
<sect2 id="sect-double-quote">
|
||||
<title>Double Quotes</title>
|
||||
|
||||
|
@ -795,7 +775,7 @@ Hello
|
|||
one word. For instance, if you need to refer to a file that has spaces in
|
||||
its name, you can use double quotes, as follows:
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<programlisting>
|
||||
|
@ -842,7 +822,7 @@ cat "meeting agenda.txt"
|
|||
happens because you can use double quotes to group words that span a
|
||||
newline character. Try this command in your interactive shell:
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<programlisting>
|
||||
|
@ -889,7 +869,7 @@ scriptname: line 3: unexpected EOF while looking for matching `"'
|
|||
certain metacharacters. If you want your script to output an asterisk,
|
||||
you might attempt to do it with this command:
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<programlisting>
|
||||
|
@ -897,7 +877,7 @@ echo *
|
|||
</programlisting>
|
||||
</blockquote>
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
But the above command outputs the names of all files and directories in
|
||||
the script's current working directory, which is not what you intended.
|
||||
|
@ -925,7 +905,7 @@ echo "*"
|
|||
metacharacter. Let's be clear exactly which metacharacters double quotes
|
||||
escape. Double quotes escape the following Bash metacharacters.
|
||||
</para>
|
||||
|
||||
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
|
@ -934,25 +914,25 @@ echo "*"
|
|||
<computeroutput>[</computeroutput>...<computeroutput>]</computeroutput>
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
The home directory character: <computeroutput>~</computeroutput>
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
Single quotes: <computeroutput>'</computeroutput>...<computeroutput>'</computeroutput>
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
The background job creation character: <computeroutput>&</computeroutput>
|
||||
</para>
|
||||
</listitem>
|
||||
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
I/O Redirection characters: <computeroutput>></computeroutput>,
|
||||
|
@ -965,7 +945,7 @@ echo "*"
|
|||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
||||
|
||||
<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
|
||||
|
@ -973,7 +953,7 @@ echo "*"
|
|||
that the output of the script includes double quote characters? A command
|
||||
of this form doesn't work:
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<programlisting>
|
||||
|
@ -1020,16 +1000,16 @@ echo Now type ""start-backup"".
|
|||
different quote characters is that you can use one kind of quote character
|
||||
to escape the other, as <xref linkend="ex-mixing-quotes"/> shows.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<example id="ex-mixing-quotes">
|
||||
<title>Mixing Single and Double Quotes</title>
|
||||
|
||||
|
||||
<programlisting>
|
||||
#!/bin/bash
|
||||
# This script shows how you can mix single and double quotes, using one kind to
|
||||
# escape the other kind.
|
||||
# This script shows how you can mix single and double
|
||||
# quotes, using one kind to escape the other kind.
|
||||
|
||||
echo "'This text is surrounded by single quotes.'"
|
||||
echo '"This text is surrounded by double quotes."'
|
||||
|
@ -1042,7 +1022,7 @@ echo "Welcome to Frodo's Place."
|
|||
<para>
|
||||
The output of the above script is:
|
||||
</para>
|
||||
|
||||
|
||||
<blockquote>
|
||||
<para>
|
||||
<screen>
|
||||
|
@ -1210,7 +1190,7 @@ Welcome to Frodo's Place!
|
|||
|
||||
<sect2 id="sect-using">
|
||||
<title>Using Variables</title>
|
||||
|
||||
|
||||
<para>
|
||||
<xref linkend="ex-variable-assignment0"/> shows how to assign a string of
|
||||
characters to a variable.
|
||||
|
@ -1319,16 +1299,16 @@ CARPLURAL="${CARMODEL}s"
|
|||
A common task is to append (or prepend) a string to the current value of
|
||||
a variable. <xref linkend="ex-variable-append"/> shows how to do each.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<example id="ex-variable-append">
|
||||
<title>Appending and Prepending Text to a Variable's Value</title>
|
||||
<programlisting>
|
||||
CUSTOMER_NAME="Fred"
|
||||
CUSTOMER_NAME="$CUSTOMER_NAME Smith" # Appends a string.
|
||||
CUSTOMER_NAME="Mr. $CUSTOMER_NAME" # Prepends a string.
|
||||
echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
||||
CUSTOMER_NAME="$CUSTOMER_NAME Smith" # Appends a string.
|
||||
CUSTOMER_NAME="Mr. $CUSTOMER_NAME" # Prepends a string.
|
||||
echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
||||
</programlisting>
|
||||
</example>
|
||||
</blockquote>
|
||||
|
@ -1399,7 +1379,7 @@ echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
|||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
|
||||
<varlistentry>
|
||||
<term><varname>HOME</varname></term>
|
||||
<listitem>
|
||||
|
@ -1431,7 +1411,7 @@ echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
|||
provides I/O redirection, but the syntax differs between various shells.
|
||||
Naturally, we'll cover the Bash syntax.
|
||||
</para>
|
||||
|
||||
|
||||
<sect2 id="sect-stdio-streams">
|
||||
<title>The Standard I/O Streams</title>
|
||||
|
||||
|
@ -1452,12 +1432,12 @@ echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
|||
process produces output) or from the terminal to the process (this is
|
||||
how the process reads input from the keyboard).
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
When a process first starts, there are always three <firstterm>standard I/O
|
||||
streams</firstterm> connected to the process:
|
||||
</para>
|
||||
|
||||
|
||||
<itemizedlist>
|
||||
<listitem><para>Standard input (stdin)</para></listitem>
|
||||
<listitem><para>Standard output (stdout)</para></listitem>
|
||||
|
@ -1498,7 +1478,7 @@ echo "$CUSTOMER_NAME" # Output is: Mr. Fred Smith
|
|||
<blockquote>
|
||||
<example id="ex-redirect-stdout">
|
||||
<title>Redirecting Standard Output</title>
|
||||
|
||||
|
||||
<programlisting>
|
||||
#!/bin/bash
|
||||
echo Hello world > xyz
|
||||
|
@ -1518,10 +1498,10 @@ echo Hello world > xyz
|
|||
world</command></computeroutput>" command is redirected to the file
|
||||
<filename>xyz</filename> in the current working directory.
|
||||
</para>
|
||||
|
||||
|
||||
<warning>
|
||||
<title>Warning!</title>
|
||||
|
||||
|
||||
<para>
|
||||
If you redirect standard output to a file that already exists, the file
|
||||
is overwritten, destroying the previous contents of the file! Always be
|
||||
|
@ -1556,7 +1536,7 @@ echo Hello world > xyz
|
|||
<varname>$?</varname>. <xref linkend="ex-exit-status"/> shows some
|
||||
examples.
|
||||
</para>
|
||||
|
||||
|
||||
<para>
|
||||
<blockquote>
|
||||
<example id="ex-exit-status">
|
||||
|
@ -1565,14 +1545,14 @@ echo Hello world > xyz
|
|||
bash$ touch newfile
|
||||
bash$ echo $?
|
||||
0
|
||||
bash$ rm newfile # This command succeeds.
|
||||
bash$ rm newfile # This command succeeds.
|
||||
bash$ echo $?
|
||||
0
|
||||
bash$ rm newfile # This command fails because the file doesn't exist anymore.
|
||||
bash$ rm newfile # This command fails: the file doesn't exist anymore.
|
||||
rm: cannot remove `newfile': No such file or directory
|
||||
bash$ echo $?
|
||||
1
|
||||
bash$ echo $? # Can you explain this output?
|
||||
bash$ echo $? # Can you explain this output?
|
||||
0
|
||||
</screen>
|
||||
</example>
|
||||
|
|
Loading…
Reference in New Issue