[ Pobierz całość w formacie PDF ]
.34suser 0m35.50ssys 0m52.13sListing 2.17 Timing data for each loop method.(continues) 74 Chapter 2Method 2:function while_read_LINE_bottomreal 0m5.89suser 0m5.62ssys 0m0.16sMethod 3:function while_line_LINE_bottomreal 6m53.71suser 0m36.62ssys 6m2.03sMethod 4:function cat_while_LINE_linereal 7m16.87suser 0m51.87ssys 6m8.54sMethod 5:function while_line_LINEreal 6m50.79suser 0m36.65ssys 5m59.66sMethod 6:function while_LINE_line_bottomreal 7m20.48suser 0m51.01ssys 6m14.57sMethod 7:function while_LINE_line_cmdsub2real 7m18.04suser 0m52.01ssys 6m10.94sMethod 8:function while_LINE_line_bottom_cmdsub2real 7m20.34sListing 2.17 Timing data for each loop method.(continued) Twelve Ways to Process a File Line by Line 75user 0m50.82ssys 6m14.26sMethod 9:function while_read_LINE_FDreal 0m5.89suser 0m5.53ssys 0m0.28sMethod 10:function while_LINE_line_FDreal 8m25.35suser 0m50.68ssys 7m15.33sMethod 11:function while_LINE_line_cmdsub2_FDreal 8m24.58suser 0m50.04ssys 7m16.07sMethod 12:function while_line_LINE_FDreal 7m54.57suser 0m35.88ssys 7m2.26sListing 2.17 Timing data for each loop method.(continued)As you can see, all file processing loops are not created equal.Two of the methodsare tied for first place.Methods 2 and 9 produce the exact same realexecution time at5.89 seconds to process a 7,500-line file.Method 1 came in second at 1 minute and 30.34seconds.The remaining methods fall far behind, ranging from almost 7 minutes toover 8 minutes and 25.35 seconds.The sorted timing output for the real time is shownin Listing 2.18.real 0m5.89s Method 2real 0m5.89s Method 9real 1m30.34s Method 1Listing 2.18 Sorted timing data by method.(continues) 76 Chapter 2real 6m50.79s Method 5real 6m53.71s Method 3real 7m16.87s Method 4real 7m18.04s Method 7real 7m20.34s Method 8real 7m20.48s Method 6real 7m54.57s Method 12real 8m24.58s Method 11real 8m25.35s Method 10Listing 2.18 Sorted timing data by method.(continued)Let s take a look at the code for the top three techniques.The order of appearance isMethod 2, 9, and 1.function while_read_LINE_bottom{while read LINEdoecho  $LINE:done $OUTFILEfidone > $OUTFILEfielif [ $FSVALUE -gt $FSMAX ] # Not in $EXCEPTIONS filethenListing 5.3 The wrong way to use grep. File System Monitoring 105echo  $FSDEVICE mount on $FSMOUNT is ${FSVALUE}% \>> $OUTFILEfifielse # No exceptions file.use script defaultif [ $FSVALUE -gt $FSMAX ]thenecho  $FSDEVICE mount on $FSMOUNT is ${FSVALUE}% \>> $OUTFILEfifidone $DATA_EXCEPTIONS}In the preceding function we use the ^, caret character, along with the grep -v toignore any line beginning with a #, pound sign.We also use the ^$with the sed state-ment to remove any blank lines and then redirect output to a data file, which is pointedto by the $DATA_EXCEPTIONSvariable.After we have the exceptions file data loaded,we have the following check_exceptions function that will look in the$DATA_EXCEPTIONS file for the current mount point and, if found, will check the$NEW_MAX value to the system s reported percent used value.The function willpresent back to the script a return code relating to the result of the test.function check_exceptions{# set -x # Uncomment to debug this function 106 Chapter 5while read FSNAME NEW_MAX # Feeding data from Bottom of Loop!!!doif [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?then # Get rid of the % sign, if it exists!NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)if [ $FSVALUE -gt $NEW_MAX ]then # Over Limit.Return a  0 , zeroreturn 0 # FOUND OVER LIMIT - Return 0else # Found in the file but is within limitsreturn 2 # Found OKfifidone $WORKFILE # Initialize to emptyOUTFILE= /tmp/df.outfile # Output display file>$OUTFILE # Initialize to emptyBINDIR= /usr/local/bin # Local bin directoryTHISHOST=`hostname` # Hostname of this machineEXCEPTIONS= ${BINDIR}/exceptions # Overrides $FSMAXDATA_EXCEPTIONS= /tmp/dfdata.out # Exceptions file w/o #, comments####### DEFINE FUNCTIONS HERE #####function load_EXCEPTIONS_file{# Ignore any line that begins with a pound sign, ## and omit all blank linescat $EXCEPTIONS | grep -v  ^# | sed /^$/d > $DATA_EXCEPTIONS}###################################function check_exceptions{# set -x # Uncomment to debug this functionwhile read FSNAME NEW_MAX # Feeding data from Bottom of Loop!!!doif [[ $FSNAME = $FSMOUNT ]] # Correct /mount_point?then # Get rid of the % sign, if it exists!NEW_MAX=$(echo $NEW_MAX | sed s/\%//g)if [ $FSVALUE -gt $NEW_MAX ]then # Over Limit [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • blondiii.htw.pl
  •