Monday, May 7, 2012

workstation beginer's guide



本文轉自我 BS2 個板,把圖片和 link 換一換XD


==============================================================================



本篇文章為板主半夜睡不著覺夢遊不小心打出來的文章

不保證其正確性,但很開心能與大家分享 

--------------------------------------

前言:大一同學應該都能漸漸體會到,愈來愈多 老師/助教 會要求同學要在系上工作站

環境上完成作業,有鑑於大多同學對工作站 Unix-like system 很陌生,而我個人對各個

助教在解釋工作站時覺得有些小細節會有小遺漏,無法帶給同學比較完整、宏觀的教學

於是乎我來夢遊一篇教學 XD


==============================================================================

Reference.

    由於我的能力以及本教學涵蓋範圍有限,希望同學有空時可以參考以下資源

    《UNIX PowerTools, 3nd Edition

    《Bash Guide for Beginners》 [PDF, 2e ]

    《Learning the bash Shell, 3rd Edition

    《Classic Shell Scripting

    《Essential System Administration, 3rd Edition

    《交大資工系 SA/NA 課程》 sysadm2011  netadm2012

    《FreeBSD Handbook

    《工三的系計中專業的助教ㄉㄉ們

==============================================================================

大綱

    主要提這三個面向的簡單概覽 (意思就是你別想從這份教學中學到太多 :P )

        about UNIX workstation 

        shell command 

        the vim editor











==============================================================================

Ch 1.  Unix 

    1.1 history 

    1.2 difference between Unix-like systems 

    1.3 why Unix 













==============================================================================

    1.1 history 

        UNIX 是米國 AT&T 公司在 1969 年於 Bell lab 發展出來的分時多人多工

        並具有強大可攜性的作業系統


        最早的 UNIX 是由兩位大師 Kenneth Thompson 和 Dennis M. Ritchie

        共同開發的,一開始是用組合語言邊寫,後來 DMR 發明並改用 C 來重

        寫 UNIX 整個系統,此為現代 Unix-like OS 的濫觴

        更多小故事可以參考 DMR 大師發表的這篇論文

        《The Evolution of the Unix Time-sharing System》 




==============================================================================

    1.2 difference between Unix-like systems

        UNIX 系統後來因為一些商業問題而有了爭議,UC Berkeley 從 UNIX 為基礎

        發展出  Berkeley Software Distribution (BSD) 


        BSD 系統的成功後來又發展了 FreeBSD、NetBSD、OpenBSD、Darwin 等
                                                            (Mac OS X的祖先)


        BSD 後來又因為一些疑似手滑別人家的 code 而遭到了質疑,部份砍掉重練(?)

        後跟乖乖一樣重新改版換包裝,各位觀眾!全新的 FreeBSD 4.4-Lite!


        目前 FreeBSD 最新 stable 版本為 9.0 和 8.3

        系上 BSD 工作站使用的是  FreeBSD 8.2-RELEASE-p3

==============================================================================

        Linux 是另一個受人喜愛的作業系統 ,由 Geek 之王,Linus Torvalds 開發

         

# Mr, Linux, King of Geeks, Dad of 3 



        Linus 大學修 OS 時,玩了一個叫作 MINIX 的 UNIX 教學版本,因為它是教育

        軟體,不准在上面玩其他有趣的事情,所以 Linus 就決定刻了一個自己的 OS

        Kernel,並且丟到了 FTP 上面引起各大牛人們圍觀,大家東改西改,加上了很

        多有用的 tools 和 patch ,Linus 決定,將新的作業系統以 GPL 授權發行 



        知名的 Linux distributions 很多, Ubuntu, Debian, Fedora, OpenSuSE ...

        Linux 是好用的 server 和 desktop 系統,大家可以嘗試看看(私心)

==============================================================================

        團子  UNIX 大家族



            各個 UNIX-like 很多,可以參考幾張經每個 UNIX 家族樹


# from wiki 

               


Linux 家族樹
# 光是 Linux 出來的分支就有這麼多 

             





==============================================================================

    1.3 why UNIX 

            為什麼我們要用 Unix 呢?老實說,這是信仰問題XD 各位可以去翻翻

            《The UNIX- HATERS Handbook》 - Simson Garfinkel 
           

            這本還有 DMR 大師為其寫序的大作,Unix sucks :P



            此外,可以比較知名 hacker esr (Eric Steven Raymond) 的大作

            《The Art of Unix Programming》 
           


            兩本相較之後,你就會明白,why Unix 這個問題


==============================================================================

 
    Some Unix Philosophy                

        UNIX is not just an operating system, but a way of life.

        Unix Is Fun to Hack

        Write programs that do one thing and do it well.
        Write programs to work together.
        Write programs to handle text streams, because that is a universal
                                                                interface.
        Small is beautiful.
        Make each program do one thing well.
        Build a prototype as soon as possible.
        Choose portability over efficiency.
        Store data in flat text files.
        Use software leverage to your advantage.
        Use shell scripts to increase leverage and portability.
        Avoid captive user interfaces.
        Make every program a filter.

==============================================================================

Ch 2.  Shell commands 

    2.1 what's shell 

    2.2 powerful shells 

    2.3 basic shell commands (and builtins) 

    2.4 more useful commands 











==============================================================================

    2.1 what's shell 

        shell 是一層讓 user 和 system kernel 溝通的那層"殼"

        Command-line shells 替 OS 提供一層 command-line interface (CLI)



        也許很多人會很不習慣用指令的方式來操控電腦,大家已經習慣滑鼠點點點

        的用法,但是,身為資工系學生,能夠在麻瓜面前啪啪啪打指令是基本的XD

        熟悉了 shell 的操控方式,很多時候工作會比圖形介面下來的簡單且輕鬆很多


        並且,shell 的一步步是可以重現的(寫成 script) 讓重複的工作可以輕鬆重用

        # vgod 大神的 Sikuli 基本上就是想做到 GUI 的 scriptting XD 


==============================================================================

    2.2 powerful shells 

        各家工作站上面提供的 shell 各有所不同

        常見的 shell 有  bash, tcsh, zsh, Bourne shell (sh) 


        bash (Bourne-Again shell) 是 Linux 上的標準 shell

        tcsh 是 BSD 上常見的 shell

        zsh (The last shell you'll ever need!) 地表上最強的 shell

        Bourne shell 最原始的 shell ,是各家 shell 的祖先



        各個 shell 基本上都完整支援 Bourne shell ,大同小異卻但各有各的超能力


==============================================================================

    2.3 basic shell commands (and builtins) 

        builtins shell commands 是 shell 為你提供的最基本的內建功能

        我們可以透過 man builtins 指令 (man for manual) 來看到各個指令提供的

        功能。

        man 是最強大也是你必須要會的指令

            man — format and display the on-line manual pages 

            在工作站上有任何問題就是問男人,他會告訴你一切有關這個指令的魔法

            man man 

                男人阿!請告訴我 man 指令的用法吧!

        註:每個 shell 提供的 builtins 略有不同

==============================================================================

    cd (change directory)

        cd 用來改變目前所在資料夾位置

            cd dir   # change to dir 

            cd       # go home ( cd 後面什麼都沒接) 

            cd ..    # 跑到上一層 

            cd -     # 時空跳轉,回到剛剛到過的資料夾 

    pwd (print working directory)

        pwd 用來知道我現在人在那邊

    echo (毫無反應,只是個 echo)

        echo Hello world!    # 印出 Hello world! 

==============================================================================

    bg (background)

        bg 用來把工作 (process) 丟到背景執行



    fg (foreground)

        fg 把丟到背景的 process 拿回來



    kill (kill a process)

        kill 送出特定 Signal 給指定的 pid

            kill -9 5566 # 送 9 號 signal (去死吧!) 給 pid 為 5566 的 process 



==============================================================================

    logout (logout)

        從你目前的 shell 登出

        a geeky way : press Ctrl-D 

            按下 Ctrl-D 會送一個 EOF 給你的 shell 代表告訴 shell 我要登出









    簡單的 builtins 到這邊,接下來是一些常用指令教學 :D



==============================================================================

    ls ( list directory contents )

        ls           # 列出目前這裡有什麼 

        ls -a        # list all 

        ls -l        # list in long format 


    mkdir (make directories)

         mkdir dir   # make the directory 'dir' 

    rm (remove directory entries)

        rm -r dir    # recursively remove everything in dir 

        rm -f file   # force remove it 

        rm -rf /     # make your computer faster !!!! 
==============================================================================

    cp (copy files)

        cp file1 file2  # copy file1 to file2 

        cp -r dir       # recursively copy everything in dir 

    mv (move files)

        mv file1 ../    # move file1 to the father directory 

        mv file1 file2  # rename the file 

        mv file{1,2}    # do the same thing 

    cat (concatenate files and print on the standard output)

        cat file1       # print file1 out 

    tac (reverse cat)

==============================================================================

    more (a simple pager for viewing a file)

        more file1     # let's see what's in file1 

    less (less is more than more)

        less file1     # similar to 'more file1' 


    both more and less are based on vi-style command keys 


    head (output the first part of files, default 10 lines)

        head file1     # print first 10 lines of file11 

    tail (output the last part of files, default 10 lines)

        tail file1     # print last 10 lines of file1 

==============================================================================

        2.4 more useful commands 

            這部份要提的是資工系學生必會的幾個好用指令

            screen          # 一個 ssh 連線,多個 user session 

            irssi           # a IRC client for UNIX 

            gcc             # GNU's compiler collection 











==============================================================================

    screen  (screen manager with VT100/ANSI terminal emulation)

        當你今天要在工作站同時做很多事情時,我們不必開多個 ssh 連線過去 

        screen 可以幫我們在本機端模擬出 login sessoin 讓我們能同時使用很多個

        shell 分別做不同的工作


        screen 最強大的功能是可以把現在的 screen detach 起來,下次從別處 ssh

        到工作站時可以把 screen 拿回來,繼續剛剛為完成的工作


        這個功能在編譯或是跑大型程式時很好用,掛 BBS 和 IRC 也很方便

        screen 功能強大,請參考 #177VUE1 #17DP0JL [P_xatier @bs2.to] 兩篇文章



==============================================================================

    irssi (a modular IRC client for UNIX)

        Internet Relay Chat (IRC) 是網路上真正高手雲集之處

        如果你想多認識世界各地的大神們,上 IRC 找準沒錯!


        irssi 是 UNIX 機器上的 IRC client

        irssi IRC 的操作方法繁多無法一一列舉,請參考這兩篇優質教學

Lzy's 用 screen + irssi 上 irc 之鄉民版教學 (含Q&A)

erepublik IRC的推廣文!   (這篇漫畫超好笑XD)


    系上在 Freenode 上面也有 IRC channel,/j #nctuNASA ILoveCSCC  # 後面是密碼
                                          /j #nctucsie     # 這邊都沒人 
                                          /j #nctucs       # 系計中ㄉㄉ專用

==============================================================================

    gcc / g++   (GNU project C and C++ compiler)

        在工作站上面寫 C code 常常會用到的標準 complier

            gcc test.c           # default complie to 'a.out' 

            gcc test.c -o test   # set the output file named 'test' 

            gcc -c test.c        # only compile (output .o files) 


        常見的 gcc flags

        -std=c99    -std=c++0x     -ansi     # using different standard 

        -Wall -Wextra -pedantic              # warning options 

        -O2 -O3 -Os -Ofast                   # optimization options 

        -lm                                  # link to the math lib 
==============================================================================

    screen, irssi, gcc 的功能強大到遠遠不只你想像的那樣,這邊給出幾個 ref 參考


       GNU Screen manual

        Irssi documentation and tutorials

        GCC online documentation



        screen 和 irssi 教學很短,一下子就唸完了

        gcc 的 manual 超精美XD 當作床邊故事來看吧 :P






==============================================================================

Ch 3.  the vim editor

    3.1 vi, vim, and emacs 

    3.2 vim starters 

    3.3 master 













==============================================================================

    3.1  vi, vim, and emacs 

        遠在古老 UNIX 時代,各個巫師都喜歡自己刻一個自己順手的 editor ,有個叫

        Bill Joy 的 hacker 發展了自己的 editor: vi 


        vi 的使用就像開車一樣,切換排檔的概念,不同 mode 底下能完成不同的事 

        常見的有 command mode 和 insert mode,分別能輸入指令和直接輸入文字


        vi 已成為 UNIX 底下比備的標準編輯器,幾乎所有的 UNIX 都可以找到 vi

        或是其衍生產品

        MJ12Net : The vi editor

       

==============================================================================

    vim, Vi IMproved 是 Bram Moolenaar 所開發的,當年他買了一台機器,可惜上面

    沒有他愛用的 vi ,於是,他就自己在這台機器上面把 vi 給刻了出來(抖) 


    vim 承襲了 vi 的特性,並加入了許多強大的功能,並具有 vim script 的高度拓

    展性,使得 vim 在 UNIX 界受到眾多 hackers 的喜愛


    vim script 讓大家可以自由的替 vim 開發 plugins ,完成更強大的功能

    更多請參考 vim.org



    功能強大的代表就是 vim 不是很好學的,請參考 編輯器學習曲線圖比較XD

   

==============================================================================

    emacs 則是是作業系統(誤)     為    [Escape Meta Alt Control Shift] 的縮寫

        在 UMIX 瀏覽器大戰中,vim 和 emacs 是敵對的文化,分別有兩派人馬支持著

        emacs 是由 GNU 創辦人, 糟老頭 RMS (Richard M. Stallman) 在 MIT 開發的


        emacs 最為人所知的是他的強大巨集拓展,以 Lisp 語言為基礎,emacs 提供了

        強大的工作環境,你能在上面做任何你想做的事,包括泡咖啡和上噗浪(?)
        http://en.wikipedia.org/wiki/Emacs#Extensibility

        emacs 特性是依賴組合鍵,平常可以任意輸入文字,加上特殊按鍵組合可以打開

        各種神奇功能 (PS. Emacs 的快捷鍵會引發腕管綜合症(誤)) 

        有這麼一句話『emacs 是強大的作業系統,只是上面缺乏了一個好的編輯器』 

        關於編輯器之神(vim) 和神的編輯器(emacs) 的戰爭,請參考
                                      wiki: Editor war
==============================================================================

    3.2 vim starters

        剛剛提到, vim 要上手非常不容易,我也不打算在這裡介紹 vim 的使用(毆)

        提供以下資源,看完的(並實際動手練習)可以學會簡單的 vim 操作 (依序閱讀)

            yannesposito: Learn Vim Progressively

            vgod: 淺談模式”mode”與文字編輯的技術與學習

           鳥哥的 Linux 私房菜: vim 程式編輯器

            大家來學VIM(一個歷久彌新的編輯器)

        cheat sheets (懶的看前面落落長文章的話)

            vgod: 給程式設計師的Vim入門圖解說明
            bullium.com : Vim Commands Cheat Sheet
            VIM QUICK REFERENCE CARD
            http://jrmiii.com/attachments/Vim.pdf 
==============================================================================

    3.3 master

        對於有時間的人,可以去翻翻去看官方的精美 vim 教學

            vim book

            vimcdoc

            Vim Tips Wiki 



        對於沒時間的人,可以玩玩看 vim 的簡單教學 (強烈建議去玩看看!!)

            在工作站上打 vimtutor 指令,花個 20 分鐘,親身體驗 vim 的有趣世界



        學習 vim 是一趟無法回頭的旅程(?) 一旦陷進去可能就會愛上這個 editor XD

==============================================================================


    本夢遊教學到這邊結束

        希望這份教學對同學們有幫助 :D

            所有不懂的東西歡迎來問我

                無論是站內信或是直接找我都可以



    最後,送你們一張很棒的 cheat sheet

       



        感謝您花時間閱讀 :D


==============================================================================


--


沒有人會為弱者流眼淚



--
▄▄▄▄   ▄▄  ▄▄▄   . .    .‘‘ .
  ▇▄█   █    ▄▄█  .  ‘ .        .          telnet://bs2.to
▄█▄█ ▄█    █▄▄  。   .‘ .


No comments:

Post a Comment