⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercises.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Exercises</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Converting DOS Batch Files to Shell Scripts"HREF="dosbatch.html"><LINKREL="NEXT"TITLE="Writing Scripts"HREF="writingscripts.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="dosbatch.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="writingscripts.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="EXERCISES"></A>Appendix M. Exercises</H1><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SCRIPTANALYSIS"></A>M.1. Analyzing Scripts</H1><P>Examine the following script. Run it, then explain what it        does.  Annotate the script and rewrite it in a more compact and        elegant manner.</P><P>       <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;MAX=10000   4&nbsp;   5&nbsp;   6&nbsp;  for((nr=1; nr&#60;$MAX; nr++))   7&nbsp;  do   8&nbsp;   9&nbsp;    let "t1 = nr % 5"  10&nbsp;    if [ "$t1" -ne 3 ]  11&nbsp;    then  12&nbsp;      continue  13&nbsp;    fi  14&nbsp;  15&nbsp;    let "t2 = nr % 7"  16&nbsp;    if [ "$t2" -ne 4 ]  17&nbsp;    then  18&nbsp;      continue  19&nbsp;    fi  20&nbsp;  21&nbsp;    let "t3 = nr % 9"  22&nbsp;    if [ "$t3" -ne 5 ]  23&nbsp;    then  24&nbsp;      continue  25&nbsp;    fi  26&nbsp;  27&nbsp;  break   # What happens when you comment out this line? Why?  28&nbsp;  29&nbsp;  done  30&nbsp;  31&nbsp;  echo "Number = $nr"  32&nbsp;  33&nbsp;  34&nbsp;exit 0</PRE></TD></TR></TABLE>       </P><P>---</P><P>Explain what the following script does. It is really just        a parameterized command-line pipe.</P><P>        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;DIRNAME=/usr/bin   4&nbsp;FILETYPE="shell script"   5&nbsp;LOGFILE=logfile   6&nbsp;   7&nbsp;file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l   8&nbsp;   9&nbsp;exit 0</PRE></TD></TR></TABLE>      </P><P>---</P><P>Examine and explain the following script. For hints, you        might refer to the listings for <AHREF="moreadv.html#FINDREF">find</A> and <AHREF="system.html#STATREF">stat</A>.</P><P>        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Author:  Nathan Coulter   4&nbsp;# This code is released to the public domain.   5&nbsp;# The author gave permission to use this code snippet in the ABS Guide.   6&nbsp;   7&nbsp;find -maxdepth 1 -type f -printf '%f\000'  | {   8&nbsp;   while read -d $'\000'; do   9&nbsp;      mv "$REPLY" "$(date -d "$(stat -c '%y' "$REPLY") " '+%Y%m%d%H%M%S'  10&nbsp;      )-$REPLY"  11&nbsp;   done  12&nbsp;}  13&nbsp;  14&nbsp;# Warning: Test-drive this script in a "scratch" directory.  15&nbsp;# It will somehow affect all the files there.</PRE></TD></TR></TABLE>      </P><P>---</P><P>A reader sent in the following code snippet.</P><P>         <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;while read LINE   2&nbsp;do   3&nbsp;  echo $LINE   4&nbsp;done &#60; `tail -f /var/log/messages`</PRE></TD></TR></TABLE>	 </P><P>He wished to write a script tracking changes to the system log	   file, <TTCLASS="FILENAME">/var/log/messages</TT>. Unfortunately,	   the above code block hangs and does nothing	   useful. Why? Fix this so it does work. (Hint:	   rather than <AHREF="redircb.html#REDIRREF">redirecting the	   <TTCLASS="FILENAME">stdin</TT> of the loop</A>, try a <AHREF="special-chars.html#PIPEREF">pipe</A>.)</P><P>---</P><P>Analyze the following <SPANCLASS="QUOTE">"one-liner"</SPAN> (here        split into two lines for clarity) contributed by Rory        Winston:</P><P>      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;export SUM=0; for f in $(find src -name "*.java");   2&nbsp;do export SUM=$(($SUM + $(wc -l $f | awk '{ print $1 }'))); done; echo $SUM</PRE></TD></TR></TABLE>	 </P><P>Hint: First, break the script up into bite-sized	sections. Then, carefully examine its use of <AHREF="dblparens.html">double-parentheses</A> arithmetic,	the <AHREF="internal.html#EXPORTREF">export</A> command,	the <AHREF="moreadv.html#FINDREF">find</A> command, the	<AHREF="textproc.html#WCREF">wc</A> command, and <AHREF="awk.html#AWKREF">awk</A>.</P><P>---</P><P>Analyze <AHREF="contributed-scripts.html#LIFESLOW">Example A-10</A>, and reorganize it in a	simplified and more logical style. See how many of the variables	can be eliminated, and try to optimize the script to speed up	its execution time.</P><P>Alter the script so that it accepts any ordinary ASCII	text file as input for its initial <SPANCLASS="QUOTE">"generation"</SPAN>. The	script will read the first <TTCLASS="PARAMETER"><I>$ROW*$COL</I></TT>	characters, and set the occurrences of vowels as	<SPANCLASS="QUOTE">"living"</SPAN> cells. Hint: be sure to translate the	spaces in the input file to underscore characters.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="dosbatch.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="writingscripts.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Converting DOS Batch Files to Shell Scripts</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Writing Scripts</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -