📄 _chapter 12.htm
字号:
assigns it to the variable <span class="docEmphasis">answer.</span> </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read first
last</span> </td>
<td class="docTableCell" vAlign="top">Reads a line from standard input to
the first whitespace or newline, putting the first word typed into the
variable <span class="docEmphasis">first</span> and the rest of the line
into the variable <span class="docEmphasis">last.</span> </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read</span>
</td>
<td class="docTableCell" vAlign="top">Reads a line from standard input and
assigns it to the built-in variable, <span class="docEmphasis">REPLY.</span>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read 朼
arrayname</span> </td>
<td class="docTableCell" vAlign="top">Reads a list of words into an array
called <span class="docEmphasis">arrayname.</span><sup class="docFootnote"><a class="docLink" href="#ch12tabfn03">[a]</a></sup>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read 杄</span>
</td>
<td class="docTableCell" vAlign="top">Used in interactive shells with
command line editing in effect; e.g., if editor is <span class="docEmphasis">
vi,</span> <span class="docEmphasis">vi</span> commands can be used on the
input line.<sup class="docFootnote"><a class="docLink" href="#ch12tabfn03">[a]</a></sup>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read 杙
prompt</span> </td>
<td class="docTableCell" vAlign="top">Prints a prompt, waits for input, and
stores input in <span class="docEmphasis">REPLY</span> variable.<sup class="docFootnote"><a class="docLink" href="#ch12tabfn03">[a]</a></sup>
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">read 杛 line</span>
</td>
<td class="docTableCell" vAlign="top">Allows the input to contain a
backslash.<sup class="docFootnote"><a class="docLink" href="#ch12tabfn03">[a]</a></sup></td>
</tr>
</table>
<blockquote>
<p class="docFootnote"><sup><a name="ch12tabfn03">[a]</a></sup> Not
implemented on versions of <span class="docEmphasis">bash</span> prior to 2.0.</p>
</blockquote>
<h5 id="ch12list04" class="docExampleTitle">Example 12.4 </h5>
<pre>(The Script)
#!/bin/bash
<span class="docEmphasis">#</span> <span class="docEmphasis">Scriptname: nosy</span>
echo -e "Are you happy? \c"
1 <span class="docEmphStrong">read answer</span>
echo "$answer is the right response."
echo -e "What is your full name? \c"
2 <span class="docEmphStrong">read first middle last</span>
echo "Hello $first"
echo 杗 "Where do you work? "
3 <span class="docEmphStrong">read</span>
4 echo I guess $REPLY keeps you busy!
-----------------------------------------------------<sup class="docFootnote"><a class="docLink" href="#ch12fn04">[a]</a></sup>
5 <span class="docEmphStrong">read -p</span> "Enter your job title: "
6 echo "I thought you might be an $REPLY."
7 echo -n "Who are your best friends? "
8 <span class="docEmphStrong">read -a friends</span>
9 echo "Say hi to <span class="docEmphStrong">${friends[2]}."</span>
-------------------------------------------------------
(The Output)
$ <span class="docEmphStrong">nosy</span>
<span class="docEmphasis">Are you happy?</span> <span class="docEmphBoldItalic">Yes</span>
1 <span class="docEmphasis">Yes is the right response.</span>
2 <span class="docEmphasis">What is your full name?</span> <span class="docEmphBoldItalic">Jon Jake Jones</span>
<span class="docEmphasis">Hello Jon</span>
3 <span class="docEmphasis">Where do you work?</span> <span class="docEmphBoldItalic">the</span> <span class="docEmphBoldItalic">Chico Nut Factory</span>
4 <span class="docEmphasis">I guess the Chico Nut Factory keeps you busy!</span>
5 <span class="docEmphasis">Enter your job title:</span> <span class="docEmphBoldItalic">Accountant</span>
6 <span class="docEmphasis">I thought you might be an Accountant.</span>
7,8 <span class="docEmphasis">Who are your best friends?</span> <span class="docEmphBoldItalic">Melvin Tim Ernesto</span>
9 <span class="docEmphasis">Say hi to Ernesto.</span>
</pre>
<blockquote>
<p class="docFootnote"><sup><a name="ch12fn04">[a]</a></sup> The commands
listed below this line are not implemented on versions of
<span class="docEmphasis">bash</span> prior to 2.x.</p>
</blockquote>
<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">read</span> command
accepts a line of user input and assigns the input to the variable
<span class="docEmphasis">answer.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">read</span> command
accepts input from the user and assigns the first word of input to the
variable <span class="docEmphasis">first,</span> the second word of input
to the variable <span class="docEmphasis">middle,</span> and all the rest
of the words up to the end of the line to the variable
<span class="docEmphasis">last.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">A line is read from standard input and stored in the
built-in <span class="docEmphasis">REPLY</span> variable.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The value of the <span class="docEmphasis">REPLY</span>
variable is printed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">With the <span class="docEmphasis">杙</span> option,
the <span class="docEmphasis">read</span> command produces a prompt,
<span class="docEmphasis">Enter your job title:</span> and stores the line
of input in the special built-in <span class="docEmphasis">REPLY</span>
variable.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The value of the <span class="docEmphasis">REPLY</span>
variable is displayed in the string.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The user is asked to enter input.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">With the <span class="docEmphasis">朼</span> option,
the <span class="docEmphasis">read</span> command takes input as an array
of words. The array is called <span class="docEmphasis">friends.</span>
The elements read into the array are <span class="docEmphasis">Melvin,
Tim,</span> and <span class="docEmphasis">Ernesto.</span></span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The third element of the <span class="docEmphasis">
friends</span> array, <span class="docEmphasis">Ernesto,</span> is
printed. Array indices start at 0.</span></li>
</ol>
</span></td>
</tr>
</table>
<h5 id="ch12list05" class="docExampleTitle">Example 12.5 </h5>
<pre>(The Script)
#!/bin/bash
<span class="docEmphasis"># Scriptname: printer_check</span>
<span class="docEmphasis"># Script to clear a hung-up printer</span>
1 if [ $LOGNAME != root ]
then
echo "Must have root privileges to run this program"
exit 1
fi
2 cat << EOF
Warning: All jobs in the printer queue will be removed.
Please turn off the printer now. Press return when you
are ready to continue. Otherwise press Control C.
EOF
3 <span class="docEmphStrong">read JUNK</span> <span class="docEmphasis"># Wait until the user turns off the printer</span>
echo
4 /etc/rc.d/init.d/lpd stop <span class="docEmphasis"># Stop the printer</span>
5 echo -e "\nPlease turn the printer on now."
6 echo "Press Enter to continue"
7 <span class="docEmphStrong">read JUNK</span> <span class="docEmphasis"># Stall until the user turns the printer</span>
<span class="docEmphasis"># back on</span>
echo <span class="docEmphasis"># A blank line is printed</span>
8 /etc/rc.d/init.d/lpd start <span class="docEmphasis"># Start the printer</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">Checks to see if user is <span class="docEmphasis">
root.</span> If not, sends an error and exits.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Creates a <span class="docEmphasis">here document.</span>
Warning message is displayed on the screen.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">read</span> command waits
for user input. When the user presses Enter, the variable
<span class="docEmphasis">JUNK</span> accepts whatever is typed. The
variable is not used for anything. The <span class="docEmphasis">read</span>
in this case is used to wait until the user turns off the printer, comes
back, and presses Enter.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">lpd</span> program stops
the printer daemon.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Now it's time to turn the printer back on!</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The user is asked to press Enter when ready.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Whatever the user types is read into the variable
<span class="docEmphasis">JUNK,</span> and when Enter is pressed, the
program will resume execution.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">The <span class="docEmphasis">lpd</span> program starts
the print daemons.</span></li>
</ol>
</span></td>
</tr>
</table>
<h3 class="docSection1Title" id="ch12lev1sec3">12.3 Arithmetic</h3>
<h4 class="docSection2Title" id="ch12lev2sec4">12.3.1 Integers (<span class="docEmphasis">declare</span>
and <span class="docEmphasis">let</span> Commands)</h4>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
declare</span> Command.</span> Variables can be declared as integers with the
<span class="docEmphasis">declare 杋</span> command. If you attempt to assign
any string value, <span class="docEmphasis">bash</span> assigns
<span class="docEmphasis">0</span> to the variable. Arithmetic can be performed
on variables that have been declared as integers. (If the variable has not been
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -