Опубликован: 06.08.2012 | Доступ: свободный | Студентов: 1189 / 25 | Оценка: 5.00 / 5.00 | Длительность: 53:41:00
Лекция 7:

The tools of the trade

Environment variables

The UNIX programming model includes a concept called environment variables. This rather unusual sounding name is simply a handy method of passing relatively long-lived information of a general nature from one program to another. It's easier to demonstrate the use than to describe. Table 7-3 takes a look at some typical environment variables. To set environment variables from Bourne-style shells, enter:

 $ export TERM=xterm

This sets the value of the TERM variable to xterm. The word export tells the shell to pass this information to any program it starts. Once it's exported, it stays exported. If the variable isn't exported, only the shell can use it.

Alternatively, if you want to set the variable only once when running a program, and then forget it, you can set it at the beginning of a command line:

$ TERM=xterm-color mutt

This starts the mutt mail reader (see page 474) with xterm's colour features enabled.

For csh and tcsh, set environment variables with:

% setenv TERM xterm

To start a process with these variables, enter:

% env xterm-color mutt
Таблица 7.3. Common environment variables
Name Purpose
BLOCKSIZE The size of blocks that programs like df count. The default is 512 bytes, but it’s often more convenient to use 1024 or even 1048576 (1 MB).
DISPLAY When running X, the name of the X server. For a local system, this is typically unix:0. For remote systems, it’s in the form system-name: server-number.screen-number. For the system bumble.example.org, you would probably write bumble.example.org:0.
EDITOR The name of your favourite editor. Various programs that start editors look at this variable to know which editor to start.
HOME The name of your home directory.
LANG The locale that you use. This should be the name of a directory in /usr/share/locale.
MAIL Some programs use this variable to find your incoming mail file.
MANPATH A list of path names, separated by colons (:), that specifies where the man program should look for man pages. A typical string might be /usr/share/man:/usr/local/man, and specifies that there are many pages in each of the directories /usr/share/man and /usr/local/man.
NTAPE The name of the non-rewinding tape device. See page 252 for more details.
PATH A list of path names, separated by colons (:), that specifies where the shell should look for executable programs if you specify just the program name.
PS1 In Bourne-style shells, this is the prompt string. It’s usually set to $, but can be changed. See page 114 for a discussion of a possible prompt for bash.
PS2 In Bourne-style shells, this is the prompt string for continuation lines. It’s usually set to >.
SHELL The name of the shell. Some programs use this for starting a shell.
TAPE The name of the rewinding tape device. See page 252 for more details.
TERM The type of terminal emulation you are using. This is very important: there is no other way for an application to know what the terminal is, and if you set it to the wrong value, full-screen programs will behave incorrectly.
TZ Time zone. This is the name of a file in /usr/share/zoneinfo that describes the local time zone. See the section on timekeeping on page 155 for more details.

Note particularly the PATH variable. One of the most popular questions in the FreeBSD-questions mailing list is "I have compiled a program, and I can see it in my directory, but when I try to run it, I get the message "command not found." This is usually because PATH does not include the current directory.

It's good practice not to have your current directory or your home directory in the PATH: if you do, you can be subject to security compromises. For example, somebody could install a program called ps in the directory /var/tmp. Despite the name, the program might do something else, for example remove all files in your home directory. If you change directory to /var/tmp and run ps, you will remove all files in your home directory. Obviously much more subtle compromises are possible.

Instead, run the program like this:

$ ./program

You should set your PATH variable to point to the most common executable directories. Add something like this to your .profile file (for Bourne-style shells):

PATH=/usr/bin:/usr/local/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin
export PATH

This variable is of great importance: one of the leading problems that beginners have is to have an incorrect PATH variable.

Printing out environment variables

So you can't start a program, and you're wondering whether your PATH environment variable is set correctly. You can find out with the echo command:

$ echo $PATH
/bin:/usr/bin

The $ at the beginning of $PATH tells the shell to substitute the value of the environment variable for its name. Without this, the shell has no way of knowing that it's an environment variable, so it passes the text PATH to echo, which just prints it out.

If you want to print out all the environment variables, use the printenv command:

$ printenv | sort
BLOCKSIZE=1048576
CLASSPATH=/usr/local/java/lib:/usr/local/java/lib/classes.zip:/hcme/grcg/netscape/
CVSROOT=/home/ncvs
DISPLAY=freebie:0
EDITOR=emacs
HOME=/home/grog
PAGER=less
PATH=.:/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin
XAUTHORITY=/home/grog/.Xauthority

