天天看點

Software Development in Unix Environment

Software Development in Unix Environment

*第一節  常用指令

本課程重點   操作指令  需要權限   i節點

提示:對本課有問題請找man (幫助文檔) 

     指令形式:man name  man -k keyword

     //  空格:一次一頁   回車:一次一行   b:回翻一頁   f:前進一頁    

     //  q:退出  /string:迅速搜尋   n:找下一個

OS  

       Hardware           Kernel          shell           People

         硬體              核心       指令解釋器     人

Kernel   

//   swap space 交換空間

//   daemons   服務程序

//   VM 虛拟記憶體 = PM 記憶體 + SWAP 硬碟

Shell

含義: 實體解釋--程式 ;   功能解釋-- 指令解釋器

分類: Bourne shell (sh) ; Korn shell (ksh) ; C shell (csh) ;

     Bourne-again shell (bash) ; tcsh (csh的擴充)

//   sh指令  =   csh 轉到 bsh

//   PTS  尾終端号

Ctrl+D  結束終端輸入

Ctrl+C  終止一個前台程式

Ctrl+Z  前台程式挂起  

//  bg %1 放入背景運作   fg %1 放入前台運作  stop %1 背景程式挂起

檔案夾

/etc/passwd   使用者名 ,UID, 主ID , 主目錄, loginshell

 more  /etc/passwd  管理注冊使用者資訊

 例如 zhouzw:x:639:1::/usr/zhouzw:/bin/csh   :分隔符  $1-$N被分隔符隔開

占位符   使用者ID  組ID 使用者主目錄 注冊shell

 clear 清屏

*Clear the terminal screen

 pwd 查找工作目錄(按絕對路徑顯示的目前目錄)

*Display the absolute pathname of the current directory(print working directory)

cd 改變目前目錄

*Change Dirrectories

*Accept both absolute(完全) and relative(關系) pathnames

cd 看目前在那個目錄

 .   Current directory目前目錄

 ..   Parent directory父目錄

 *cd [dirctory_name] 

 cd /etc/home   絕對路徑

 cd /jerry/dir1   相對路徑

 cd ../..   回到主目錄

 cd ~etc  進入etc主目錄       (./etc = etc   ./可省)

ls 列出制定目錄裡的内容

*Displaying a Linsting of Files and Directories           放在目錄的date block裡

 ls -a  顯示目前目錄下所有檔案和目錄,包含隐含檔案(以.開頭)

 ls -d  把目錄像檔案那樣顯示,不顯示目錄下的檔案

 ls -i  顯示目錄和檔案占用的i節點号 (i-node number)   檔案名和i節點的對應關系

 ls -t  按時間排列

 ls -l  顯示目前目錄下詳細資訊(按檔案名排序)       除了檔案名以外的關于這個檔案的

 ls -lu 按照最後通路時間排序                      所有屬性,以及date block的位置

 ls -lt 按照最後修改日期排序

 ls -F   (* 可執行檔案 ; .../  目錄 ; none 文本檔案 ; = Socket ; @ 符号連接配接 ; | 管道名稱)

 ls -R  顯示目錄下所有檔案,包括子目錄(檔案管理器中點開所有+号)

 ls -ld 列出目前目錄的權限

file filename 檢視檔案詳細屬性及檔案關聯

如: 147975  -rw-r--r--  2  zhouzw   other   13    6月27  13:47  dir3

   i 節點     權限          使用者  使用者組  檔案大小  檔案修改日期  檔案夾

      // 數字減2表示他下面子目錄的數量(2表示.和..)

檔案權限 

r 讀(檢視檔案内容) ; w 寫(修改檔案内容) ; x 執行(運作腳本) ; - 沒有權限

      檔案類型

如: - rw- r-- r--                         t

擁有者u ;同組人g ;其他人o ;所有人a     chmod +t  (chmod 1777) 賦予t權限

  權限                                (僅限/tmp目錄在777權限下增設)

touch fileneme

*Create new, empty files or update access and modification times

