Unix Shell Programming Third Edition phần 6 ppt

69 453 0
Unix Shell Programming Third Edition phần 6 ppt

Đ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

do printf "%12d %12d\n" $number1 $number2 done $ cat data 1234 7960 593 -595 395 304 3234 999 -394 -493 $ align data 1234 7960 593 -595 395 304 3234 999 -394 -493 $ In Chapters 12, 14, and 15 you'll see more uses for printf. But first try your hand at the following exercises. Exercises 1: What happens to mycp if one or more of the files to be copied doesn't exist? Can you make any suggestions to better handle the situation? 2: What happens to mycp if one of the filenames contains a character that has a special meaning to the shell such as ; or |? 3: Write a program called mymv that does with the mv command what mycp does with the cp command. How many changes did you have to make to mycp to produce this new program? 4: Modify mycp to prompt for arguments if none are supplied. A typical execution of the modified version should look like this: $ mycp Source file name? voucher Destination file name? voucher.sv $ Make sure that the program allows one or both of the files to be specified with filename substitution characters. 5: Add a -n option to mycp that suppresses the normal check for the existence of the destination files. 6: Modify mycp to use sed instead of the while loop to process the arguments typed on the command line. 7: Modify the rem program used by rolo so that if multiple entries are found, the program will prompt the user for the entry to be removed. Here's a sample session: $ rolo Please select one of the above (1-3): 3 Enter name to be removed: Susan More than one match; please select the one to remove: Susan Goldberg Remove (y/n)? n Susan Topple Remove (y/n)? y $ 8: Modify rolo so that the menu is redisplayed after each selection is made and processed. To allow the user to get out of this, add another selection to the menu to exit from the program. 9: What happens to the rolo program if just an Enter is given as the name for the add, look up, or remove options? 10: Modify lu to use printf to print the name and phone number so that they line up in columns for names up to 40 characters in length (Hint: use cut –f and the fact that the fields in the phonebook are separated by tabs). Chapter 11. Your Environment IN THIS CHAPTER Local Variables Exported Variables PS1 and PS2 HOME, James Your PATH Your Current Directory More on Subshells Your .profile File The TERM Variable The TZ Variable Exercises When you log on to the system, you're effectively given your own copy of the shell program. This shell maintains what's known as your environment—an environment that is distinct from other users on the system. This environment is maintained from the moment you log on until the moment you log off. In this chapter you'll learn about this environment in detail, and you'll see how it relates to writing and running programs. Local Variables Type the following program called vartest into your computer: $ cat vartest echo :$x: $ vartest consists of a solitary echo command that displays the value of the variable x, surrounded by colons. Now assign any value you want to the variable x from your terminal: $ x=100 Here we chose 100. Question: What do you think will be displayed when vartest is now executed? Answer: $ vartest :: $ vartest doesn't know about the value of x. Therefore, its value is null. The variable x that was assigned the value 100 in the login shell is known as a local variable. The reason why it has this name will become clear shortly. Here's another example. This program is called vartest2: $ cat vartest2 x=50 echo :$x: $ x=100 $ vartest2 Execute it :50: $ Now the question is: What's the value of x? $ echo $x 100 $ So you see that vartest2 didn't change the value of x that you set equal to 100 in your login shell. Subshells The behavior exhibited by vartest and vartest2 is due to the fact that these two programs are run as subshells by your login shell. A subshell is, for all intents and purposes, an entirely new shell executed by your login shell to run the desired program. So when you ask your login shell to execute vartest, it starts up a new shell to execute the program. Whenever a new shell runs, it runs in its own environment, with its own set of local variables. A subshell has no knowledge of local variables that were assigned values by the login shell (the "parent" shell). Furthermore, a subshell cannot change the value of a variable in the parent shell, as evidenced by vartest2. Let's review the process that goes on here. Before executing vartest2, your login shell has a variable called x that has been assigned the value 100 (assume for now that this is the only variable defined in the shell). This is depicted in Figure 11.1. Figure 11.1. Login shell with x=100. When you ask to have vartest2 executed, your login shell starts up a subshell to run it, giving it an empty list of local variables to start with (see Figure 11.2). Figure 11.2. Login shell executes vartest2. After the first command in vartest2 is executed (that assigns 50 to x), the local variable x that exists in the subshell's environment will have the value 50 (see Figure 11.3). Note that this has no relation whatsoever to the variable x that still maintains its value of 100 in the login shell. Figure 11.3. vartest2 executes x=50. When vartest2 finishes execution, the subshell goes away, together with any variables assigned values. Exported Variables There is a way to make the value of a variable known to a subshell, and that's by exporting it with the export command. The format of this command is simply export variables where variables is the list of variable names that you want exported. For any subshells that get executed from that point on, the value of the exported variables will be passed down to the subshell. Here's a program called vartest3 to help illustrate the difference between local and exported variables: $ cat vartest3 echo x = $x echo y = $y $ Assign values to the variables x and y in the login shell, and then run vartest3: $ x=100 $ y=10 $ vartest3 x = y = $ x and y are both local variables, so their values aren't passed down to the subshell that runs vartest3. Now let's export the variable y and try it again: $ export y Make y known to subshells $ vartest3 x = y = 10 $ This time, vartest3 knew about y because it is an exported variable. Conceptually, whenever a subshell is executed, the list of exported variables gets "copied down" to the subshell, whereas the list of local variables does not (see Figure 11.4). Figure 11.4. Execution of vartest3. Now it's time for another question: What do you think happens if a subshell changes the value of an exported variable? Will the parent shell know about it after the subshell has finished? To answer this question, here's a program called vartest4: $ cat vartest4 x=50 y=5 $ [...]... vartest4 In the case of a subshell executing another subshell (for example, the rolo program executing the lu program), the process is repeated: The exported variables from the subshell are copied to the new subshell These exported variables may have been exported from above, or newly exported from within the subshell After a variable is exported, it remains exported to all subshells subsequently executed... Figure 11 .6 Figure 11 .6 Subshell execution To summarize the way local and exported variables work: 1 Any variable that is not exported is a local variable whose existence will not be known to subshells 2 Exported variables and their values are copied into a subshell's environment, where they may be accessed and changed However, such changes have no effect on the variables in the parent shell 3 Exported... for directly spawned subshells, but also for subshells spawned by those subshells (and so on down the line) 4 A variable can be exported any time before or after it is assigned a value export -p If you simply type export -p, you'll get a list of the variables and their values exported by your shell: $ export –p export LOGNAME=steve export PATH=/bin:/usr/bin: export TIMEOUT =60 0 export TZ=EST5EDT export... your current directory Because the current directory is part of the environment, when a cd is executed from a subshell, the current directory of that subshell is altered There is no way to change the current directory of a parent shell from a subshell When cd is invoked, it sets the PWD shell variable to the full pathname of the new current directory, so the command echo $PWD produces the same output... gets executed, the exported variable y will be copied into the subshell's environment vartest4 sets the value of x to 50, changes the value of y to 5, and sets the value of z to 1 Then it exports z This makes the value of z accessible to any subshell subsequently run by vartest4 vartest5 is such a subshell, and when it is executed, the shell copies into its environment the exported variables from vartest4:... parent shell PS1 and PS2 The characters that the shell displays as your command prompt are stored in the variable PS1 You can change this variable to be anything you want As soon as you change it, it'll be used by the shell from that point on $ echo :$PS1: :$ : $ PS1="==> " ==> pwd /users/steve ==> PS1="I await your next command, master: " I await your next command, master: date Wed Sep 18 14: 46: 28... name of a program, say for example rolo, the shell searches the directories listed in PATH from left to right until it finds an executable file called rolo First it looks in /bin, then in /usr/bin, and finally in the current directory for an executable file called rolo As soon as it finds rolo, the shell executes it; if the shell doesn't find rolo, the shell issues a "not found" message The path /bin:.:/usr/bin... $y 100 10 $ So the subshell couldn't even change the value of the exported variable y; it merely changed the copy of y that was passed to its environment when it was executed (see Figure 11.5) Just as with local variables, when a subshell goes away, so do the values of the exported variables There is no way to change the value of a variable in a parent shell from within a subshell Figure 11.5 Execution... subdirectory x) If the current directory isn't listed first, you may end up in an unexpected directory More on Subshells It's important for you to understand the way subshells work and how they interact with your environment You know now that a subshell can't change the value of a variable in a parent shell, nor can it change its current directory Suppose that you want to write a program to set values for... directory Incidentally, cd is a shell built-in command CDPATH The CDPATH variable works like the PATH variable: It specifies a list of directories to be searched by the shell whenever you execute a cd command This search is done only if the specified directory is not given by a full pathname and if CDPATH is not null (obviously) So if you type in cd /users/steve the shell changes your directory directly . 100 in your login shell. Subshells The behavior exhibited by vartest and vartest2 is due to the fact that these two programs are run as subshells by your login shell. A subshell is, for all intents. entirely new shell executed by your login shell to run the desired program. So when you ask your login shell to execute vartest, it starts up a new shell to execute the program. Whenever a new shell. set of local variables. A subshell has no knowledge of local variables that were assigned values by the login shell (the "parent" shell) . Furthermore, a subshell cannot change the value

Ngày đăng: 13/08/2014, 15:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan