[ Pobierz całość w formacie PDF ]
.For beginners, the word  function helps under-standing the whole shell script a lot.The $FILENAMEvariable is set in the main bodyof the shell script.Within the while loop notice that I added the no-op (:) after the echostatement.A no-op (:) does nothing, but it always has a 0, zero, return code.I use theno-op only as a placeholder so that you can cut the function code out and paste it in oneof your scripts.If you should remove the echo statement and leave the no-op, thewhile loop will not fail; however, the loop will not do anything either.Method 2: while read $FILENAME from BottomYou are now entering one of my favorite methods of parsing through a file.We still usethe while read LINE syntax, but this time we feed the loop from the bottom instead ofusing a pipe.You will find that this is one of the fastest ways to process each line of afile.The first time you see this it looks a little unusual, but it works very well.Look at the code in Listing 2.4, and we will go over the function at the end.function while_read_LINE_bottom{while read LINEdoecho  $LINE:done > $TIMEFILEecho  \nMethod 9:echo  \nfunction while_read_LINE_FD\n >> $TIMEFILEecho  function while_read_LINE_FDtime while_read_LINE_FD >> $TIMEFILEecho  \nMethod 10:echo  \nfunction while_LINE_line_FD\n >> $TIMEFILEecho  function while_LINE_line_FDtime while_LINE_line_FD >> $TIMEFILEecho  \nMethod 11:echo  \nfunction while_LINE_line_cmdsub2_FD\n >> $TIMEFILEecho  function while_LINE_line_cmdsub2_FDtime while_LINE_line_cmdsub2_FD >> $TIMEFILEecho  \nMethod 12:echo  \nfunction while_line_LINE_FD\n >> $TIMEFILEecho  function while_line_LINE_FDtime while_line_LINE_FD >> $TIMEFILEListing 2.15 12_ways_to_parse.ksh shell script listing.(continued)The shell script in Listing 2.15 first defines all of the functions that we previouslycovered in the Methods sections.After the functions are defined, we do a little testingof the input.We are expecting exactly one command parameter, and it should be aregular file.Look at the following code block in Listing 2.16 to see the file testing.# Test the Input# Looking for exactly one parameter(( $# == 1 )) || usage# Does the file exist as a regular file?[[ -f $1 ]] || usageListing 2.16 Code to test command input. Twelve Ways to Process a File Line by Line 73The first test checks to ensure that the number of command parameters, specified bythe $#operator, is exactly one.Notice that we used the double parentheses mathematicaltest, specified as (( math test )).Additionally, we used a logical OR, specified by||, to execute the usagefunction if the number of parameters is not equal to one.We use the same type of test for the file to ensure that the file exists and the file is aregular file, as opposed to a character or block special file.When we do the test, noticethat we used the double bracket test for character data, specified by [[ charactertest ]].This is an important distinction to note.We again use the logical OR to exe-cute the usagefunction if the return code from the test is nonzero.Now we start the actual timing tests.In doing these tests we execute the Methodfunctions one at a time.The function s internal while loop does the file processing, butwe redirect each function s output to a file so that we have some measurable systemactivity.As I stated before, the timing measurements produced by the time commandsgo to stderr, or file descriptor 2, which will just go to the screen by default.When thisshell script executes, there are three things that go to the screen, as you will see in List-ing 2.17.You can also send all of this output to a file by using the following commandsyntax:12_ways_to_parse.ksh /scripts/bigfile > /tmp/timing_data.out 2>&1The previous command starts with the script name, followed by the file to parsethrough.The output is redirected to the file /tmp/timing_data.outwith stderr(file descriptor 2) redirected to stdout (file descriptor 1), specified by 2>&1.Do notforget the ampersand, &, before the 1.If the &is omitted, a file with the name 1will becreated.This is a common mistake when working with file descriptors.The placementof the stderrto stdoutis important in this case.If the 2>&1is at the end of the com-mand, you will not get the desired result, which is all of the timing data going to a datafile.In some cases the placement of the 2>&1 redirection does not matter, but it doesmatter here.Timing Data for Each MethodNow all of the hard stuff has been done.We have a 7,500-line file, /scripts/bigfile, and we have our shell script written, so let s look at which function is thefastest in Listing 2.17 [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • matkasanepid.xlx.pl