BBS水木清华站∶精华区

作者: bbs.ee.ntu.edu.tw (连线精华区) 
标题: 使用 UNIX 
时间: Sun Aug 10 11:06:52 1997 
 
       台大计算机中心网路推广协会 
              网路课程讲义 
 
课程名称:使用UNIX 
上课日期:82年11月2日 
讲师:原孝忠 
——————————————————————————— 
<<<版权声明>>> 
本著作物版权属於国立台湾大学电子计算机中心。为了推广网路的使用,除了下列的限 
制之外,任何人均可以任何型式复制或修改这份讲义。 
一、不得有任何的商业行为 
二、复制或修改这份讲义时,必须将本版权声明列入,并注明之 
三、国立台湾大学电子计算机中心不对修改过後的内容负任何的责任 
——————————————————————————— 
□前言 
 
    在你使用UNIX之前,你必须在workstation上有account(帐号),你才能进去使用。 
在login:出现後,打入帐号名称,然後输入密码,现在你已经完成login 的动作,进入 
UNIX了。在进入之後,系统会启动 "shell",这个程式会接受你打的指令,去执行它, 
shell 有好几种,有csh tcsh sh bash ksh ...,我主要指的是csh 。基本上,我们待 
会谈的指令都是在shell 下输入的。 
 
会谈的指令都是在shell 下输入的。 
    当你输入set 指令,你会看到一些变数,来看几个重要的: 
 
    home    : 家。Home是你拥有的地方,你可以在这个目录下放置任何东东。你一进 
        入系统,一定是先到这里的。 
    path    : 路径。通常执行档是放在不同的地方,系统会顺著path中的目录一个个 
        去找执行档。 
    term    : 终端机。表示现在的终端机形态,常用vt100 、vt102 、ansi。 
 
set <name>=xxxx 就可以设定变数的值,$<name> 代表这个变数。 
 
    <eg>    %set term=vt100 
            %set path=($home/bin $path) 
            %echo $term 
            vt100 
            % 
 
□档案系统介绍 
 
UNIX的档案系统呈树状结构,包含了目录(Directory)和档案(file) 。如果说你以前有 
接触过DOS ,那应该不陌生;如果没有,那你应该仔细瞧瞧。 
 
一、目录(Directory) 
 
一、目录(Directory) 
        基本上,目录是用来分类档案的。一个目录下面可以包含许多档案,或是许多 
    目录,而这些下层的目录就叫″子目录″ (subdirectory) ,它们也都具有相 
    同性质。由这些目录所构成的一层层分枝,看起来就像一棵树,所以才叫树状 
    结构,而最上面的目录称作″根目录″。   
    一个目录在建立的时候,具有两个基本项目:″.”和″..”。”.”指的 
    是目录本身,而″..”指的是上一层的目录,这两个是一直跟著目录共存亡 
    的。 
        这是档案系统简图,很像一棵树吧! 
                         ┌ Joe  ..... 
                         │      ┌ Mail  ┬ joe 
                         │      │       ├ Mary 
              ┌— users ┼ Tom  ┤       └ Eric 
              │         │      └ dead.letter 
              │         └ Mary ┬ Mail ..... 
              │                 └ .... 
            / ┼— bin ┬ ls 
              │       ├ mail 
              │       ├ rm 
              │       └ cp 
              └— tmp 
 
        <eg>    %cd / 
 
        <eg>    %cd / 
                %pwd 
                / 
                %cd bin 
                %pwd 
                /bin 
                %cd .. 
                %pwd 
                / 
                % 
 
二、档案 (file) 
        档案是储存资料的地方,这个资料可能是一份文字档,也可能是一个可执行的 
    程式,不同功能的档案,有著不同的属性,现在来看看。 
 
        % ls -l 
        total 1059 
        drwx------  4 jong          512 Sep  6 14:54 Mail 
        drwxr-xr-x  2 jong          512 Oct 22 19:02 News 
        lrwxrwxrwx  1 jong            5 Oct 29 16:05 bin -> games 
        drwxr-xr-x  2 jong          512 Oct 29 16:05 games 
        -rw-------  1 jong         9915 Sep  6 14:55 mbox 
        drwxr-xr-x  2 jong          512 Oct  1 10:06 ok 
 
        drwxr-xr-x  2 jong          512 Oct  1 10:06 ok 
        -rw-r--r--  1 jong       107171 Apr 14  1993 soccer.zip 
        drwxr-xr-x  4 jong         1024 Oct 27 20:54 xtype 
        -r-xr-xr-x  3 jong       925696 Aug 28 14:48 xv 
 
        在每个档案前有十个字,第一个字元是档案形式,d 指目录,- 指档案,l 指 
    连结(link)档案。从二到十是属性,三字一组,一共三组。一组三字代表<读、写 
    、执行>,即  r w x , -  则表无此属性。第一组是owner ,第二组是group , 
    第三组是others。 
 
    <eg>        rw-------       只有owner 可读写 
                r-xr-xr-x       可读、执行,不可写 
                rw-r--r--       owner 可读写,其他只可读 
 
        档案的名字除了"/" 之外,都可以用,随便多少都可以。档案开头是"." 的档 
    案是隐藏档,平常看不到,要用ls -a 或 ls -al 来看。 
 
