台大計算機中心網路推廣協會
              網路課程講義

課程名稱:使用UNIX
上課日期:82年11月2日
講師:原孝忠
───────────────────────────
<<<版權聲明>>>
本著作物版權屬於國立台灣大學電子計算機中心。為了推廣網路的使用,除了下列的限
制之外,任何人均可以任何型式複製或修改這份講義。
一、不得有任何的商業行為
二、複製或修改這份講義時,必須將本版權聲明列入,並註明之
三、國立台灣大學電子計算機中心不對修改過後的內容負任何的責任
───────────────────────────
□前言

    在你使用UNIX之前,你必須在workstation上有account(帳號),你才能進去使用。
在login:出現後,打入帳號名稱,然後輸入密碼,現在你已經完成login 的動作,進入
UNIX了。在進入之後,系統會啟動 "shell",這個程式會接受你打的指令,去執行它,
shell 有好幾種,有csh tcsh sh bash ksh ...,我主要指的是csh 。基本上,我們待
會談的指令都是在shell 下輸入的。
    當你輸入set 指令,你會看到一些變數,來看幾個重要的:

    home    : 家。Home是你擁有的地方,你可以在這個目錄下放置任何東東。你一進
        入系統,一定是先到這裡的。
    path    : 路徑。通常執行檔是放在不同的地方,系統會順著path中的目錄一個個
        去找執行檔。
    term    : 終端機。表示現在的終端機形態,常用vt100 、vt102 、ansi。

set =xxxx 就可以設定變數的值,$ 代表這個變數。

        %set term=vt100
            %set path=($home/bin $path)
            %echo $term
            vt100
            %

□檔案系統介紹

UNIX的檔案系統呈樹狀結構,包含了目錄(Directory)和檔案(file) 。如果說你以前有
接觸過DOS ,那應該不陌生;如果沒有,那你應該仔細瞧瞧。

一、目錄(Directory)
        基本上,目錄是用來分類檔案的。一個目錄下面可以包含許多檔案,或是許多
    目錄,而這些下層的目錄就叫〞子目錄〞 (subdirectory) ,它們也都具有相
    同性質。由這些目錄所構成的一層層分枝,看起來就像一棵樹,所以才叫樹狀
    結構,而最上面的目錄稱作〞根目錄〞。  
    一個目錄在建立的時候,具有兩個基本項目:〞.”和〞..”。”.”指的
    是目錄本身,而〞..”指的是上一層的目錄,這兩個是一直跟著目錄共存亡
    的。
        這是檔案系統簡圖,很像一棵樹吧!
                         ┌ Joe  .....
                         │      ┌ Mail  ┬ joe
                         │      │       ├ Mary
              ┌─ users ┼ Tom  ┤       └ Eric
              │         │      └ dead.letter
              │         └ Mary ┬ Mail .....
              │                 └ ....
            / ┼─ bin ┬ ls
              │       ├ mail
              │       ├ rm
              │       └ cp
              └─ tmp

            %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
        -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。

            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 ,">>"則是將結果附加在檔案後面。

        %echo TEST > test.1
            %echo test >> test.1
            %cat test.1
            TEST
            test
            %

    另外還有一種是標準錯誤輸出(standard error),用來顯示錯誤訊息,如果output
被轉向,我們還是可以立刻在螢幕上看到錯誤訊息。">&"可以把它轉向,">>&" 則是附
加在檔案後面。

    再介紹管道的觀念。"|" 是管道的符號,它的性質就像一根管子,用來連接左右兩
邊的輸出和輸入。左邊指令的output,會當作右邊指令的input ,如此一來,我們就可
以對output做篩選的工作,只留下想要的資料。

        %ps -aux | grep 
            就可以看到所有正在執行的程式。

□行程(process)

    一個正在執行中的程式,我們稱做process 。UNIX是多工的系統(multitasking),
可以同時執行多個程式,讓好幾個人同時使用系統,執行程式。執行程式可分為兩種:
前景執行(foreground)和背景執行(background)。一般我們在shell 下執行程式,一直
要等到程式結束才能繼續輸入指令,這就是前景執行。如果說我們不想等待程式結束,
或是執行時間太長,就可以將程式放到背景去執行,系統會自動執行它,而你就可以去
做別的事了。
    怎樣才能把程式放入背景﹖只要在指令後面加上"&" 就可以了。一但你在背景執行
程式,系統會傳回"[n] xxxx",n 是job id,xxxx是process id。每一個process 都有
process id,而且不會有重覆。
    要想停止前景執行的程式,可以按中止,若是想暫停一下,可以按
暫時停止執行,要恢復執行可以用fg或bg來決定前景或背景執行。

        %xtetris&
             [1]  5678
            %

□萬用字元(wildcard)

在UNIX中,萬用字元對輸入檔名非常有幫助。現在想看一下有哪些指令可用,你先打入

    %cd /bin
    %ls -al

你會發現好多執行檔,如果你只想找"m" 開頭的檔案,可以鍵入

    %ls m*

就可以做到。萬用字元有三種:

    *       代表任意長度的字串
    ?       代表任何的單一字元
    [...]   代表區間內定義的任一字元

    
            c*      代表cat ,cp ,cal ...任意長度且開頭為c 。
            ca?     代表cat,cal ...三個字且前二字為ca。
            ca[a-z] 代表caa,cab,caz ... 三個字且頭二字為ca第三字為a到z。

附錄A   指令介紹

yppasswd                change your password

man               see the manual page of 

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        show the user's group name

quota -v      show the quota limited on user

du                      disply disk usage

pwd                     print working directory name

cd             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
    -g      show the group ownership
    -R      recursively list subdirectories encoutered
    -F      mark directories with '/', executable files
            with '*'

        ls -al
            ls -lF

cat           catenate and display a file

more          display a file page by page

echo                    display a string
        %echo Hello World
            Hello World
            %

cp        copy files

rm            remove a file
rm -r          recursively remove a directory

mv   rename a file (directory)
mv   move a file to the directory

mkdir          creat a new directory
rmdir          remove an empty directory

ln            link a file
    option: -s      symbolic link

chmod   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
             a  all
             r  read access
             w  write access
             x  execute access

        chmod 777 test.1
            chmod a+r,a+x,a+w test.1

sort                  sort a file

talk [@]    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
        %kill -9 -1    //kill all processes
            %kill -9 1234  //kill process #1234

ps                              listing your process
        %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
      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.

      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
      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.