建立新的空檔案,如果檔案存在,則更新檔案(在使用者主目錄下無寫權限)

// 建立或删除目錄 必須有寫權限和執行權限

chmod 修改權限  (root和owner才有此權限)

*Change permissions

*Symbolic mode uses combinations of letters and symbols

// file1指檔案或目錄 g=group ; u=user,owner ; a=all ; o=other 

// +(-) 在現有權限上增加(減少)權限  = 指派

 chmod g-r  file1

 chmod u+x,go+r file1

 chmod a=rw file1

// g=group ;u=user owner ;o=other ;a=all ;+- 在現有權限上追加(減少)權限 ;=指派

權限

Read  4  ;   Write  2  ;  Execute  1 

7    rwx  ;  6    rw-  ;  5    r-x  ;   4    r--

3    -wx  ;  2    -w-  ;  1    --x  ;   0    ---

//   rwxrwxrwt  (t為粘貼位 權限為 1777  )

mkdir 建立目錄

*Create directories using either an absolute or a relative pathname

*Must have the appropriate permissions to create a directory

 //  -p 建立新目錄時,其父目錄不存在時先建立父目錄

 //  -m 建立新目錄的同時制定其權限

 * mkdir [-p] d_name 

 mkdir dir1/dir2 

 mkdir -p dir6/dir7/dir8 

 mkdir dir3 dir4 dir5  建立兄弟目錄

 mkdir ~/games  主目錄下建立目錄games

cp (copy) 檔案備份 檔案拷貝

//   -i 當目标檔案存在時,系統提示是否拷貝

//   -f 當目标檔案存在時,系統不給提示直接拷貝

//   -r 拷貝這個目錄及目錄裡的所有檔案和子目錄

*Copy files

 * cp [-i] source_file destination_file

 * cp [-i] source_file(s) destination_directory

 cp beans apple   (beans必須存在)

 cp beans apple dir2

 cp -i beans apple

*Copy a directory and its contents to another directory

 * cp -r source_directory(s)  destination_directory

 cp -r dir3 dir4

 cp -r dir3 dir4 /tmp

rm 删除檔案(此指令為徹底删除連結)   rmdir (隻删空目錄)

*Remove files

//  -f 删除檔案時不需确認

//  -i 删除檔案時需要确認

//  -r 删除指定目錄及其子目錄,和包含的所有檔案

 * rm [-i] filename(s)

 rm file1 file2 file3

 rm -i file4

*Remove Directories

//  -p 删除指定目錄上的所有目錄

 * rmdir directory_name(s)

 * rmdir -r[i] directory_name(s)

 rmdir empty_dir

 rm -r dir6

 rm -ir ~/dir7

mv

*Move or rename files and directory

// -i 移動時需要确認

// -f 覆寫前永不提示

 * mv [-i] source target_file

 * mv [-i] source target_directory

 mv nuts beans

 mv /export/home/jerry/brands -

 mv dir5 dir6              

                      可以是檔案,可以是目錄

ln  連結指令(連接配接檔案或目錄)   給系統中已有的檔案指定另一個名稱

*Create alternate names or aliases for files and directories

*Two kinds of links,hard and symbolic (or soft)

// -s 符号連結(軟連結)   先找源,确定源的i節點,然後連結   

// -d 硬連結

 * ln [-s] source_file target_file

//  根的bin是使用者的bin的源檔案  bin --> ./usr/bin

cat 在螢幕上顯示檔案,連接配接兩個或多個檔案

*Displays the contents of a text file on screen

*Is often used to join two or more files into one large file

 cat filename(s) 

         清空一個檔案的内容

 cat /dev/null > ~/b.txt  

 cat /etc/passwd | awk -F: '{print $1 "/t" $6}' /

    | sort > ~/userinfo

          排序            制表符   續行必須使用/

more  在螢幕上顯示檔案并分屏

*Displays the contents of a text file one screen at a time

 * more filename(s)

//  b   Move back one screen

