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

Unix Shell Programming Third Edition phần 6 ppt

69 454 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 69
Dung lượng 1,36 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 pare

Trang 2

Source file name? voucher

Destination file name? voucher.sv

Trang 3

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 theprogram

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 incolumns for names up to 40 characters in length (Hint: use cut –f and the fact that thefields in the phonebook are separated by tabs)

Trang 4

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 youlog off In this chapter you'll learn about this environment in detail, and you'll see how it relates towriting and running programs

Trang 5

Here's another example This program is called vartest2:

$ cat vartest2

x=50

echo :$x:

Trang 6

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 executevartest, 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 variabledefined in the shell) This is depicted in Figure 11.1

Figure 11.1 Login shell with x=100.

Trang 7

When you ask to have vartest2 executed, your login shell starts up a subshell to run it, giving it anempty 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 norelation whatsoever to the variable x that still maintains its value of 100 in the login shell

Figure 11.3 vartest2 executes x=50.

Trang 8

When vartest2 finishes execution, the subshell goes away, together with any variables assigned

values.

Trang 9

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

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:

Trang 10

$ export y Make y known to subshells

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 anexported variable? Will the parent shell know about it after the subshell has finished? To answer thisquestion, here's a program called vartest4:

$ cat vartest4

x=50

y=5

$

Trang 11

We'll assume that you haven't changed the values of x and y, and that y is still exported.

Figure 11.5 Execution of vartest4.

In the case of a subshell executing another subshell (for example, the rolo program executing the luprogram), the process is repeated: The exported variables from the subshell are copied to the newsubshell These exported variables may have been exported from above, or newly exported fromwithin the subshell

After a variable is exported, it remains exported to all subshells subsequently executed.

Consider a modified version of vartest4:

$ cat vartest4

Trang 12

When vartest4 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 byvartest4 vartest5 is such a subshell, and when it is executed, the shell copies into its environmentthe exported variables from vartest4: y and z This should explain the following output:

Trang 13

Figure 11.6 Subshell execution.

To summarize the way local and exported variables work:

Any variable that is not exported is a local variable whose existence will not be known to

subshells

1.

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 parentshell

Trang 14

Note that the variables listed include those that have been inherited from a parent shell.

Trang 15

PS1 and PS2

The characters that the shell displays as your command prompt are stored in the variable PS1 Youcan change this variable to be anything you want As soon as you change it, it'll be used by the shellfrom that point on

==> PS1="I await your next command, master: "

I await your next command, master: date

Wed Sep 18 14:46:28 EDT 2002

I await your next command, master: PS1="$ "

Trang 16

2

3

$

Like any other shell variables, after you log off the system, the values of those variables go with it

So if you change PS1, the shell will use the new value for the remainder of your login session Nexttime you log in, however, you'll get the old value again You can make the change yourself everytime you log in, or you can have the change made automatically by adding it to your profile file(discussed later in this chapter)

Trang 18

$

Trang 20

Whenever you type in the name of a program to be executed, the shell searches a list of directoriesuntil it finds the requested program.[1] When found, it initiates its execution This list of directories iscontained in a special shell variable called PATH This variable is automatically set for you when youlog on to the system See what it's set to now:

[1] Actually, the shell is a bit more intelligent, because it keeps track of where it finds each command you

execute When you re-execute one of these commands, the shell remembers where it was found and doesn't go searching for it again This feature is known as hashing.

The path

/bin:.:/usr/bin

specifies to search /bin, followed by the current directory, followed by /usr/bin To have the

current directory searched first, you put the period at the start of the path:

waits for another user to change to that directory and run su If the PATH specifies that the current

directory be searched first, then the horsed version of su will be executed This version will get the

password that is typed and then print out Sorry The user will think he just typed the wrong

Trang 21

You can always override the PATH variable by specifying a path to the file to be executed For

example, if you type

This last case says to execute the program rolo in the current directory

So now you understand why you couldn't execute rolo from your HOME directory: /users/steve/binwasn't included in your PATH, and so the shell couldn't find rolo This is a simple matter to rectify.You can simply add this directory to your PATH:

$ PATH=/bin:/usr/bin:.:/users/steve/bin

$

Now any program in /users/steve/bin can be executed by you from anywhere:

Trang 22

echo "Incorrect number of arguments"

echo "Usage: lu name"

Trang 23

which the program itself resides).

The PATH only specifies the directories to be searched for programs to be executed, and not for anyother types of files So phonebook must be precisely located for lu There are several ways to fix thisproblem—a problem which, by the way, exists with the rem and add programs as well One approach

is to have the lu program change directory to /users/steve/bin before it does the grep That way,grep finds phonebook because it exists in the current directory:

identical program on the system—programs that you'll probably have to maintain And what happens

if you make a small change to rolo? Are you going to update all their copies as well? A better

solution might be to keep just one copy of rolo but to give other users access to it.[3]

[3] This can be done by giving them execute permission on all the directories leading to rolo, as well as read and execute permissions on the programs themselves They can always copy your programs at that point, but you won't have to maintain them.

