📄 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.57"><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"><TABLEWIDTH="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">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="writingscripts.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="EXERCISES">Appendix M. Exercises</A></H1><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SCRIPTANALYSIS">M.1. Analyzing Scripts</A></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>A reader sent in the following code snippet. <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> 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 <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="VARNAME">$ROW*$COL</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%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="dosbatch.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="writingscripts.html">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 + -