Узбекистан, Бухара, Бухарский институт высоких технологий, 2013 |
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
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.
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.
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).
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.
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.