📄 unx02.htm
字号:
Meow, whine
meow
$ cat scotty
Meow, whine
meow
$</PRE>
<P>Although this simple case may not seem terribly useful yet, wait to see its use as you add redirection.
<BR></P>
<P>UNIX shells have special characters that signify redirection. Only the basics are covered here. Refer to Part II, "Hunt for Shells," for details on each shell's redirection syntax. Output redirection is signified by the > character and
input redirection is signified by the < character. Output is commonly redirected to and input is redirected from a file. Now, continue with the rest of the example.
<BR></P>
<P>Next, try the cat command using output redirection, leaving standard input alone. Enter cat > <I>filename</I>. The filename is a name of your choice. Once again, the cat command should be waiting for input (coming from standard input, which is your
keyboard) at the beginning of the next line. Enter a few words, as you did before, press Return, and then, at the start of the next line, press Control+D. The words you typed didn't show up on your screen because you redirected the output of the cat
command. The output was directed to go to the file <I>filename</I>. But how do you know it is there? In order to verify this, use the cat command with input redirection—which is the next order of business.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B><Ctrl><D> must be specified as the first character of an input line for it to be seen as "eof."
<BR></NOTE>
<HR ALIGN=CENTER>
<P>To see the contents of the file <I>filename</I>, you would like the input of the cat command to come from that file, and the output to go to the screen so that you can see it. Therefore, you want to redirect standard input and leave the output alone.
Enter cat < <I>filename</I>. This time, the cat command did not wait for you—because you were not supplying the input. The file supplied the input. The cat command printed the contents of the file to the screen.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>Note the subtle distinction between these two commands: cat > <I>filename</I> and cat < <I>filename</I>. You can remember the difference by verbalizing which way the sign points; does it point
into the command or out of the command? Into the command is input redirection and out of the command is output redirection.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>The cat command allows you to specify a filename to use as input. Try showing the contents of the file this (more common) way: enter cat <I>filename</I>. Many commands are designed similarly—they have an argument that is used to specify a file as
the input. Because of this common command design, redirecting input in this way is not nearly as common as redirecting the output.
<BR></P>
<P>UNIX was developed with the philosophy of having simple commands that do well-defined, simple things. Then, by combining these simple commands, the user could do very powerful things. Pipes are one of the ways UNIX allows users to combine several
commands. The pipe is signified by the vertical bar (|) symbol. A pipe is a means of taking the output of one command and redirecting it as the input of another command. Say that you want to know how many files you have in your current directory. Recall
that the ls command will list all the files in your current directory. You could then count the number of files. But UNIX has a command that counts the number of characters, words, and lines of input and displays these statistics. Therefore, you can
combine these two commands to give you the number of files in your directory. One way you could do this is as follows: ls -l | wc -l. Analyzing this command, you can see that the first part is something familiar. The ls -l command gives a directory listing
in long format. In fact, it prints one file per line. The wc -l command gives the number of lines that are in the input. Combining the two commands via a pipe takes the output of the first command (the long directory listing) and gives it to the input of
the second command. The output of the second command (which is not redirected—it goes to standard output) is displayed on your screen.
<BR></P>
<P>These basic forms of redirection allow you to be very versatile as you learn a few commands at a time. Try to learn a command and use it with various options and arguments, then add redirection of input and output. And finally, combine commands with
pipes. This approach should help you to feel comfortable with the commands and their varied uses.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I13" NAME="I13">
<FONT SIZE=4><B>Configuring Your Environment</B>
<BR></FONT></A></CENTER></H3>
<P>In order to make using the shell easier and more flexible, UNIX uses the concept of an environment. Your environment is a set of values. You can change these values, add new values, or remove existing values. These values are called environment
variables—environment because they describe or define your environment, and variables because they can change.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I14" NAME="I14">
<FONT SIZE=3><B>Viewing and Setting Environment Variables</B>
<BR></FONT></A></CENTER></H4>
<P>Every user's environment looks a little different. Why don't you see what your environment looks like? Type the env command with no arguments. The output formatting and variable names depend on which shell you are using and how your system is
configured. A typical environment might include some of the following:
<BR></P>
<PRE>$ <B>env</B>
HOME=/u/sartin
LOGNAME=sartin
MAIL=/usr/mail/sartin
MANPATH=/usr/man:/usr/contrib/man:/usr/local/man
PATH=/bin/posix:/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin
SHELL=/bin/sh
TERM=vt100
TZ=CST6CDT
$ echo $HOME
/u/sartin
$</PRE>
<P>Sometimes the number of variables in your environment grows quite large, so much so that you don't want to see all of the values displayed when you are interested in just one. If this is the case, you can use the echo command to show an environment
variable's current value. To specify that a word you type should be treated differently—as a value of an environment variable—you immediately precede the variable name with a dollar sign ($). Be careful not to type any whitespace between the $
and the word. One of the variables in the example is HOME. You probably have this variable in your environment, too. Try to display its value using echo.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> If you use csh, some environment variables are automatically copied to and from csh variables. These include HOME, TERM, and PATH, which csh keeps in home, term, and path.
<BR></NOTE>
<HR ALIGN=CENTER>
<P>You can create a new environment variable by simply giving it a value. If you give an existing variable a value, the old value is overwritten. One difficulty in setting environment variables is that the way you set them depends on the shell you are
using. To see how to set environment variables, look at the details about the shell you are using in Part II, "Hunt For Shells."
<BR></P>
<P>In order for your screen to display the output correctly, the environment variable TERM needs to have a reasonable value. This variable name comes from the times when terminals were used as displays (before PCs and graphics displays were common).
Different terminals supported varying output control. Therefore, UNIX systems have various terminal types that they support. These are not standard, so you need to find out which terminal type to use from your support personnel. If you are using a PC to
connect to a UNIX system, your PC is running a terminal emulation tool. Most of these tools have the capability to emulate several types of terminal. The important point here is to make sure that your emulator and your TERM variable are the same (or
compatible). Start by seeing what your TERM variable is set to by entering echo $TERM. Refer to your PC terminal emulation manual and ask your system administrator for help to make sure that this is set up correctly.
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>Many terminal emulators (including the Microsoft Windows "Terminal" program) support either "VT100" or ANSI standard terminal control sequences. Try setting TERM to vt100 or ansi for
this type of terminal emulator.
<BR></NOTE>
<HR ALIGN=CENTER>
<H4 ALIGN="CENTER">
<CENTER><A ID="I15" NAME="I15">
<FONT SIZE=3><B>Using Shell Startup Files</B>
<BR></FONT></A></CENTER></H4>
<P>Where do all these environment variables come from? Well, the system sets up various ones for you. And each user commonly sets up others during the login process. Yes, you may be doing this without even knowing it. During the startup, which happens at
login, a shell is started. This shell automatically looks in a special place or two for some startup information. One of these places is in your home directory. The startup information in your home directory is found in special files. The specific shell
you are using will determine the name of the particular file. When the shell starts up, it examines this file and performs whatever actions are specified. One of the common actions is to give values to environment variables. This action is called
initializing or setting the values.
<BR></P>
<P>One environment variable that is commonly set in a user's shell startup file is the PATH variable (or lowercase path for C-shell users). This variable's value is a list of places (directories) on the system where the shell should look to locate a
command. Each command you type is physically located as a file somewhere on your file system. It is possible for the same command name to be located in different places (and to have either the same or different behavior when executed). Say that you have a
program called my_program that is stored in your home directory, and your friend has a program called my_program, which is in her home directory. If you type my_program at the prompt, the shell needs to know where to look to find the storage location of
my_program. The shell looks at the value of the PATH variable and uses the list of directories as an ordered directory search list. The first directory that has a my_program stops the search, and the shell executes that file. Because all files within a
single directory must be unique, this gives a straightforward and sufficient method for finding executables (commands).
<BR></P>
<P>You probably want $HOME/bin to be toward the beginning of your PATH directory list, whereas you may want your friend's binary directory to be toward the end, or not listed at all. This way, when you type my_program, you will execute your my_program
rather than hers. You can do all types of things in shell startup files in addition to setting environment variable values. If you want, you can add an echo command that prints out a greeting or reminds you to do something. One common item that is
configured inside a shell startup file is the setup of your control characters. (See "Working on the System" later in this chapter.) These startup files are a powerful tool for you, the user of the shell, to configure the behavior of the shell
automatically. Shell startup files are covered in more detail in Part II, "Hunt for Shells."
<BR></P>
<HR ALIGN=CENTER>
<NOTE>
<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP: </B>It is a good idea to create a bin directory in your HOME and store executables there. Include $HOME/bin in your path.
<BR></NOTE>
<HR ALIGN=CENTER>
<H4 ALIGN="CENTER">
<CENTER><A ID="I16" NAME="I16">
<FONT SIZE=3><B>Configuring with </B><B><I>rc</I></B><B> files</B>
<BR></FONT></A></CENTER></H4>
<P>The idea of having a file that is read on startup is not only used by the shells. In fact, many commands have special files containing configuration information that the user can modify. The general class of files is called rc files. This comes from the
naming convention of these files. Most of these files end with the letters rc. Some of the more common files are .exrc, .mailrc, and .cshrc. These are all dot files; that is, they begin with a period (dot). The significance of starting a filename with a
dot is that this file is not displayed during normal directory listing. If you want to see these files, use the -a option to the ls command. The .exrc file is used by the vi and ex editors (see Chapter 7, "Text Editing with vi, EMACS, and sed").
The .mailrc file is used by various electronic mail tools (see Chapter 9, "Communicating with Others"). The .cshrc file is the C-shell startup file just discussed. The rc files are normally found in your home directory; that is, the default
location for most of these files. Look at which rc files you have in your home directory (use the ls -a command). Then examine the contents of one of the files (use the cat <I>filename</I> command).
<BR></P>
<P>Your environment has a great effect on the use of your system. It is initialized during login with a shell startup file, and it grows and changes as you create new variables and change existing ones. Your environment affects every command you execute.
It is important to get your environment set up to make your common actions easy. Spend the time to do this now and you will be glad you did later.
<BR></P>
<H3 ALIGN="CENTER">
<CENTER><A ID="I17" NAME="I17">
<FONT SIZE=4><B>Managing Your Password</B>
<BR></FONT></A></CENTER></H3>
<P>During login, UNIX asked you to enter your password. If this is your first time on this computer, your password was configured by the system administrator. One of the very first things you should do after logging in is change your password so that no
one, not even the system administrator, knows what it is. You can do this via the passwd command. But before you do this, you should put some thought into what you want your password to be. Here are some points to consider:
<BR></P>
<OL>
<LI>It should be easy for you to remember. If you forget what your password is, no one, not even the system administrator, can look it up for you. The only thing the system administrator can do is to reset your password to a value. This wastes the
administrator's time as well as yours.
<BR>
<BR></LI>
<LI>It shouldn't be easy for anyone to figure out. Do not make it anyone's name or birth date, or your user name, or any of these spelled backwards. It is also wise to avoid something that appears in a dictionary. A good idea would be to include at least
one nonalphabetic character (for example, a period or a dollar sign).
<BR>
<BR></LI>
<LI>Make it a reasonable length. Some systems impose a minimum number of characters for a password. At least 5 characters is adequate. There isn't usually a limit as to the maximum number of characters, but only the first 8 are significant. The ninth
character and after are ignored when checking to see whether you typed your password correctly.
<BR>
<BR></LI>
<LI>Change your password once in a while. Some systems check the last time you changed your password. If a time limit has been reached, you will be notified that your password has expired as you log in. You will be prompted to change it immediately and
won't be allowed to log in until you successfully get it changed. This time limit is system imposed. Changing your password every few months is reasonable.
<BR>
<BR></LI>
<LI>Don't write it down or tell it to anyone. Don't write it on scraps of paper. Don't tell your mother. Don't write it in your calendar. Don't write it in your diary. Don't tell your priest. Don't put it in a dialup terminal configuration file. Nowhere.
Nobody. Maybe in your safe deposit box.
<BR>
<BR></LI></OL>
<P>After you have thought about what you want your password to be, you can change it with the passwd command. Try it now; you can change your password as often as you like. Enter passwd. First, a prompt asking you to enter your old password is displayed.
Type your old password and press Return. Next, you are prompted for your new password. Type it in and press Enter. Finally, you are prompted to re-enter your new password. This confirmation helps avoid changing your password if you made a typing error. If
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -