ch06s12.html 3.6 KB

12345678910111213141516171819202122232425
  1. <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">
  2. User Interaction
  3. </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s11.html">Prev</a> </td><th width="60%" align="center">Chapter 6. 
  4. Scripting
  5. </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>
  6. User Interaction
  7. </h2></div></div></div><p>
  8. 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.
  9. </p><pre class="programlisting">
  10. MAC=00:B0:52:00:BA:BE
  11. echo -n "MAC Address [${MAC}]: "; read
  12. if [ ! -z ${REPLY} ]; then
  13. MAC="${REPLY}"
  14. fi
  15. </pre><p>
  16. 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.
  17. </p><pre class="screen">
  18. MAC Address [00:B0:52:00:BA:BE]:
  19. </pre><p>
  20. 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.
  21. </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">
  22. Reading PHY Rates
  23.  </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 7. 
  24. Support Function Reference
  25. </td></tr></table></div></body></html>