📄 _chapter 12.htm
字号:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 12</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 11.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 13.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 12. Programming with the
<span class="docEmphasis">bash</span> Shell</h2><ul><li> <a class="docLink" href="#ch12lev1sec1">12.1 Introduction</a></li>
<li> <a class="docLink" href="#ch12lev1sec2">12.2 Reading User Input</a></li>
<li> <a class="docLink" href="#ch12lev1sec3">12.3 Arithmetic</a></li>
<li> <a class="docLink" href="#ch12lev1sec4">12.4 Positional Parameters and Command Line Arguments</a></li>
<li> <a class="docLink" href="#ch12lev1sec5">12.5 Conditional Constructs and Flow Control</a></li>
<li> <a class="docLink" href="#ch12lev1sec6">12.6 Looping Commands</a></li>
<li> <a class="docLink" href="#ch12lev1sec7">12.7 Functions</a></li>
<li> <a class="docLink" href="#ch12lev1sec8">12.8 Trapping Signals</a></li>
<li> <a class="docLink" href="#ch12lev1sec9">12.9 Debugging</a></li>
<li> <a class="docLink" href="#ch12lev1sec10">12.10 Processing Command Line Options with<span class="docEmphasis">getopts</span></a></li>
<li> <a class="docLink" href="#ch12lev1sec11">12.11 The <span class="docEmphasis">eval</span> Command and Parsing the Command Line</a></li>
<li> <a class="docLink" href="#ch12lev1sec12">12.12 <span class="docEmphasis">bash</span> Options</a></li>
<li> <a class="docLink" href="#ch12lev1sec13">12.13 Shell Built-In Commands</a></li>
<li> <a class="docLink" href="#ch12lev1sec14">BASH SHELL LAB EXERCISES</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch12.gif" src="ch12.gif" border="0" width="500" height="412"></p>
<h3 class="docSection1Title" id="ch12lev1sec1">12.1 Introduction</h3>
<p class="docText">When commands are executed from within a file, instead of
from the command line, the file is called a shell script and the shell is
running noninteractively. When the <span class="docEmphasis">bash</span> shell
starts running noninteractively, it looks for the environment variable,
<span class="docEmphasis">BASH_ENV</span> (<span class="docEmphasis">ENV</span>)
and starts up the file (normally <span class="docEmphasis">.bashrc</span>)
assigned as its value. After the <span class="docEmphasis">BASH_ENV</span> file
has been read, the shell will start executing commands in the script.<span id="ENB12-1"><a class="docLink" href="#EN12-1"><sup>[1]</sup></a></span></p>
<h4 class="docSection2Title" id="ch12lev2sec1">12.1.1 The Steps in Creating a Shell Script</h4>
<p class="docText">A shell script is normally written in an editor and consists
of commands interspersed with comments. Comments are preceded by a pound sign
and consist of text used to document what is going on.</p>
<p class="docText"><b>The First Line.</b> The first line at the top left corner
of the script will indicate the program that will be executing the lines in the
script. This line, commonly called the <span class="docEmphasis">shbang</span>
line, is written as</p>
<pre>#!/bin/bash</pre>
<p class="docText">The <span class="docEmphasis">#!</span> is called a magic
number and is used by the kernel to identify the program that should be
interpreting the lines in the script. This line must be the top line of your
script. The <span class="docEmphasis">bash</span> program can also accept
arguments to modify its behavior. See
<a class="docLink" href="#ch12table08">Table
12.8</a> for a list of <span class="docEmphasis">bash</span> options.</p>
<p class="docText"><b>Comments.</b> <span class="docEmphasis">Comments</span>
are lines preceded by a pound sign (<span class="docEmphasis">#</span>) and can
be on a line by themselves or on a line following a script command. They are
used to document your script. It is sometimes difficult to understand what the
script is supposed to do if it is not commented. Although comments are
important, they are often too sparse or not used at all. Try to get used to
commenting what you are doing not only for someone else, but also for yourself.
Two days from now you may not recall exactly what you were trying to do.</p>
<p class="docText"><span class="docEmphStrong">Executable Statements and
<span class="docEmphasis">bash</span> Shell Constructs.</span> A
<span class="docEmphasis">bash</span> shell program consists of a combination of
UNIX commands, <span class="docEmphasis">bash</span> shell commands, programming
constructs, and comments.</p>
<p class="docText"><b>Making the Script Executable.</b> When you create a file,
it is not given the execute permission. You need this permission to run your
script. Use the <span class="docEmphasis">chmod</span> command to turn on the
execute permission.</p>
<h5 id="ch12list01" class="docExampleTitle">Example 12.1 </h5>
<pre>1 $ <span class="docEmphStrong">chmod +x myscript</span>
2 $ <span class="docEmphStrong">ls -lF myscript</span>
<span class="docEmphasis">-rwxr-xr-x 1 ellie 0 Jul 13:00 myscript*</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">chmod</span> command is
used to turn on the execute permission for the user, group, and others.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The output of the <span class="docEmphasis">ls</span>
command indicates that all users have execute permission on the
<span class="docEmphasis">myscript</span> file. The asterisk at the end of
the filename also indicates that this is an executable program.</span></li>
</ol>
</span></td>
</tr>
</table>
<p class="docText"><b>A Scripting Session.</b> In the following example, the
user will create a script in the editor. After the user saves the file, the
execute permissions are turned on, and the script is executed. If there are
errors in the program, the shell will respond immediately.</p>
<h5 id="ch12list02" class="docExampleTitle">Example 12.2 </h5>
<pre>(The Script)
1 #!/bin/bash
2 # <span class="docEmphasis">This is the first Bash shell program of the day.</span>
# <span class="docEmphasis">Scriptname: greetings</span>
# <span class="docEmphasis">Written by: Barbara Bashful</span>
3 echo "Hello $LOGNAME, it's nice talking to you."
4 echo "Your present working directory is 'pwd'."
echo "You are working on a machine called 'uname -n'."
echo "Here is a list of your files."
5 ls <span class="docEmphasis"># List files in the present working directory</span>
6 echo "Bye for now $LOGNAME. The time is 'date +%T'!"
(The Command Line)
$ <span class="docEmphStrong">greetings</span> <span class="docEmphasis"># Don't forget to turn turn on x permission!</span>
<span class="docEmphasis">bash: ./greetings: Permission denied.</span>
$ <span class="docEmphStrong">chmod +x greetings</span>
$ <span class="docEmphStrong">greetings</span> <span class="docEmphasis">or</span> <span class="docEmphStrong">./greetings</span>
3 <span class="docEmphasis">Hello barbara, it's nice talking to you.</span>
4 <span class="docEmphasis">Your present working directory is /home/lion/barbara/prog</span>
<span class="docEmphasis">You are working on a machine called lion.</span>
<span class="docEmphasis">Here is a list of your files.</span>
5 <span class="docEmphasis">Afile cplus letter prac</span>
<span class="docEmphasis">Answerbook cprog library prac1</span>
<span class="docEmphasis">bourne joke notes perl5</span>
6 <span class="docEmphasis">Bye for now barbara. The time is 18:05:07!</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The first line of the script, <span class="docEmphasis">
#!/bin/bash,</span> lets the kernel know what interpreter will execute the
lines in this program, in this case the <span class="docEmphasis">bash</span>
(Bourne Again shell) interpreter.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The comments are nonexecutable lines preceded by a
pound sign. They can be on a line by themselves or appended to a line
after a command.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">After variable substitution is performed by the shell,
the <span class="docEmphasis">echo</span> command displays the line on the
screen.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">After command substitution is performed by the shell,
the <span class="docEmphasis">echo</span> command displays the line on the
screen.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">ls</span> command is
executed. The comment will be ignored by the shell.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">echo</span> command
displays the string enclosed within double quotes. Variables and command
substitution (backquotes) are expanded when placed within double quotes.
In this case, the quotes were really not necessary.</span></li>
</ol>
</span></td>
</tr>
</table>
<h3 class="docSection1Title" id="ch12lev1sec2">12.2 Reading User Input</h3>
<h4 class="docSection2Title" id="ch12lev2sec2">12.2.1 Variables (Review)</h4>
<p class="docText">In the last chapter we talked about declaring and unsetting
variables. Variables are set local to the current shell or as environment
variables. Unless your shell script will invoke another script, variables are
normally set as local variables within a script. (See "<a class="docLink" href="_chapter 7.htm#ch07lev1sec1">Variables</a>".)</p>
<p class="docText">To extract the value from a variable, precede the variable
with a dollar sign. You can enclose the variable within double quotes and the
dollar sign will be interpreted by the shell for variable expansion. Variable
expansion is not performed if the variable is enclosed in single quotes.</p>
<h5 id="ch12list03" class="docExampleTitle">Example 12.3 </h5>
<pre>1 name="John Doe" <span class="docEmphasis">or</span> declare name="John Doe" <span class="docEmphasis"># local variable</span>
2 export NAME="John Doe" <span class="docEmphasis"># global variable</span>
3 echo "$name" "$NAME" <span class="docEmphasis"># extract the value</span>
</pre>
<h4 class="docSection2Title" id="ch12lev2sec3">12.2.2 The <span class="docEmphasis">read</span>
Command</h4>
<p class="docText">The <span class="docEmphasis">read</span> command is a
built-in command used to read input from the terminal or from a file (see
<a class="docLink" href="#ch12table01">Table 12.1</a>). The
<span class="docEmphasis">read</span> command takes a line of input until a
newline is reached. The newline at the end of a line will be translated into a
null byte when read. If no names are supplied, the line read is assigned to the
special built-in variable, <span class="docEmphasis">REPLY.</span> You can also
use the <span class="docEmphasis">read</span> command to cause a program to stop
until the user hits Enter. To see how the <span class="docEmphasis">read</span>
command is most effectively used for reading lines of input from a file, see "<a class="docLink" href="#ch12lev1sec6">Looping
Commands</a>". The <span class="docEmphasis">杛</span> option to
<span class="docEmphasis">read</span> causes the backslash/newline pair to be
ignored; the backslash is treated as part of the line. The
<span class="docEmphasis">read</span> command has four options to control its
behavior: <span class="docEmphasis">朼, 杄, 杙,</span> and
<span class="docEmphasis">杛.</span><span id="ENB12-2"><a class="docLink" href="#EN12-2"><sup>[2]</sup></a></span></p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch12table01" class="docTableTitle">Table 12.1. The <span class="docEmphasis">read</span>
Command</h5>
</caption>
<colgroup span="2" align="left">
</colgroup>
<tr>
<th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
Format</span> </th>
<th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
Meaning</span> </th>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read answer</span>
</td>
<td class="docTableCell" vAlign="top">Reads a line from standard input and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -