executes the command. Because the variable is "passed" to the command by the environment, the shell
simply sets the standard output to the appropriate file, and then processes the line for variables, and lastly
executing the command on the line. More on this later.
The set command
If you with to examine the values of all of your current variables, use the command "set:"
$ set
DISPLAY=:0.0
HOME=/home/barnett
IFS=
LD_LIBRARY_PATH=/usr/openwin/lib:/usr/openwin/lib/server
LOGNAME=barnett
MAILCHECK=600
OPENWINHOME=/usr/openwin
PATH=/home/barnett/bin:/usr/ucb:/usr/bin
PS1=$
PS2=>
PWD=/home/barnett
SHELL=/bin/csh
TERM=vt100
USER=barnett
$
Notice the alphabetical order of the variables, and the equals character between the variable and the value.
The "set" command is one way to determine which shell you are currently using. (The C shell puts spaces
between the variable and the value.) Also note the assortment of variables already defined. These are
environment variables.
Environment Variables
UNIX provides a mechanism to pass information to all processes created by a parent process by using
environment variables. When you log onto a system, you are given a small number of variables, predefined.
You can add to this list in your shell start-up files. Every program you execute will inherit these variables. But
the information flow is one-way. New UNIX users find this confusing, and cannot understand why a shell
script can't change their current directory. Picture it this way: suppose you executed hundreds of programs,
and they all wanted to change their environment to a different value. It should be obvious that they can't all
control the same variable. Imagine hundreds of programs trying to change the directory you are currently
using! Perhaps these variables ought to be called hereditary, and not environmental. Children processes
inherit these values form the parents, but can never change how the parents were created. That would require
time-travel, a feature not currentable available in commercial UNIX systems.
As I mentioned before, the shell command "export" is used to update environment variables. The command
export a b c
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
15 of 66
11/21/2011 12:03 PM
marks the variables "a" "b" and "c," and all child processes will inherit the current value of the variable. With
no arguments, it lists those variables so marked. The command "export" is necessary. Changing the value of
an environment variable does not mean this change will be inherited. Example:
HOME=/
myprogram
When "myprogram" executes, the value of the "HOME" variable is not changed. However, in this example:
HOME=/
export HOME
myprogram
the program does get the modified value. Another way to test this is to start a new copy of the shell, and
execute the "export" command. No variables are reported.
The command marks the variable. It does not copy the current value into a special location in memory. A
variable can be marked for export before it is changed. That is,
export HOME
HOME=/
myprogram
works fine.
Special Environment Variables
There are several special variables the shell uses, and there are special variables the system defined for each
user. SunOS and Solaris systems use different environment variables. If in doubt, check the manual pages. I'll
describe some the important Solaris variables.
PATH - Sets searchpath
The "PATH" environment variable lists directories that contain commands. When you type an arbitrary
command, the directories listed are searched in the order specified. The colon is used to separate directory
names. An empty string corresponds to the current directory. Therefore the searchpath
:/usr/bin:/usr/ucb
contains three directories, with the current directory being searched first. This is dangerous, as someone can
create a program called "ls" and if you change your current directory to the one that contains this program,
you will execute this trojan horse. If you must include the current directory, place it last in the searchpath.
/usr/bin:/usr/ucb:
HOME - Your home directory
The "HOME" variable defines where the "cd" goes when it is executed without any arguments. The HOME
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
16 of 66
11/21/2011 12:03 PM
evvironment variable is set by the login process.
CDPATH - cd searchpath
When you execute the "cd" command, and specify a directory, the shell searches for that directory inside the
current working directory. You can add additional directories to this list. If the shell can't find the directory in
the current directory, it will look in the list of directories inside this variable. Adding the home directory, and
the directory above the current directory is useful:
CDPATH=$HOME:..
export CDPATH
IFS - Internal Field Seperator
The "IFS" variable lists the characters used to terminate a word. I discussed this briefly earlier. Normally,
whitespace separates words, and this variable contains a space, a tab and a new line. Hackers find this
variable interesting, because it can be used to break into computer systems. A poorly written program may
carelessly execute "/bin/ps." A hacker may redefine the PATH variable, and define IFS to be "/." When the
program executes "/bin/ps," the shell will treat this as "bin ps." In other words, the program "bin" is executed
with "ps" as an argument. If the hacker has placed a program called "bin" in the searchpath, then the hacker
gains privileged access.
PS1 - Normal Prompt
The "PS1" variable specifies the prompt printed before each command. It is normally "$ ." The current
directory cannot be placed inside this prompt. Well, some people make a joke, and tell a new user to place a
period inside this variable. A "." does signifies the current directory, however, most users prefer the actual
name.
PS2 - Secondary Prompt
The "PS2" environment variable defines the secondary prompt, This is the prompt you see when you execute
a multi-line command, such as "for" or "if." You also see it when you forget to terminate a quote. The default
value is "> ."
MAIL - Incoming mail
The "MAIL" variable specifies where your mailbox is located. It is set by the login process.
MAILCHECK - How often to check for mail
The "MAILCHECK" variable specifies how often to check for mail, in seconds. The default value is 600
seconds (10 minutes). If you set it to zero, every time the shell types a prompt, it will check for mail.
Bourne Shell Tutorial
http://www.grymoire.com/Unix/Sh.html
17 of 66
11/21/2011 12:03 PM
Dostları ilə paylaş: |