If you change all the references of phonebook to explicitly reference your phone book, everyone elsewho uses your rolo program will be using your phone book, and not his own One way to solve theproblem is to require that everyone have a phonebook file in his home directory; this way, if theprogram references the file as $HOME/phonebookw, it will be relative to the home directory of theperson running the program

Let's try this approach: Define a variable inside rolo called PHONEBOOK and set it to

$HOME/phonebook If you then export this variable, lu, rem, and add (which are executed as

subshells by rolo) can use the value of PHONEBOOK to reference the file One advantage of this is if inthe future you change the location of the phonebook file, all you'll have to do is change this onevariable in rolo; the other three programs can remain untouched

Trang 24

Here is the new rolo program, followed by modified lu, add, and rem programs.

$ cd /users/steve/bin

$ cat rolo

#

# rolo - rolodex program to look up, add, and

# remove people from the phone book

#

#

# Set PHONEBOOK to point to the phone book file

# and export it so other progs know about it

Trang 25

2 Add someone to the phone book

3 Remove someone from the phone book

Please select one of the above (1-3): \c'

Trang 28

echo "Incorrect number of arguments"

echo "Usage: rem name"

Trang 29

echo "More than one match; please qualify further"

elif [ "$matches" -eq 1 ]

$ rolo Liz Quick lookup

No phonebook file in /users/steve! Forgot to move it

$ mv /users/steve/bin/phonebook

$ rolo Liz Try again

Liz Stachiw 212-555-2298

Trang 30

$ rolo Try menu selection

Would you like to:

1 Look someone up

2 Add someone to the phone book

3 Remove someone from the phone book

Please select one of the above (1-3): 2

Enter name to be added: Teri Zak

Trang 31

programs to ensure that PHONEBOOK is set to some value.

Trang 32

Your Current Directory

Your current directory is also part of your environment Take a look at this small shell program calledcdtest:

Trang 33

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,

cd also sets OLDPWD to the full pathname of the previous current 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 bythe shell whenever you execute a cd command This search is done only if the specified directory isnot 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 to /users/steve; but if you type

Trang 35

Judicious use of the CDPATH variable can save you a lot of typing, especially if your directory

hierarchy is fairly deep and you find yourself frequently moving around in it (or if you're frequentlymoving around into other directory hierarchies as well)

Unlike the PATH, you'll probably want to put your current directory first in the CDPATH list This givesyou the most natural use of CDPATH (because you're used to doing a cd x to switch to the

subdirectory x) If the current directory isn't listed first, you may end up in an unexpected directory

Trang 36

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, norcan it change its current directory Suppose that you want to write a program to set values for somevariables that you like to use whenever you log on For example, assume that you have the followingfile called vars:

Trang 37

and whose purpose is to execute the contents of file in the current shell That is, commands from file

are executed by the current shell just as if they were typed at that point A subshell is not spawned

to execute the program The shell uses your PATH variable to find file, just like it does when executingother programs

$ vars Execute vars in the current shell

Trang 39

Next, the PATH is modified to include the database bin directory, and the CDPATH variable is set tosearch the current directory, the HOME directory, and the RPTS directory (which presumably containssubdirectories).

After exporting these variables (which as you recall must be done to put the values of these variablesinto the environment of subsequently spawned subshells), the standard shell, /usr/bin/sh, is

started From that point on, this new shell processes commands typed in from the terminal When

Ctrl+d is typed to this shell, control returns to db, which in turn returns control to your login shell

DB: ps See what processes are running

PID TTY TIME COMMAND

Trang 40

123 13 0:40 sh Your login shell

Trang 41

The exec Command

After you started up the new shell from db, you weren't interested in doing anything further after theshell finished, as evidenced by the fact that no commands followed /usr/bin/sh in the program.Instead of having db wait around for the new shell to finish, you can use the exec command to

replace the current program (db) with the new one (/usr/bin/sh) The general format of exec is

exec program

where program is the name of the program to be executed Because the exec'ed program replaces

the current one, there's one less process hanging around; also, startup time of an exec'ed program isquicker, due to the way the Unix system executes processes

To use exec in the db program, you simply replace the last line with

Trang 42

exec /usr/bin/sh

As noted, after this gets executed, db will be replaced by /usr/bin/sh This means that it's pointless

to have any commands follow the exec because they'll never be executed

exec can be used to close standard input and reopen it with any file that you want to read To change

standard input to file, you use the exec command in the form

exec < file

Any commands that subsequently read data from standard input will read from file.

Redirection of standard output is done similarly The command

exec > report

redirects all subsequent output written to standard output to the file report Note here that exec isnot used to start up execution of a new program as previously described; here it is used to reassignstandard input or standard output

If you use exec to reassign standard input and later want to reassign it someplace else, you cansimply execute another exec To reassign standard input back to the terminal, you would write

exec < /dev/tty

The same discussion applies to reassignment of standard output

Sometimes you may want to group a set of commands together for some reason For example, youmay want to send a sort followed by execution of your plotdata program into the background forexecution You can group a set of commands together by enclosing them in a set of parentheses orbraces The first form causes the commands to be executed by a subshell, the latter form by thecurrent shell

Here are some examples to illustrate how they work:

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

TỪ KHÓA LIÊN QUAN