□输出入的重置(redirection)与管道(pipe) 
 
    输出和输入虽然是硬体设备,但UNIX却把它们视为档案,既然是档案,就不需要知 
道到底是什麽,可能是keyboard、monitor ,也可能是磁碟上的一个档。一般说来,标 
准输出(standard input)是monitor,标准输入(standard output)是keyboard,但我们 
能够把它们转向到别的档案,也就是从档案输入资料,或是输出资料到某个档。">" 用 
 
能够把它们转向到别的档案,也就是从档案输入资料,或是输出资料到某个档。">" 用 
来转向output,"<" 用来转向input ,">>"则是将结果附加在档案後面。 
 
    <eg>    %echo TEST > test.1 
            %echo test >> test.1 
            %cat test.1 
            TEST 
            test 
            % 
 
    另外还有一种是标准错误输出(standard error),用来显示错误讯息,如果output 
被转向,我们还是可以立刻在萤幕上看到错误讯息。">&"可以把它转向,">>&" 则是附 
加在档案後面。 
 
    再介绍管道的观念。"|" 是管道的符号,它的性质就像一根管子,用来连接左右两 
边的输出和输入。左边指令的output,会当作右边指令的input ,如此一来,我们就可 
以对output做筛选的工作,只留下想要的资料。 
 
    <eq>    %ps -aux | grep <name> 
            就可以看到<name>所有正在执行的程式。 
 
□行程(process) 
 
 
 
    一个正在执行中的程式,我们称做process 。UNIX是多工的系统(multitasking), 
可以同时执行多个程式,让好几个人同时使用系统,执行程式。执行程式可分为两种: 
前景执行(foreground)和背景执行(background)。一般我们在shell 下执行程式,一直 
要等到程式结束才能继续输入指令,这就是前景执行。如果说我们不想等待程式结束, 
或是执行时间太长,就可以将程式放到背景去执行,系统会自动执行它,而你就可以去 
做别的事了。 
    怎样才能把程式放入背景?只要在指令後面加上"&" 就可以了。一但你在背景执行 
程式,系统会传回"[n] xxxx",n 是job id,xxxx是process id。每一个process 都有 
process id,而且不会有重覆。 
    要想停止前景执行的程式,可以按<Ctrl-C>中止,若是想暂停一下,可以按 
<Ctrl-Z>暂时停止执行,要恢复执行可以用fg或bg来决定前景或背景执行。 
 
    <eq>    %xtetris& 
             [1]  5678 
            % 
 
□万用字元(wildcard) 
 
在UNIX中,万用字元对输入档名非常有帮助。现在想看一下有哪些指令可用,你先打入 
 
    %cd /bin 
    %ls -al 
 
    %ls -al 
 
你会发现好多执行档,如果你只想找"m" 开头的档案,可以键入 
 
    %ls m* 
 
就可以做到。万用字元有三种: 
 
    *       代表任意长度的字串 
    ?       代表任何的单一字元 
    [...]   代表区间内定义的任一字元 
 
    <eq> 
            c*      代表cat ,cp ,cal ...任意长度且开头为c 。 
            ca?     代表cat,cal ...三个字且前二字为ca。 
            ca[a-z] 代表caa,cab,caz ... 三个字且头二字为ca第三字为a到z。 
 
附录A   指令介绍 
 
yppasswd                change your password 
 
man  <name>             see the manual page of <name> 
 
 
 
who                     who is logged in on the system 
 
whoami                  display the effective current username 
 
w                       who is logged in, and what are they doing 
 
groups <username>       show the user's group name 
 
quota -v <username>     show the quota limited on user 
 
du                      disply disk usage 
 
pwd                     print working directory name 
 
cd <dirname>            change directory to another directory 
 
cd                      change directory to your home directory 
 
ls                      list the contents of a directory 
    options: 
    -a      list all entries 
    -l      list in long format 
 
    -l      list in long format 
    -g      show the group ownership 
    -R      recursively list subdirectories encoutered 
    -F      mark directories with '/', executable files 
            with '*' 
 
    <eg>    ls -al 
            ls -lF 
 
cat <filename>          catenate and display a file 
 
more <filename>         display a file page by page 
 
echo                    display a string 
    <eg>    %echo Hello World 
            Hello World 
            % 
 
cp <file1> <file2>      copy files 
 
rm <filename>           remove a file 
rm -r <dirname>         recursively remove a directory 
 
 
 
mv <filename> <newname> rename a file (directory) 
mv <filename> <dirname> move a file to the directory 
 
mkdir <dirname>         creat a new directory 
rmdir <dirname>         remove an empty directory 
 
ln <filename>           link a file 
    option: -s      symbolic link 
 