//  h   Display a Help menu of more features

//  q   Quit and return to the shell prompt

//  f    Move forward one screen

//  n   Find next occurrence of string

 Spacebar   Scroll to the next screen

 Return     Scroll one line at a time

 /string     Search forward to the shell prompt

head  &  tail

*Displays the first or last n lines of one or more files

*Displays first or last 10 lines by default 預設10行

 *head [-n] filename(s)

 *tail [-n] filename(s)

tail -30 /usr/dict/words

特殊符号

Asterisk(*)

Represents zero or more characters  代表0或者更多字元

ls d*  

Question mark(?)

Represents any single character  任何與此相符的條件

ls d?  ls z?.txt  

Square Brackets([ ])

Match a set or range of characters to a single character position

ls [b-f]b*  ls [fF]f*a?c 

Semicolon(;)

Enter multiple commands on a command line 輸入很多指令并同時執行

cd;ls  date;cal;pwd 

Angle Brackets(>,<,>>)   輸入重定向

Use(>) to redirect the output of a command to a file rarher than to the screen

Use(<) to redirect the input of a command from a file rather than from the keyboard

Use(>>) to append the output of a command to an existing file

cal 10 2004>a.txt    把一個指令的輸出覆寫到已存在的檔案裡

cal 11 2004>>a.txt    把一個指令的輸出添加到已存在的檔案裡

cal<f1.txt>f2.txt   把f1的内容重定向到f2裡去

Piping ( | ) 管道: 把一個指令的輸出傳給下一個指令當作輸入

Takes the output of one command and passes it as input into a following command

cal 7 2003 |  lp         列印      分流     追加

ls -l /etc | more 

ls -l /etc | tee  -a  etc.lst   在檢視的同時儲存

VI - Full Screen Text Editor

              Return                           Escape

底行模式                      指令模式                        輸入模式

Invoking  vi

vi filename   Open or create a file

vi      Open a new file to be named later

Positioning

方向調整  上/下/左/右      k / j / h / l  或者小鍵盤方向鍵

行列調整  行尾/行首/檔案尾/檔案首/到第20行   $ / ^ / G / 1G / 20G

翻滾調整  到螢幕底/到螢幕中/到螢幕頂/往下翻頁/往回翻頁   L / M / H / ctrl + F / ctrl + B

Input

i 在光标目前位置插入

a 在光标所在位置插入

o 在光标所在行下插入一空行

O 在光标所在行上插入一空行

<Esc> 退出插入狀态

Deleting Text

x 删除一個字元

dw 删除目前詞

3dw 删除三個詞

dd  删除目前行

5dd 删除5行

:5,10d  删除第5行至第10行

Changing Text

r 替換一個字元

cw 替換一個單詞

cc  替換一行

C  替換從光标至行尾

Copying and Pasting Text

yw   拷貝詞

yy   拷貝行(或Y)

p    目前行下粘貼

:1,2co3  拷貝行1,行2在行3之後

:4,5m6   移動行4,行5在行6之後

Saving and Quitting Files

:w  存盤

:wq 存盤退出VI(或ZZ,或:x)

:q!  強行退出不存盤

:w newfile  存成新檔案

Advanced Editing Options

-  改變大小寫

J  把目前行和下一行連起來

u  廢除剛才的編輯操作(undo)

:set nu  顯示行代碼(:set nonu)

:21  光标停在指定行

21G  光标停在指定行(G到檔案尾,1G到檔案頭)

/串  從目前行往下查找

?串  從目前行往上查找

n   查找繼續

:r file2  在光标所在位置插入另一個檔案

:1,$s /舊串/新串/g   替換全文(或 :%s/舊串/新串/g )

   替換                                  全換

Finding Things on Unix

* /usr/bin , /usr/sbin , /usr/ucb , /usr/local/bin , ...

id

*Displays the user name corresponding ro the effective user ID

 * id [option(s)]

id

id root

id -a root

user  目前有那些使用者登陸

*Display a compact list of users logged in

