1. Trang chủ
  2. » Công Nghệ Thông Tin

Hệ Điều Hành Linux (P12) pptx

30 272 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 1,64 MB

Nội dung

< Day Day Up > Page 331 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 332 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Advanced Exercises 13. Write a script that takes a colon-separated list of items and outputs the items, one per line, to standard output (without the colons). 14. Generalize the script written in exercise 13 so that the character separating the list items is given as an argument to the function. If this argument is absent, the separator should default to a colon. 15. Write a function named funload that takes as its single argument the name of a file containing other functions. The purpose of funload is to make all functions in the named file available in the current shell; that is, funload loads the functions from the named file. To locate the file, funload searches the colon-separated list of directories given by the environment variable FUNPATH. Assume that the format of FUNPATH is the same as PATH and that searching FUNPATH is similar to the shell's search of the PATH variable. 16. Rewrite bundle (page 469) so that the script it creates takes an optional list of filenames as arguments. If one or more filenames are given on the command line, only those files should be re-created; otherwise, all files in the shell archive should be re-created. For example, suppose that all files with the filename extension .c are bundled into an archive named srcshell, and you want to unbundle just the files test1.c and test2.c. The following command will unbundle just these two files: $ bash srcshell test1.c test2.c 17. What kind of links will the lnks script (page 445) not find? Why? 18. In principle, recursion is never necessary. It can always be replaced by an iterative construct, such as while or until. Rewrite makepath (page 511) as a nonrecursive function. Which version do you prefer? Why? 19. Lists are commonly stored in environment variables by putting a colon (:) between each of the list elements. (Th e value of the PATH variable is a good example.) You can add an element to such a list by catenating the new element to the front of the list, as in PATH=/opt/bin:$PATH If the element you add is already in the list, you now have two copies of it in the list. Write a shell function named addenv that takes two arguments: (1) the name of a shell variable and (2) a string to prepend to the list that is the value of the shell variable only if that string is not already an element of the list. For example, the call addenv PATH /opt/bin would add /opt/bin to PATH only if that pathname is not already in PATH. Be sure that your solution works even if the shell variable starts out empty. Also make sure that you check the list elements carefully. If /usr/opt/bin is in PATH but /opt/bin is not, the example just given should still add /opt/bin to PATH. (Hint: You may find this exercise easier to complete if you first write a function locate_field that tells you whether a string is an element in the value of a variable.) 20. Write a function that takes a directory name as an argument and writes to standard output the maximum of the lengths of all filenames in that directory. If the function's argument is not a directory name, write an error message to standard output and exit with nonzero status. 21. Modify the function you wrote for exercise 20 to descend all subdirectories of the named directory recursively and to find the maximum length of any filename in that hierarchy. 22. Write a function that lists the number of regular files, directories, block special files, character special files, FIFOs, and symbolic links in the working directory. Do this in two different ways: a. a. Use the first letter of the output of ls –l to determine a file's type. b. b. Use the file type condition tests of the [[ expression ]] syntax to determine a file's type. 23. Modify the quiz program (page 518) so that the choices for a question are randomly arranged. Page 333 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 334 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 335 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Chapter 12. The gawk Pattern Processing Language IN THIS CHAPTER Syntax 528 Arguments 528 Options 529 Patterns 530 Actions 531 Variables 531 Functions 532 Associative Arrays 534 Control Structures 535 Examples 537 getline: Controlling Input 554 Coprocess: Two-Way I/O 557 Getting Input from a Network 558 Error Messages 559 The gawk (GNU awk) utility is a pattern-scanning and processing language that searches one or more files to see whether they contain records (usually lines) that match specified patterns. It processes lines by performing actions, such as writing the record to standard output or incrementing a counter, each time it finds a match. As opposed to procedural languages, the gawk language is data driven: You describe the data you want to work with and tell gawk what to do with the data once it finds it. You can use gawk to generate reports or filter text. It works equally well with numbers and text; when you mix the two, gawk usually comes up with the right answer. The authors of awk (Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan), on which gawk is based, designed the original utility to be easy to use. To achieve this end they sacrificed execution speed. The gawk utility takes many of its constructs from the C programming language. It includes the following features:   Flexible format   Conditional execution   Looping statements   Numeric variables   String variables   Regular expressions   Relational expressions   C's printf   Coprocess execution   Network data exchange Page 336 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 337 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Syntax A gawk command line has the following syntax: gawk [options] [program] [file-list] gawk [options] – f program-file [file-list] The gawk utility takes its input from files you specify on the command line or from standard input. An advanced command, getline, gives you more choices about where input comes from and how you read it. Using a coprocess, gawk can interact with another program or exchange data over a network. Unless you redirect output from gawk, it goes to standard output. < Day Day Up > Page 338 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Arguments In the preceding syntax, program is a gawk program that you include on the command line. The program-file is the name of the file that holds a gawk program. Putting the program on the command line allows you to write short gawk programs without having to create a separate program-file. To prevent the shell from interpreting the gawk commands as shell commands, enclose the program within single quotation marks. Putting a long or complex program in a file can reduce errors and retyping. The file-list contains pathnames of the ordinary files that gawk processes. These files are the input files. When you do not specify a file-list, gawk takes input from standard input or as specified by getline (page 554) or a coprocess (page 557). < Day Day Up > Page 339 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 340 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html [...]... http://www.processtext.com/abcchm.html < Day Day Up > Page 342 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Notes The gawk utility is the GNU version of UNIX awk For convenience many Linux systems provide a link from /bin/awk to /bin/gawk so that you can run the program using either name See page 554 for advanced gawk commands and page 559 for examples of gawk error messages < Day . < Day Day Up > Notes The gawk utility is the GNU version of UNIX awk. For convenience many Linux systems provide a link from /bin/awk to /bin/gawk so that you can run the program using either

Ngày đăng: 07/07/2014, 09:20

TỪ KHÓA LIÊN QUAN