1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>User Interaction</title><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><meta name="keywords" content="Intellon, Atheros, Qualcomm, HomePlug, powerline, communications, INT6000, INT6300, INT6400, AR7400, AR7420"><link rel="home" href="index.html" title="Qualcomm Atheros Open Powerline Toolkit"><link rel="up" href="ch06.html" title="Chapter 6. Scripting"><link rel="prev" href="ch06s11.html" title="Reading PHY Rates"><link rel="next" href="ch07.html" title="Chapter 7. Support Function Reference"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">
User Interaction
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s11.html">Prev</a> </td><th width="60%" align="center">Chapter 6.
Scripting
</th><td width="20%" align="right"> <a accesskey="n" href="ch07.html">Next</a></td></tr></table><hr></div><div class="section" title="User Interaction"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="scripting-interaction"></a>
User Interaction
</h2></div></div></div><p>
Scripts should be designed, whenever possible, to perform correctly without user intervention but there are cases where user intervention is appropriate. The following example illustrates one <span class="application">bash</span> shell method that only requires user input when default values are wrong.
</p><pre class="programlisting">
MAC=00:B0:52:00:BA:BE
echo -n "MAC Address [${MAC}]: "; read
if [ ! -z ${REPLY} ]; then
MAC="${REPLY}"
fi
</pre><p>
First, we define symbol <acronym class="acronym">MAC</acronym> with a default value. The Linux <span class="application">echo</span> utility prints a prompt on the console that includes the symbol value. The trailing newline is suppressed (<code class="option">-n</code>) so that text can be typed immediately after the prompt. The <span class="application">echo</span> command is terminated with semicolon (<strong class="userinput"><code>;</code></strong>) so that another command can be included on the same line. The shell <span class="application">read</span> statement waits for the user to type something and press the <strong class="userinput"><code>enter</code></strong> key. The shell will assign the input to shell variable <code class="varname">REPLY</code>. The value of <code class="varname">REPLY</code> is evaluated and used to redefine the symbol only if the input was a non-zero length string.
</p><pre class="screen">
MAC Address [00:B0:52:00:BA:BE]:
</pre><p>
The user will see something like this. If the value is correct the user can press the <strong class="userinput"><code>enter</code></strong> key to generate a zero length string. Otherwise, the user can type the correct value before pressing the <strong class="userinput"><code>enter</code></strong> key.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s11.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch07.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">
Reading PHY Rates
</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 7.
Support Function Reference
</td></tr></table></div></body></html>
|