See Section A.10, "Answers to Chapter 11 Exercises" for answers to the following exercises:
[20] Make a program which asks the user for a source file name, a destination file name, a search pattern, and a replacement string. (Be sure to ask the user interactively for these; don't get them from the command-line arguments.) Your program should read the source file and write it out as the destination file, replacing the search pattern with the replacement string wherever it appears. That is, the destination file will be a modified duplicate of the source file. Can you overwrite an existing file (not the same as the input file)? Can you use regular expression metacharacters in the search pattern? (That is, can you enter (fred|wilma) flintstone to search for either name?) Can you use the memory variables and backslash escapes in the replacement string? (That is, can you use \u\L$1\E Flintstone as the replacement string to properly capitalize the names of Fred and Wilma?) Don't worry if you can't accomplish each of these things; it's more important simply to see what happens when you try.
[15] Make a program which takes a list of files named on the command line and reports for each one whether it's readable, writable, executable, or doesn't exist. (Hint: It may be helpful to have a function which will do all of the file tests for one file at a time.) What does it report about a file which has been chmod'ed to 0? (That is, if you're on a Unix system, use the command chmod 0 some_file to mark that file as neither being readable, writable, nor executable.) In most shells, use a star as the argument to mean all of the normal files in the current directory. That is, you could type something like ./ex11-2 * to ask the program for the attributes of many files at once.
[10] Make a program to identify the oldest file named on the command line and report its age in days. What does it do if the list is empty? (That is, if no files are mentioned on the command line.)
Copyright © 2002 O'Reilly & Associates. All rights reserved.