📄 exercises.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 #!/bin/bash 2 3 MAX=10000 4 5 6 for((nr=1; nr<$MAX; nr++)) 7 do 8 9 let "t1 = nr % 5" 10 if [ "$t1" -ne 3 ] 11 then 12 continue 13 fi 14 15 let "t2 = nr % 7" 16 if [ "$t2" -ne 4 ] 17 then 18 continue 19 fi 20 21 let "t3 = nr % 9" 22 if [ "$t3" -ne 5 ] 23 then 24 continue 25 fi 26 27 break # What happens when you comment out this line? Why? 28 29 done 30 31 echo "Number = $nr" 32 33 34 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 #!/bin/bash 2 3 DIRNAME=/usr/bin 4 FILETYPE="shell script" 5 LOGFILE=logfile 6 7 file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l 8 9 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 #!/bin/bash 2 3 # Author: Nathan Coulter 4 # This code is released to the public domain. 5 # The author gave permission to use this code snippet in the ABS Guide. 6 7 find -maxdepth 1 -type f -printf '%f\000' | { 8 while read -d $'\000'; do 9 mv "$REPLY" "$(date -d "$(stat -c '%y' "$REPLY") " '+%Y%m%d%H%M%S' 10 )-$REPLY" 11 done 12 } 13 14 # Warning: Test-drive this script in a "scratch" directory. 15 # 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 while read LINE 2 do 3 echo $LINE 4 done < `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 export SUM=0; for f in $(find src -name "*.java"); 2 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"> </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 + -