user 

Finding People

*Displays information about all users currently logged on to the local system

who   登陸者詳細資訊

w

*The who am i command displays information about your real user ID

who am i    我的登陸資訊

*The whoami command displays only the login name of the effecctive user

whoami   我的登陸名字

find   在目錄下找檔案

*Find files based on specific criteria, then execute a command on the matching files

 *find path expression [action]

find / -name perl   精确比對          +400  大于400個block

find .-mtime 10 -print  可省   從此往前第10天 (-10  10天内 )

find /etc -user 0 -size +400 -print  找出owner大小大于200k的檔案( 1 block = 512 byte位元組)

find ~ -perm 777> ~/holes  找出主目錄下權限為777并把結果放入檔案裡

*find /export/home -type f -atime +365 -exec rm {} /   找出一年内未通路的檔案并删除

  權限        f 類型   通路時間         此符号為寫作要求

     rm 無效時可用此指令

grep  在檔案裡找出指定的文本,并把包含此文本串的所有行列印在螢幕上

*Searches a file for a specified text string and prints all lines contain that pattern to the screen

 - case sensitive

 *grep [option(s)] string filename         -i  忽略大小寫

 _  -i lgnore case of string when searching

 _  -v Search for all lines that do not match string

 *grep root /etc/passwd    找root使用者的注冊資訊

 ls -la | grep -i 'sep 1'    列出最後修改日期是9月1号的檔案

 ls file*|xargs grep dd    

      在檔案内容裡查找,無則在目錄裡查找 (局部傳輸可用,無則整體傳輸)

     等價于grep dd file* 

wc   統計字數

*Displays a line , word , or character count of a file

 *wc [options] filename(s)

_  -l Counts lines

_  -w Counts words

_  -C Counts characters

 grep wang  /etc/passwd | wc -l  查找wang從passwd檔案中并統計其字數列印到螢幕

du   顯示目錄或檔案大小

*Summarize disk usage

 du -sk * 隻顯示目前目錄 (s 僅顯示總計  k 以1024bytes為機關)

ps  檢視程序

*List the processes currently running on the system

/usr/bin/ps [-options]

-e  List every process now running     每一個程序

-f  Generates a full listing    全清單形式

-U uidlist  Processes whose teal UID ro login names are given

*ps -ef | grep telnet

*/usr/ucb/ps -auwx

  //   TTY  終端号       demon 精靈程序

  //   grep -v grep  不包含自身  (-v 不包含)

kill

*Terminate unwanted command processes that you cannot quit in the normal way

 *kill [-signal] process-id

kill 12932

kill -9 12418   //需要owner權限     -9 強制殺死程序

sleep 1001 &

pkill sleep

Job Control

*Ability to execute several jobs simultaneously

fg %n     Place a job in the foreground    放入前台運作

bg %n     Place a job in the background   放入背景運作

stop %n   Suspend a background job      背景程式挂起

kill %n    Abort the specified background job

Control-c   Abort the foreground job

Control-z   Suspend the foreground job

fg   ctrl+Z    stop    bg    bg     stop   stop 

                                 fg       fg 

Setup Initialization Files

Environment Variables  環境變量

常用路徑 /usr/sbin  ;  /usr/bin ; /usr/ucb ; /usr/local/bin

Eeatures of Initialization Files

*Initialization files contain commands and variable settings that are executed when a shell is started

*Two levels:

  _  System-wide - Teside in the /etc directory                   更改cshrc步驟

  _  User-specific - Teside in a user's home directory              進入使用者主目錄

$HOME 表示變量  目前使用者主目錄                          vi .cshrc 進行編輯

  source .cshrc  進行激活

Shell System-Wide (Read First) User-Specific (Read Second) 

Bourne /etc/profile $HOME/.profile 

C /etc/.login $HOME/.login

$HOME/.cshrc 初始化環境變量檔案

$HOME/.logout 

Templates of The Initialization Files

*./etc/skel/local.profile , local.login , local.cshrc

 $HOME/local.profile , local.login , local.cshrc

