⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u2

📁 UNIX版本6的源代码
💻
📖 第 1 页 / 共 2 页
字号:
.SHWhat's in a Filename, Continued.PPWhen you first made that file called ``junk'',how did .UC UNIXknow that there wasn't another ``junk'' somewhere else,especially since the person in the next office is alsoreading this tutorial?The reason is that generally each user of .UC UNIXhas his own ``directory'',which contains only the files that belong to him.When you create a new file,unless you take special action,the new file is made in your own directory,and is unrelated to any other file of the same namethat might exist in someone else's directory..PPThe set of all files that.UC UNIXknows about are organized into a (usually big) tree,with your files located several branches up into the tree.It is possible for you to ``walk'' around this tree,and to find any file in the system, by starting at the rootof the tree and walking along the right set of branches..PPTo begin, type .B1ls /.B2``/'' is the name of the root of the tree (a convention usedby.UC UNIX ).You will get a response something like this:.B1.ne 6bindevetclibtmpusr.B2This is a collection of the basic directories of filesthat.UC UNIXknows about.On mostsystems,``usr'' is a directory that contains all thenormal users of the system, like you.Now try.B1ls  /usr.B2This should list a long series of names,among which is your own login name.Finally, try.B1ls  /usr/your-name.B2You should get what you get from a plain.B1ls.B2Now try.B1cat  /usr/your-name/junk.B2(if ``junk'' is still around).The name.B1/usr/your-name/junk.B2is called the ``pathname'' of the file thatyou normally think of as ``junk''.``Pathname'' has an obvious meaning:it represents the full name of the path you have to followthrough the tree of directories to get to a particular file.It is a universal rule in.UC  UNIXthat anywhere you can use an ordinary filename,you can use a pathname..PPHere is a picture which may make this clearer:.B1 1.vs 9p.if t .tr /\(sl.ce 100.ne 12(root)/ | \\/  |  \\/   |   \\  bin    etc    usr    dev   tmp / | \\   / | \\   / | \\   / | \\   / | \\/  |  \\/   |   \\adam  eve   mary/        /   \\        \\             /     \\       junkjunk  temp.ce 0.br.tr //.B2.PPNotice that Mary's ``junk'' is unrelated to Eve's..PPThis isn't too exciting if all the files of interest are in your owndirectory, but if you work with someone elseor on several projects concurrently,it becomes handy indeed.For example, your friends can print your book by saying.B1pr  /usr/your-name/chap*.B2Similarly, you can find out what files your neighbor hasby saying.B1ls  /usr/neighbor-name.B2or make your own copy of one of his files by.B1cp  /usr/your-neighbor/his-file  yourfile.B2.PP(If your neighbor doesn't want you poking around in his files,or vice versa,privacy can be arranged.Each file and directory can have read-write-execute permissions for the owner,a group, and everyone else,to control access.See.SE ls (I)and.SE chmod (I)for details.As a matter of observed fact,most users most of the time find openness of morebenefit than privacy.).PPAs a final experiment with pathnames, try.B1ls  /bin  /usr/bin.B2Do some of the names look familiar?When you run a program, by typing its name after a ``%'',the system simply looks for a file of that name.It looks first in your directory(where it typically doesn't find it),then in ``/bin'' and finally in ``/usr/bin''.There is nothing magic about commands like.C cator.C ls ,except that they have been collected into two places to be easy to find and administer..sp.PPWhat if you work regularly with someone else on common informationin his directory?You could just log in as your friend each time you want to,but you can also say``I want to work on his files instead of my own''.This is done by changing the directory that you arecurrently in:.B1chdir  /usr/your-friend.B2Now when you use a filename in something like.C cator.C pr ,it refers to the file in ``your-friend's'' directory.Changing directories doesn't affect any permissions associatedwith a file _if you couldn't access a file from your own directory,changing to another directory won't alter that fact..PPIf you forget what directory you're in, type.B1pwd.B2(``print working directory'')to find out..PPIt is often convenient to arrange one's filesso that all the files related to one thing are in a directory separatefrom other projects.For example, when you write your book, you might want to keep all the textin a directory called book.So make one with.B1mkdir book.B2then go to it with.B1chdir book.B2then start typing chapters.The book is now found in (presumably).B1/usr/your-name/book.B2To delete a directory, see.SE rmdir (I)..PPYou can go up one level in the tree of files by saying.B1chdir ...B2``..'' is the name of the parent of whatever directory you are currently in.For completeness, ``.'' is an alternate namefor the directory you are in..SHUsing Files instead of the Terminal.PPMost of the commands we have seen so far produce outputon the terminal;some, like the editor, also take their input from the terminal.It is universal in.UC UNIXthat the terminal can be replaced by a filefor either or both of input and output.As one example, you could say.B1ls.B2to get a list of files.But you can also say.B1ls >filelist.B2to get a list of your files in the file ``filelist''.(``filelist'' will be created if it doesn't already exist,or overwritten if it does.)The symbol ``>'' is used throughout.UC UNIXto mean ``put the output on the following file,rather than on the terminal''.Nothing is produced on the terminal.As another example, you could concatenateseveral files into one by capturing the output of.C catin a file:.B1cat  f1  f2  f3 >temp.B2.PPSimilarly, the symbol ``<'' means to take the inputfor a program from the following file,instead of from the terminal.Thus, you could make up a script of commonly used editing commandsand put them into a file called ``script''.Then you can run the script on a file by saying.B1ed file <script.B2.SHPipes.PPOne of the novel contributions of.UC UNIXis the idea of a.ulpipe.A pipe is simply a way to connect the output of one programto the input of another program,so the two run as a sequence of processes _a pipe-line..PPFor example,.B1pr  f  g  h.B2will print the files``f'', ``g'' and ``h'',beginning each on a new page.Suppose you wantthem run together instead.You could say.B1.ne 3cat  f  g  h  >temppr  temprm  temp.B2but this is more work than necessary.Clearly what we want is to take the output of.C catandconnect it to the input of.C pr .So let us use a pipe:.B1cat  f  g  h  |  pr.B2The vertical bar means totake the output from.C cat ,which would normally have gone to the terminal,and put it into.C pr ,which formats it neatly..PPAny programthat reads from the terminalcan read from a pipe instead;any program that writes on the terminal can drivea pipe.You can have as many elements in a pipeline as you wish..PPMany.UC UNIXprograms are written so that they will take their input from one or more filesif file arguments are given;if no arguments are given they will read from the terminal,and thus can be used in pipelines..SHThe Shell.PPWe have already mentioned once or twice the mysterious``shell,''which is in fact.SE sh (I).The shell is the program that interprets what you type ascommands and arguments.It also looks after translating ``*'', etc.,into lists of filenames..PPThe shell has other capabilities too.For example, you can start two programs with one command lineby separating the commands with a semicolon;the shell recognizes the semicolon andbreaks the line into two commands.Thus.B1date; who.B2does both commands before returning with a ``%''..PPYou can also have more than one program running.ulsimultaneouslyif you wish.For example, if you are doing something time-consuming,like the editor scriptof an earlier section,and you don't want to wait around for the results before starting something else,you can say.B1ed  file  <script &.B2The ampersand at the end of a command linesays ``start this command running,then take further commands from the terminal immediately.''Thus the script will begin,but you can do something else at the same time.Of course, to keep the output from interferingwith what you're doing on the terminal,it would be better to have said.B1ed  file  <script >lines &.B2which would save the output lines in a filecalled``lines''..PPWhen you initiate a command with ``&'',.UC UNIXreplies with a numbercalled the process number,which identifies the command in case you later wantto stop it.If you do, you can say.B1kill process-number.B2You might also read.SE ps (I)..PPYou can say.B1 1(command-1; command-2; command-3) &.B2to start these commands in the background,or you can start a background pipeline with.B1command-1 | command-2 &.B2.PPJust as you can tell the editoror some similar program to take its inputfrom a file instead of from the terminal,you can tell the shell to read a fileto get commands.(Why not? The shell after all is just a program,albeit a clever one.)For instance, suppose you want to set tabs onyour terminal, and find out the dateand who's on the system every time you log in.Then you can put the three necessary commands(.C tabs ;.C date ;.C who )into a file, let's call it  ``xxx'',and then run it witheither.B1sh xxx.B2or.B1sh <xxx.B2This says to run the shell with the file ``xxx'' as input.The effect is as if you had typed the contents of ``xxx''on the terminal.(If this is to be a regular thing,you can eliminate the need to type``sh'';see.SE chmod (I)and.SE sh (I).).PPThe shell has quite a few other capabilities as well,some of which we'll get to in the section on programming.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -