📄 writingscripts.html
字号:
></DT><DD><P>What is the most efficient way to make change for $1.68, using only coins in common circulations (up to 25c)? It's 6 quarters, 1 dime, a nickel, and three cents.</P><P>Given any arbitrary command line input in dollars and cents ($*.??), calculate the change, using the minimum number of coins. If your home country is not the United States, you may use your local currency units instead. The script will need to parse the command line input, then change it to multiples of the smallest monetary unit (cents or whatever). Hint: look at <AHREF="functions.html#EX61">Example 23-8</A>.</P></DD><DT><BCLASS="COMMAND">Quadratic Equations</B></DT><DD><P>Solve a <SPANCLASS="QUOTE">"quadratic"</SPAN> equation of the form <TTCLASS="PARAMETER"><I>Ax^2 + Bx + C = 0</I></TT>. Have a script take as arguments the coefficients, <TTCLASS="USERINPUT"><B>A</B></TT>, <TTCLASS="USERINPUT"><B>B</B></TT>, and <TTCLASS="USERINPUT"><B>C</B></TT>, and return the solutions to four decimal places.</P><P>Hint: pipe the coefficients to <AHREF="mathc.html#BCREF">bc</A>, using the well-known formula, <TTCLASS="PARAMETER"><I>x = ( -B +/- sqrt( B^2 - 4AC ) ) / 2A</I></TT>.</P></DD><DT><BCLASS="COMMAND">Sum of Matching Numbers</B></DT><DD><P>Find the sum of all five-digit numbers (in the range 10000 - 99999) containing <SPANCLASS="emphasis"><ICLASS="EMPHASIS">exactly two</I></SPAN> out of the following set of digits: { 4, 5, 6 }. These may repeat within the same number, and if so, they count once for each occurrence.</P><P>Some examples of matching numbers are 42057, 74638, and 89515.</P></DD><DT><BCLASS="COMMAND">Lucky Numbers</B></DT><DD><P>A "lucky number" is one whose individual digits add up to 7, in successive additions. For example, 62431 is a "lucky number" (6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the "lucky numbers" between 1000 and 10000.</P></DD><DT><BCLASS="COMMAND">Alphabetizing a String</B></DT><DD><P>Alphabetize (in ASCII order) an arbitrary string read from the command line.</P></DD><DT><BCLASS="COMMAND">Parsing</B></DT><DD><P>Parse <TTCLASS="FILENAME">/etc/passwd</TT>, and output its contents in nice, easy-to-read tabular form.</P></DD><DT><BCLASS="COMMAND">Logging Logins</B></DT><DD><P>Parse <TTCLASS="FILENAME">/var/log/messages</TT> to produce a nicely formatted file of user logins and login times. The script may need to run as <ICLASS="FIRSTTERM">root</I>. (Hint: Search for the string <SPANCLASS="QUOTE">"LOGIN."</SPAN>)</P></DD><DT><BCLASS="COMMAND">Pretty-Printing a Data File</B></DT><DD><P>Certain database and spreadsheet packages use save-files with <ICLASS="FIRSTTERM">comma-separated values</I> (CSVs). Other applications often need to parse these files.</P><P>Given a data file with comma-separated fields, of the form: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989 2 Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612 3 ...</PRE></TD></TR></TABLE> Reformat the data and print it out to <TTCLASS="FILENAME">stdout</TT> in labeled, evenly-spaced columns.</P></DD><DT><BCLASS="COMMAND">Justification</B></DT><DD><P>Given ASCII text input either from <TTCLASS="FILENAME">stdin</TT> or a file, adjust the word spacing to right-justify each line to a user-specified line-width, then send the output to <TTCLASS="FILENAME">stdout</TT>.</P></DD><DT><BCLASS="COMMAND">Mailing List</B></DT><DD><P>Using the <AHREF="communications.html#COMMMAIL1">mail</A> command, write a script that manages a simple mailing list. The script automatically e-mails the monthly company newsletter, read from a specified text file, and sends it to all the addresses on the mailing list, which the script reads from another specified file.</P></DD><DT><BCLASS="COMMAND">Generating Passwords</B></DT><DD><P>Generate pseudorandom 8-character passwords, using characters in the ranges [0-9], [A-Z], [a-z]. Each password must contain at least two digits.</P></DD><DT><BCLASS="COMMAND">Monitoring a User</B></DT><DD><P>You suspect that one particular user on the network has been abusing his privileges and possibly attempting to hack the system. Write a script to automatically monitor and log his activities when he's signed on. The log file will save entries for the previous week, and delete those entries more than seven days old.</P><P>You may use <AHREF="system.html#LASTREF">last</A>, <AHREF="system.html#LASTLOGREF">lastlog</A>, and <AHREF="system.html#LASTCOMMREF">lastcomm</A> to aid your surveillance of the suspected malefactor.</P></DD><DT><BCLASS="COMMAND">Checking for Broken Links</B></DT><DD><P>Using <AHREF="communications.html#LYNXREF">lynx</A> with the <TTCLASS="OPTION">-traversal</TT> option, write a script that checks a Web site for broken links.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="EXDIFFICULT1"></A>DIFFICULT</B></P><DL><DT><BCLASS="COMMAND">Testing Passwords</B></DT><DD><P>Write a script to check and validate passwords. The object is to flag <SPANCLASS="QUOTE">"weak"</SPAN> or easily guessed password candidates.</P><P>A trial password will be input to the script as a command line parameter. To be considered acceptable, a password must meet the following minimum qualifications: <UL><LI><P>Minimum length of 8 characters</P></LI><LI><P>Must contain at least one numeric character</P></LI><LI><P>Must contain at least one of the following non-alphabetic characters: <SPANCLASS="TOKEN">@</SPAN>, <SPANCLASS="TOKEN">#</SPAN>, <SPANCLASS="TOKEN">$</SPAN>, <SPANCLASS="TOKEN">%</SPAN>, <SPANCLASS="TOKEN">&</SPAN>, <SPANCLASS="TOKEN">*</SPAN>, <SPANCLASS="TOKEN">+</SPAN>, <SPANCLASS="TOKEN">-</SPAN>, <SPANCLASS="TOKEN">=</SPAN></P></LI></UL></P><P>Optional: <UL><LI><P>Do a dictionary check on every sequence of at least four consecutive alphabetic characters in the password under test. This will eliminate passwords containing embedded <SPANCLASS="QUOTE">"words"</SPAN> found in a standard dictionary.</P></LI><LI><P>Enable the script to check all the passwords on your system. These may or may not reside in <TTCLASS="FILENAME">/etc/passwd</TT>.</P></LI></UL></P><P>This exercise tests mastery of <AHREF="regexp.html#REGEXREF">Regular Expressions</A>.</P></DD><DT><BCLASS="COMMAND">Cross Reference</B></DT><DD><P>Write a script that generates a <ICLASS="FIRSTTERM">cross-reference</I> (<ICLASS="FIRSTTERM">concordance</I>) on a target file. The output will be a listing of all word occurrences in the target file, along with the line numbers in which each word occurs. Traditionally, <ICLASS="FIRSTTERM">linked list</I> constructs would be used in such applications. Therefore, you should investigate <AHREF="arrays.html#ARRAYREF">arrays</A> in the course of this exercise. <AHREF="textproc.html#WF">Example 15-12</A> is probably <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> a good place to start.</P></DD><DT><ANAME="NEWTONSQRT"></A><BCLASS="COMMAND">Square Root</B></DT><DD><P>Write a script to calculate square roots of numbers using <ICLASS="FIRSTTERM">Newton's Method</I>.</P><P>The algorithm for this, expressed as a snippet of Bash <AHREF="assortedtips.html#PSEUDOCODEREF">pseudo-code</A> is:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # (Isaac) Newton's Method for speedy extraction 2 #+ of square roots. 3 4 guess = $argument 5 # $argument is the number to find the square root of. 6 # $guess is each successive calculated "guess" -- or trial solution -- 7 #+ of the square root. 8 # Our first "guess" at a square root is the argument itself. 9 10 oldguess = 0 11 # $oldguess is the previous $guess. 12 13 tolerance = .000001 14 # To how close a tolerance we wish to calculate. 15 16 loopcnt = 0 17 # Let's keep track of how many times through the loop. 18 # Some arguments will require more loop iterations than others. 19 20 21 while [ ABS( $guess $oldguess ) -gt $tolerance ] 22 # ^^^^^^^^^^^^^^^^^^^^^^^ Fix up syntax, of course. 23 24 # "ABS" is a (floating point) function to find the absolute value 25 #+ of the difference between the two terms. 26 # So, as long as difference between current and previous 27 #+ trial solution (guess) exceeds the tolerance, keep looping. 28 29 do 30 oldguess = $guess # Update $oldguess to previous $guess. 31 32 # ======================================================= 33 guess = ( $oldguess + ( $argument / $oldguess ) ) / 2.0 34 # = 1/2 ( ($oldguess **2 + $argument) / $oldguess ) 35 # equivalent to: 36 # = 1/2 ( $oldguess + $argument / $oldguess ) 37 # that is, "averaging out" the trial solution and 38 #+ the proportion of argument deviation 39 #+ (in effect, splitting the error in half). 40 # This converges on an accurate solution 41 #+ with surprisingly few loop iterations . . . 42 #+ for arguments > $tolerance, of course. 43 # ======================================================= 44 45 (( loopcnt++ )) # Update loop counter. 46 done</PRE></TD></TR></TABLE></P><P>It's a simple enough recipe, and <SPANCLASS="emphasis"><ICLASS="EMPHASIS">seems</I></SPAN> at first glance easy enough to convert into a working Bash script. The problem, though, is that Bash has <AHREF="operations.html#NOFLOATINGPOINT">no native support for floating point numbers</A>. So, the script writer needs to use <AHREF="mathc.html#BCREF">bc</A> or possibly <AHREF="awk.html#AWKREF">awk</A> to convert the numbers and do the calculations. It may get rather messy . . .</P></DD><DT><BCLASS="COMMAND">Logging File Accesses</B></DT><DD><P>Log all accesses to the files in <TTCLASS="FILENAME">/etc</TT> during the course of a single day. This information should include the filename, user name, and access time. If any alterations to the files take place, that should be flagged. Write this data as neatly formatted records in a logfile.</P></DD><DT><BCLASS="COMMAND">Monitoring Processes</B></DT><DD><P>Write a script to continually monitor all running processes and to keep track of how many child processes each parent spawns. If a process spawns more than five children, then the script sends an e-mail to the system administrator (or <ICLASS="FIRSTTERM">root</I>) with all relevant information, including the time, PID of the parent, PIDs of the children, etc. The script appends a report to a log file every ten minutes. </P></DD><DT><BCLASS="COMMAND">Strip Comments</B></DT><DD><P>Strip all comments from a shell script whose name is specified on the command line. Note that the <SPANCLASS="QUOTE">"#! line"</SPAN> must not be stripped out.</P></DD><DT><BCLASS="COMMAND">Strip HTML Tags</B></DT><DD><P>Strip all HTML tags from a specified HTML file, then reformat it into lines between 60 and 75 characters in length. Reset paragraph and block spacing, as appropriate, and convert HTML tables to their approximate text equivalent.</P></DD><DT><BCLASS="COMMAND">XML Conversion</B></DT><DD><P>Convert an XML file to both HTML and text format.</P></DD><DT><ANAME="CSPAMMERS"></A><BCLASS="COMMAND">Chasing Spammers</B></DT><DD><P> Write a script that analyzes a spam e-mail by doing DNS lookups on the IP addresses in the headers to identify the relay hosts as well as the originating ISP. The script will forward the unaltered spam message to the responsible ISPs. Of course, it will be necessary to filter out <SPANCLASS="emphasis"><ICLASS="EMPHASIS">your own ISP's IP address</I
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -