Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 69 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
69
Dung lượng
1,36 MB
Nội dung
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 '^' '\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 "^$line$" $PHONEBOOK > /tmp/phonebook$$ { tr '\012' '^' < /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 ^ 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. The phone book file is then sorted, and the temporary files removed. listall # # list all of the entries in the phone book # IFS='^' # to be used in set command below echo " " while read line do # # Get the first and last fields, presumably names and numbers # set $line # # display 1st and last fields (in reverse order!) # eval printf "\"%-40.40s %s\\n\"" "\"$1\"" "\"\${$#}\"" done < $PHONEBOOK echo " " The listall program lists all entries in the phone book, printing just the first and last lines of each entry. The internal field separator characters (IFS) is set to a ^, to be used later inside the loop. Each line from the phone book file is then read and assigned to the variable line. The set command is used to assign each field to the positional parameters. The trick now is to get the value of the first and last positional parameters because that's what we want to display. The first one is easy because it can be directly referenced as $1. To get the last one, you use eval as you saw in Chapter 13, "Loose Ends." The command eval echo \${$#} has the effect of displaying the value of the last positional parameter. The command eval printf "\"%-40.40s %-s\\n\"" "\"$1\"" "\"\${$#}\"" gets evaluated to printf "%-40.40s %-s\n" "Steve's Ice Cream" "${4}" using the entry shown previously as the example, and then the shell rescans the line to substitute the value of ${4} before executing printf. Sample Output Now it's time to see how rolo works. We'll start with an empty phone book and add a few entries to it. Then we'll list all the entries, look up a particular one, and change one (using the default editor ed—remember that the variable EDITOR can always be set to a different editor and then exported). To conserve space, we'll show only the full menu that rolo displays the first time. $ PHONEBOOK=/users/steve/misc/book $ export PHONEBOOK $ rolo Start it up /users/steve/misc/book does not exist! Should I create it for you (y/n)? y Would you like to: 1. Look someone up 2. Add someone to the phone book 3. Remove someone from the phone book 4. Change an entry in the phone book 5. List all names and numbers in the phone book 6. Exit this program Please select one of the above (1-6): 2 Type in your new entry When you're done, type just a single Enter on the line. >> Steve's Ice Cream >> 444 6th Avenue >> New York City 10003 >> 212-555-3021 >> Steve's Ice Cream has been added to the phone book Would you like to: Please select one of the above (1-6): 2 Type in your new entry When you're done, type just a single Enter on the line. >> YMCA >> 973-555-2344 >> YMCA has been added to the phone book Would you like to: Please select one of the above (1-6): 2 Type in your new entry When you're done, type just a single Enter on the line. >> Maureen Connelly >> Hayden Book Companu >> 10 Mulholland Drive >> Hasbrouck Heights, N.J. 07604 >> 201-555-6000 >> Maureen Connelly has been added to the phone book Would you like to: Please select one of the above (1-6): 2 Type in your new entry When you're done, type just a single Enter on the line. >> Teri Zak >> Hayden Book Company >> (see Maureen Connelly for address) >> 201-555-6060 >> Teri Zak has been added to the phone book Would you like to: Please select one of the above (1-6): 5 Maureen Connelly 201-555-6000 Steve's Ice Cream 212-555-3021 Teri Zak 201-555-6060 YMCA 973-555-2344 Would you like to: Please select one of the above (1-6): 1 Enter name to look up: Maureen | Maureen Connelly | | Hayden Book Companu | | 10 Mulholland Drive | | Hasbrouck Heights, NJ 07604 | | 201-555-6000 | | o o | | Teri Zak | | Hayden Book Company | | (see Maureen Connelly for address)| | 201-555-6060 | | | | o o | Would you like to: Please select one of the above (1-6): 4 Enter name to change: Maureen | Maureen Connelly | | Hayden Book Companu | | 10 Mulholland Drive | | Hasbrouck Heights, NJ 07604 | | 201-555-6000 | | o o | Change this person (y/n)? y Enter changes with /bin/ed 101 1,$p Maureen Connelly [...]... POSIX shell standard These features are available in Bash and the Korn shell, the two most commonly available POSIX-compliant shells The Korn shell was developed by David Korn of AT&T Bell Laboratories It was designed to be "upward compatible" with the System V Bourne shell and the POSIX standard shell It is available in the standard Unix distributions from Sun, HP, and IBM, and is the default shell. .. for Bourne-Again Shell) was developed by Brian Fox for the Free Software Foundation It was also designed to be upward compatible with the System V Bourne shell and the POSIX standard shell, and also contains many extensions from the Korn and C shells Bash is the standard shell on Linux systems Except for a few minor differences, Bash and the Korn shell provide all the POSIX standard shell' s features,... determine what shell to start up when you execute a shell escape In such cases, you want to make sure that each time you start up a new shell, you get the shell you want and not an older Bourne shell Command-Line Editing Line edit mode is a feature of the shell that allows you to edit a command line using built-in commands that mimic those found in two popular screen editors The POSIX standard shell provides... noninteractive Bash shell is started (for example, when you run a shell program), it reads commands from the file specified by the BASH_ENV environment variable, and when an interactive Bash shell is started (for example, by typing bash at the command prompt), it doesn't You should also set and export inside your profile file a variable called SHELL $ cat profile SHELL= /usr/bin/ksh export SHELL $ This... give you an idea of the compatibility of these shells with the POSIX standard, all shell programs in the previous chapters work under both Bash and the Korn shell We'll note any nonstandard features that we discuss in this chapter, and Table 15.4 at the end of this chapter lists the features supported by the different shells Getting the Right Shell Most shells follow a convention that allows you to... operation of the history command differs between the Korn shell and Bash because it is not part of the POSIX standard The Korn shell history command writes your last 16 commands to standard output: $ history 507 cd shell 5 08 cd ch15 509 vi int 510 ps 511 echo $HISTSIZE 512 cat $ENV 513 cp int int.sv 514 history 515 exit 516 cd shell 517 cd ch16 5 18 vi all 519 run -n5 all 520 ps 521 lpr all.out 522 history... exported in your profile file Naturally, there is a limit to the number of commands the shell records The default value of this limit varies by implementation, but the POSIX standard requires it to be at least 1 28; the default value for the Korn shell is 1 28; the default value for Bash is 500 Each time you log in, the shell automatically truncates your history file to this length You can control the size... however, because many programs, such as Perl, don't reside in a standard place on every Unix system Also, this is not a feature specified by the POSIX standard, even though it's found in every modern shell we've seen and is even implemented at the operating system level on many Unix versions The ENV File When you start the shell, one of the first things it does is look in your environment for a variable... to develop larger shell programs, and how to use the many different programming tools provided by the system Other than the shell built-ins, rolo relies on tr, grep, an editor, sort, and the standard file system commands such as mv and rm to get the job done The simplicity and elegance that enable you to easily tie all these tools together account for the deserved popularity of the Unix system See Appendix... of the Unix system See Appendix B for more information on downloading the rolo programs Chapter 15, "Interactive and Nonstandard Shell Features," introduces you to interactive features of the shell and two shells that have some nice features not found in the POSIX standard shell Exercises 1: Modify rolo so that upper- and lowercase letters are not distinguished when doing a lookup in the phone book . Bourne shell and the POSIX standard shell. It is available in the standard Unix distributions from Sun, HP, and IBM, and is the default shell on MIPS workstations. Bash (short for Bourne-Again Shell) . variable called SHELL. $ cat .profile SHELL= /usr/bin/ksh export SHELL $ This variable is used by certain applications (such as vi) to determine what shell to start up when you execute a shell escape the Korn shell provide all the POSIX standard shell& apos;s features, as well as many new ones. To give you an idea of the compatibility of these shells with the POSIX standard, all shell programs