tailieunhanh - Unix Shell Programming Third Edition phần 8
Chương trình thay đổi cho phép người dùng chỉnh sửa một mục trong danh bạ điện thoại. Mã ban đầu là hầu như giống hệt với rem: nó tìm thấy các mục phù hợp và sau đó nhắc nhở người dùng lựa chọn được thay đổi. Các mục được chọn sau đó bằng văn bản vào các tập tin tạm thời / tmp / ed $ $, với những nhân vật ^ dịch sang dòng mới. | display line echo Change this entry y n c read answer dev tty if answer y then break fi done tmp matches rm tmp matches if answer y then exit fi Start up editor on the confirmed entry echo line c tr A 012 tmp ed echo Enter changes with EDITOR bin ed trap 2 don t abort if DELETE hit while editing EDITOR tmp ed Remove old entry now and insert new one grep -v A line PHONEBOOK tmp phonebook tr 012 A tmp ed echo tmp phonebook last echo was to put back trailing newline translated by tr sort tmp phonebook -o PHONEBOOK rm tmp ed tmp phonebook The change program allows the user to edit an entry in the phone book. The initial code is virtually identical to rem it finds the matching entries and then prompts the user to select the one to be changed. The selected entry is then written into the temporary file tmp ed with the A characters translated to newlines. This unfolds the entry into separate lines for convenient editing. The program then displays the message echo Enter changes with EDITOR bin ed which serves a dual purpose It tells the user what editor will be used to make the change while at the same time setting the variable EDITOR to bin ed if it s not already set. This technique allows the user to use his or her preferred editor by simply assigning its name to the variable EDITOR and exporting it before executing rolo EDITOR vi export EDITOR rolo The signal generated by the Delete key 2 is ignored so that if the user presses this key while in the editor the change program won t abort. The editor is then started to allow the user to edit the entry. After the user makes his changes writes the file and quits the editor control is given back to change. The old entry is then removed from the phone book with grep and the modified entry is converted into the special internal format with tr and tacked onto the end. An extra newline character must be added here to make sure that a real newline is stored in the file after the entry. This is done with an echo with no arguments. .
đang nạp các trang xem trước