1 / 13

Shell

Shell. Shell 是一個 interpreter( 翻譯者 ) 的介面程式 , 負責將 user 的指令解讀並送到 kernel 去處理 . Unix Shell 有二大主流 : Bourne Shell 即 /bin/ sh 有 # 提示符號 GNU Bourne Again 即 /bin/ bash 另有 ash , ksh(korn shell) , zsh C Shell 即 /bin/ csh 有 % 提示符號 另有很受喜愛的 Tenex C Shell( tcsh )

dieter
Télécharger la présentation

Shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell • Shell是一個interpreter(翻譯者)的介面程式,負責將user的指令解讀並送到kernel去處理. • Unix Shell有二大主流: • Bourne Shell即/bin/sh有#提示符號 • GNU Bourne Again即/bin/bash • 另有ash, ksh(korn shell), zsh • C Shell即/bin/csh有%提示符號 • 另有很受喜愛的 Tenex C Shell(tcsh) • 練習: (本學期Lab預設的shell是tcsh, 未來run程式將以 • bash為主) • 請執行 /bin/bash (暫時改設為bash shell) • 請執行 /bin/tcsh (暫時再改設為bash shell) • 請執行 exit 或按 ^d (離開tcsh) • 請執行 exit 或按 ^d (離開bash)

  2. Shell 的重要特性 • Shell除了解譯之外,有七大重要的特性,這些特性讓user便利操作Unix(Linux)系統.這些特性如下: • Shell interactive (prompt,history) • Background execution (加入&) • Redirection (> 或 <) • Pipes (|) • Wild-card characters (*或?) • Environment variables ($HOME, PATH) • Shell Scripts (就是利用指令去做像寫程式的事,類似DOS的batch file,整合一堆指令並依序執行以達成特定的功能)

  3. Shell 的內建功能-1 • ~和 $HOME均代表使用者的home 例: cd ~ 或 cd $HOME • .目前的目錄, 可用pwd顯示出來 • .. 上一層的目錄 • [a-z,1-9]比對或找尋字的範圍, 例如: mv f[1-4] /DIR1 • 又例如: rm f[1,3,5] • *代表零或多個任意字元,例如: ls *file* 或 cp f* /DIR1 • ?代表一個任意字元, 例如: ls f? 或 ls ??.txt • !n執行history第n個指令,如: !3(執行history中第3個指令) • !!重覆上一個指令,亦可再附加選項(option)及參數 • (argument),如: 先打 ls *.txt 再打 !! 或 !! *.txt • ^xxx^yy^將上面的指令中有xxx的地方,改為yy

  4. Shell 的內建功能-2 • > 輸出導向,指令執行後的結果轉輸出到… • < 輸入導向,指令執行時需要輸入…… • >& 錯誤輸出導向,指令執行如有錯誤訊息轉輸出到… • ; 用來分隔指令,例如: ls ; cat a.txt • () 同一般括號作用即先集合指令執行, 例如: • (cd ~/DIR1; tar zcvf - *)|(cd /tmp ; tar zcvf -) • alias yy=‘xx‘(bash shell) 訂定別名(綽號),如果只下 alias 則會列出所有己設定的 alias 內容 • bash範例: alias rm=“rm –i”又例如: alias l=“ls -alF” • \ 暫時取消alias的設定, 例如: \ls 或 \rm

  5. Environment Variables • 在Shell下有三種variables: • Environment variables: 通常做為預設的shell環境設定變數值 • User-created variables:由user自訂的變數 • Positional parameters: 指定位的參數 • 常用的Environment variables有:(有些shell均用小寫) • HOME: pathname of your home directory • PATH: directories where shell is to look for command • TERM: The termcap code for your terminal • USER: your user name • UID: your user id • MAIL: pathname of your system mailbox • SHELL: pathname of shell

  6. Setting Environment Variables • 練習: (加上$符號表示是指內容而非其名稱) • echo $TERM (試試看 echo TERM有何不同) • echo $PATH • set (顯示所有環境設定變數) • 請同學自行練習echo所有的設定 • 請同學將path的設定內容抄下,並解釋其內容 • set history = 20 • alias m=mail (請自行練習其它別名… alias h history) • echo $user • set user=DoLAMon • echo $HISTSIZE

  7. 例: (請將下面指令打在一 個檔案內,檔名自訂) # A simple shell script-1 #!/bin/bash cal date who | less 例: # shell script-2 #!/bin/bash who | grep 90| cut –b 1-8 | sort| uniq -c echo ‘login check’ date Shell Script • Shell scriptis a program consisting of shell commands. • 通常我們利用編輯器來編寫shell script為一個檔案,其中每行的第一個字若為#,則為註解,#!則為指定shell使用的目錄) • 要執行寫好的shell程式有下列幾個步驟: • (1)設定script檔為可執行,即更改 • 權限為 chmod u+x 檔名 • (或用 chmod 755 檔名) • (2)要執行spcitp檔案時請打 • /bin/bash 檔名 • (3)因bash的script較普遍,往後 • script練習將以bash為主,請確 • 認您login後在bash下.

  8. Shell Script 基本語法 先編修檔名: echo-4a • varName=value • Var=`expr int-var1 +-*/% int-var2` • 變數符號:舉凡有加$符號的即為變數, 例: $abc,$home,$kkk,但如果$後面加數字,如$1,$2,$3…,則表示第n個參數 • $n:指第n個參數,例:$1,$2,$3… • $0是指程式自己本身 • $*:指所有參數 • $@:指所有參數但不含有加引號的字串 • $#:指參數個數 • $?:指上個命令傳回的參數值 • $$:指目前此shell的pid • #?var:測試是否有設定var這個變數,0 表示沒有設此var變數 • $<:指從標準輸入設備讀取資料 #!/bin/bash #positional parameters, i.e., $n echo $0 $1 $2 $3 $4 要測試上面的檔案請先: chmod u+x echo-4a (或者可用 chmod 755 檔名) 接著執行這個檔名加上參數 ./echo-4a I will do this 檔名: echo-all #!/bin/bash #positional prarmeters, i.e., $* echo you had type $# argument echo they are: $*

  9. Shell Script 語法練習 檔名: add 1.打字時,注意運算式的 引號為倒引號 `, 運算參數間必需要加上空白格 2. 請記得每個檔案都要先改權限, 即chmod 755 檔名後, 再測試你的shell script 檔案 3. 接著將左邊的檔案做執行動作 ./add 3 5 (或 sh add 3 5) ./add-1 (或 sh add-1) 3. 如果您的檔案內容都照打無誤, 但執行時仍有錯誤訊息出現,則可能是你登入時被設定為tcsh, 所以可以再加run下面的指令,將shell改成bash的shell來執行 /bin/bash #!/bin/bash #add two numbers sum=`expr $1 + $2` echo “$1 + $2 = $sum” 檔名: add-1 #!/bin/bash a=5 b=9 sum=`expr $a + $b` echo $a + $b = $sum 檔名: add-2 #!/bin/bash read a read b sum=`expr $a + $b` echo “$a+$b=$sum”

  10. Shell Script 四則運算練習 檔名: add-4op 1.注意, 加 減 乘 除的運算式中,其中 乘 和 除 二個運算元均為shell的特殊字元, 故必須再加上逸敘字元(escape char.) 2.執行 /bin/bash add-4op #!/bin/bash a=20 b=4 sum=`expr $a + $b` echo “$a+$b=$sum”; echo sub=`expr $a - $b` echo “$a-$b=$sub”; echo mul=`expr $a \* $b` echo “$a*$b=$mul”; echo div=`expr $a \/ $b` echo “$a/$b=$div”; echo

  11. Shell Script 迴圈語法-1 這裡的 -f是測試檔名的選項 • 常用的測試及迴圈語法類別 • (1) if (2) case • (3) for (4) while • (5) until • if conditional expr • then • command(s) • fi #!/bin/bash echo “input a file name:” read ff if test -f $ff then echo “file exist” else echo “file does not exist” fi #!/bin/bash if test $1 = “週五” then echo “Thank God it’s Friday” elif test $1 = “週六” || test $1 = “週日” then echo “Have a nice holiday!!” else echo “Go to work….Just do it…” fi if conditional expr then command(s) elif conditional expr then command(s) else command(s) fi

  12. Shell Script 迴圈語法-2 #!/bin/bash for name in $* do finger $name done • for var in list • do • command(s) • done #!/bin/bash count=10 until test $count –eq 0 do echo $* count=`expr $count – 1` done • until condition • do • command(s) • done #!/bin/bash count=10 while test $count –gt 0 do echo $* count=`expr $count – 1` done • while condition • do • command(s) • done

  13. Shell Script 迴圈語法-3 case word in pattern1) commands ;; pattern2) commands ;; pattern3) commands ;; ………… patternN) commands ;; *) commands ;; esac #!/bin/bash set `date` case $1 in 週五) echo “Thank God it’s Friday”;; 週六 | 週日) echo “Nice Holiday”;; *) echo “Work hard….Just do it”;; esac • #!bin/bash • cd / • tar zcvf mybackup.tar.gz ~/ • mv mybackup.tar.gz $HOME/backup • mail –s “backup complete!!” $USERNAME < backup #!bin/bash for i in /etc/profile.d/*.sh; do if [-x $i]; then $i fi done

More Related