Contents of The Initialization Files

*Set the default file creation permissions

*Set environment variables

*Set the default prompt

*Set the command path for the default command path

*Set the variable TERM for the default terminal type

*Set up custom commands

Shell Switches

ksh      Invoking Korn Shell

sh       Invoking Bourne Shell

csh      Invoking C Shell

bash     Invoking Bourne-again Shell

Environment Variables

setenv variable value     系統預設建議

setenv OPENWINHOME /user/openwin     使用者自定義(包括使用者定義以及應用軟體設定)

Quoting in Shells

% setenv WTO `uname-n`     當指令解釋

% echo $WTO

% echo "$WTO"   變量

% echo '$WTO'    字元串

% echo /$WTO

PATH

set path=(... ... ...)

set path=($path pathname...)

% echo $PATH

% echo $path

*Which

 *Display the pathname leading to an accessible command based on your search path

 *which vi

*where is

 *Displays all directories where desired command is located, including appropriate man page

 * /usr/ucb/whereis vi

History

*Displays previously entered commands typed in the C shell to the screen

 * set history=30

% history

% history 5

% !n

% !!

Aliases 别名

*Substitute a short command for a long one

*Create a single command for a series of commands

*Create alternate forms of existing commands

*Change a command set

 *alias alias-name value

% alias h history

% alias c clear

% alias home 'cd;ls'    特殊字元 空格需加單引号

% alias ls 'ls -l'

% alias copy 'cp -i'

% alias

% unalias copy

Custom Prompts

set prompt= value

set prompt="`hostname`: $cwd %"

An .cshrc Example  變量 系統變量 應用軟體變量

# Starting .cshrc ...

umask 022

setenv CC /usr/local/bin/gcc

setenv CCC /usr/local/bin/g++

setenv ORACLE_BASE /tarenaora

setenv ORACLE_HOME ${ORACLE_BASE}/software/9.0.1

setenv ORACLE_SID TARENADB  我要連接配接的資料庫

setenv JAVA_HOME /usr/j2se      MAN要用的指令的搜尋路徑

setenv MANPATH /usr/share/man:/usr/man:/usr/share/catman:/usr/catman

setenv LD_LIBRARY_PATH /usr/lib:/usr/dt/lib:${ORACLE_HOME}/lib

setenv EDITOR vi         動态連結庫

set path=(/usr/j2se/bin /usr/bin /usr/local/bin /usr/ucb /usr/ccs/bin /etc.)

set history=100

set prompt="`hostname`!%"

alias || "ls -l | more"

alisa make /usr/bin/make

Shell Scripting

*Script 1:

vi a.sh

   #!/bin/sh

   pwd

   cal

   date

chmod +x a.sh

*Script 2: lcf ( lowercase filenames)

#!/bin/sh

# lowercase any filenames with uppercase chars   

for oldname in $* 從這裡取值 $*是一個位置參數

do

if [ -f $oldname ]

then

newname=`echo $oldname | tr "[A-Z]" "[a-z]"

if [ $oldname != $newname ]

then

mv $oldname $newname

fi

fi

done

# 請注意空格

* crontab -l

* crontab -e   做這個之前先在.cshrc設定中加上editor

   - setenv EDITOR vi   排程vi

* crontab -r

* mailx

       分鐘 小時 日期 月份 星期

0 2 *** a.sh     排程 (在某個特定時間打開某個檔案或程式)

13 5 13 * 5 b.sh

0,30 8-17 ** 1-5 c.sh  每隔30分鐘

***** d.sh

兩種shell設定變量的方法

csh

.cshrc

setenv 變量名 變量值

source .cshrc

                            這兩個步驟等同

bash  

.bash-profile

LANG=C export  LANG

source .bash-profile

/usr/ucb/ps -auwx | more

輸出結果

user   pid    %cpu    %mem    sz   rss    tt   s     start    time     command

            (cpu 記憶體)-使用率                 狀态