chmod <Mode> <filename> change the permission mode of file 
    absolute:400  read by owner 
             200  write by owner 
             100  execute by owner 
             040  read by group 
             020  write by group 
             010  execute by group 
             004  read by others 
             002  write by others 
             001  execute by others 
    symbolic:u  user(owner) 
             g  group 
             o  others 
 
             o  others 
             a  all 
             r  read access 
             w  write access 
             x  execute access 
 
    <eg>    chmod 777 test.1 
            chmod a+r,a+x,a+w test.1 
 
sort <filename>                 sort a file 
 
talk <username>[@<hostname>]    talk with other 
 
mesg [y/n]                      allow others to talk to you 
 
history                         list your used commands 
 
jobs                            list of background jobs 
 
kill                            terminate process 
    <eg>    %kill -9 -1    //kill all processes 
            %kill -9 1234  //kill process #1234 
 
 
 
ps                              listing your process 
    <eg>    %ps 
            PID     TTY     TIME    COMMAND 
            3211    p0      0:00    - (csh) 
            1568    p0      0:00    ps 
            3456    p0      0:01    vi /tmp/snd.3476 
            3455    p0      0:01    elm 
            %kill -9 3456 
            %ps 
            PID     TTY     TIME    COMMAND 
            3211    p0      0:00    - (csh) 
            1570    p0      0:00    ps 
            3455    p0      0:01    elm 
 
using floppy: 
    mdir                        list msdos disk 
    mcopy  a:dosfile unixfile   transfer files from/to floppy 
    mcopy  unixfile a:dosfile 
    mdel  a:dosfile             delete floppy files 
    eject                       eject floppy files disk 
 
附录B   A very brief look at Unix history 
 
附录B   A very brief look at Unix history 
      Unix history goes back to 1969 and the famous "little-used PDP-7 
      in a corner" on which Ken Thompson, Dennis Ritchie (the R in K&R) 
      and others started work on what was to become Unix.  The name 
      "Unix" was intended as a pun on Multics (and was written "Unics" 
      at first -- UNiplexed Information and Computing System). 
 
      For the first 10 years, Unix development was essentially confined 
      to Bell Labs.  These initial versions were labeled "Version n" or 
      "Nth Edition" (of the manuals), and were for DEC's PDP-11 (16 
      bits) and later VAXen (32 bits).  Some significant versions 
      include: 
 
      V1 (1971):  1st Unix version, in assembler on a PDP-11/20. 
         Included file system, fork(), roff, ed.  Was used as a text 
         processing tool for preparation of patents.  Pipe() appeared 
         first in V2! 
 
      V4 (1973):  Rewritten in C, which is probably the most 
         significant event in this OS's history: it means Unix can be 
         ported to a new hardware in months, and changes are easy.  The 
         C language was originally designed for the Unix operating 
         system, and hence there is a strong synergy between C and Unix. 
 
         system, and hence there is a strong synergy between C and Unix. 
 
      V6 (1975):  First version of Unix widely available outside 
         Bell Labs (esp.  in universities).  This was also the start of 
         Unix diversity and popularity.  1.xBSD (PDP-11) was derived 
         from this version.  J. Lions published "A commentary on the 
         Unix Operating System" based on V6. 
 
      V7 (1979):  For many, this is the "last true Unix", an 
         "improvement over all preceding and following Unices" 
         [Bourne].  It included full K&R C, uucp, Bourne shell.  V7 was 
         ported to the VAX as 32V.  The V7 kernel was a mere 40 
         Kbytes! 
 
      These Vn versions were developed by the Computer Research Group 
      (CRG) of Bell Labs.  Another group, the Unix System Group (USG), 
      was responsible for support.  A third group at Bell Labs was also 
      involved in Unix development, the Programmer's WorkBench (PWB), 
      to which we owe, for example, sccs, named pipes and other 
      important ideas.  Both groups were merged into Unix System 
      Development Lab in 1983. 
 
      Work on Unix continued at Bell Labs in the 1980s.  The V series 
 
      Work on Unix continued at Bell Labs in the 1980s.  The V series 
      was further developed by the CRG (Stroustrup mentions V10 in the 
      2nd edition of his book on C++), but we don't seem to hear much 
      about this otherwise.  The company now responsible for Unix 
      (System V) is called Unix System Laboratories (USL) and is 
      majority-owned by AT&T.  Novell has bought USL (early 93)?! 
 
      But much happened to Unix outside AT&T, especially at Berkeley 
      (where the other major flavor comes from).  Vendors (esp. of 
      workstations) also contributed much (e.g. Sun's NFS). 
 
      The book "Life with Unix" by Don Libes and Sandy Ressler is 
      fascinating reading for anyone interested in Unix, and covers a 
      lot of the history, interactions, etc..  Much in the present 
      section is summarized from this book. 
 
 
   □—————————————————————————————————□ 
   │ ☆  ㄙ  ┼  □┼ ┌┼□ ☆     bbs.ntu.edu.tw      │ 
   │ ☆  O  ∧  Ο│ □┼□ ☆    < 140.112.8.19 >     │ 
   □—————————————————————————————————□ 
 

BBS水木清华站∶精华区