tailieunhanh - Unix Shell Programming Third Edition phần 4

Chúng ta hãy tiếp tục với sự phát triển của các chương trình làm việc với các tập tin danh bạ. Bạn có thể sẽ muốn thêm một người nào đó vào tập tin, đặc biệt là bởi vì tập tin danh bạ của chúng tôi là quá nhỏ. Bạn có thể viết một chương trình được gọi là thêm hai tham số: tên của người được bổ sung và số lượng. | Team LiB PREVIOUS A Program to Add Someone to the Phone Book Let s continue with the development of programs that work with the phonebook file. You ll probably want to add someone to the file particularly because our phonebook file is so small. You can write a program called add that takes two arguments the name of the person to be added and the number. Then you can simply write the name and number separated from each other by a tab character onto the end of the phonebook file cat add Add someone to the phone book echo 1 2 phonebook Although you can t tell there s a tab character that separates the 1 from the 2 in the preceding echo command. This tab must be quoted to make it to echo without getting gobbled up by the shell. Let s try out the program add Stromboli Pizza 973-555-9478 lu Pizza See if we can find the new entry Stromboli Pizza 973-555-9478 So far so good cat phonebook See what happened Alice Chebba 973-555-2015 Barbara Swingle 201-555-9257 Liz Stachiw 212-555-2298 Susan Goldberg 201-555-7776 Susan Topple 212-555-4932 Tony lannino 973-555-1295 Stromboli Pizza 973-555-9478 Stromboli Pizza was quoted so that the shell passed it along to add as a single argument what would have happened if it wasn t quoted . After add finished executing lu was run to see whether it could find the new entry and it did. The cat command was executed to see what the modified phonebook file looked like. The new entry was added to the end as intended. Unfortunately the new file is no longer sorted. This won t affect the operation of the lu program but you can add a sort to the add program to keep the file sorted after new entries are added cat add Add someone to the phonebook file -- version 2 echo 1 2 phonebook sort -o phonebook phonebook Recall that the -o option to sort specifies where the sorted output is to be written and that this can be the same as the input file add Billy Bach 201-555-7618 cat phonebook Alice Chebba 973-555-2015 Barbara Swingle 201-555-9257 Billy Bach .

TÀI LIỆU MỚI ĐĂNG