This example sorts the variables to make it easier to find them. In all probability, you'll find many more variables.

Command line editing

Typing is a pain. If you're anything like me, you're continually making mistakes, and you may spend more time correcting typing errors than doing the typing in the first place. It's particularly frustrating when you enter something like:

$ groff -rex=7.5 -r$$ -rL -rW -rN2 -mpic tmac.M unixerf.nm
troff: fatal error: can't open 'unixerf.mm': No such file or directory

This command should create the PostScript version of this chapter, but unfortunately I messed up the name of the chapter: it should have been unixref.mm, and I typed unixerf.mm.

Yes, I know this looks terrible. In fact, UNIX has ways to ensure
 you almost never need to write commands like this. The command I really use to format this chapter
is "make unixref".

It would be particularly frustrating if I had to type the whole command in again. UNIX offers a number of ways to make life easier. The most obvious one is so obvious that you tend to take it for granted: the Backspace key erases the last character you entered. Well, most of the time. What if you're running on a machine without a Backspace key? You won't have that problem with a PC, of course, but a lot of workstations have a DEL key instead of a Backspace key. UNIX lets you specify what key to use to erase the last character entered. By default, the erase character really is DEL, but the shell startup changes it and prints out a message saying what it has done:

 erase ^H, kill ^U, intr ^C, status ^T

in the example on page 113. ^H (Ctrl-H) is an alternative representation for Backspace.

The three other functions kill, intr, and status perform similar editing functions. Kill erases the whole line, and intr stops a running program.

More correctly, intr sends a signal called SIGINT to the process. This normally causes a program to stop.

You'll notice that it is set to Ctrl-C, so its function is very similar to that of the MS-DOS Break key. status is an oddball function: it doesn't change the input, it just displays a statistics message. bash doesn't in fact use it: it has a better use for Ctrl-T.

In fact, these control characters are just a few of alarge number of control characters that you can set. Table 7-4 gives a nover view of the more common control characters. For a complete list, see the man page stty(1).

Таблица 7.4. Terminal control characters
Name Default Function
CR \r Go to beginning of line. Normally, this also terminates input (in other words, it returns the complete line to the program, which then acts on the input).
NL \n End line. Normally, this also terminates input.
INTR Ctrl-C Generate a SIGINT signal. This normally causes the process to terminate.
QUIT Ctrl-| Generate a SIGQUIT signal. This normally causes the process to terminate and core dump, to save a copy of its memory to disk for later analysis.
ERASE DEL Erase last character. FreeBSD sets this to Backspace on login, but under some unusual circumstances you might find it still set to DEL.
KILL Ctrl-U Erase current input line.
EOF Ctrl-D Return end-of-file indication. Most programs stop when they receive an EOF.
STOP Ctrl-S Stop output. Use this to examine text that is scrolling faster than you can read.
START Ctrl-Q Resume output after stop.
SUSP Ctrl-Z Suspend process. This key generates a SIGTSTP signal when typed. This normally causes a program to be suspended. To restart, use the fg command
DSUSP Ctrl-Y Delayed suspend. Generate a SIGTSTP signal when the character is read. Otherwise, this is the same as SUSP.
REPRINT Ctrl-R Redisplay all characters in the input queue (in other words, characters that have been input but not yet read by any process). The term "print" recalls the days of harcopy terminals. Many shells disable this function.
DISCARD Ctrl-O Discard all terminal output until another DISCARD character arrives, more input is typed or the program clears the condition.

To set these characters, use the stty program. For example, if you're used to erasing the complete input line with Ctrl-X, and specifying an end-of-file condition with Ctrl-Z, you could enter:

$ stty susp \377 kill "X eof "Z

You need to set SUSP to something else first, because by default it is Ctrl-Z, so the system wouldn't know which function to perform if you press "Z.

The combination \377 represents the character octal 377 (this notation comes from the C programming language, and its origin is lost in the mists of time, back in the days when UNIX ran on PDP-11s). This character is the "null" character that turns off the corresponding function. System V uses the character \0 for the same purpose.

In this particular case, ^X really does mean the character ^ followed by the letter X, and not Ctrl-X, the single character created by holding down the Control character and pressing X at the same time.

Дмитрий Миляев
Дмитрий Миляев
Россия
Berkut Molodoy
Berkut Molodoy
Россия