1 / 26

Shell Script

Shell Script. Sparker: 施創宏 Date:2010/7/7. Outline. Shell 簡介 Bash 介紹 Shell 變數功能 Shell Script Reference. 管理整個電腦硬體的其實是作業系統的核心 (kernel) ,所以我們一般使用者就只能透過 shell 這個使用者介面 來跟核心溝通,讓核心達到我們所想要達到的工作。 . Shell 簡介 ( 一 ). Shell 簡介 ( 二 ). Shell 的種類 : sh =Bourne Shell ( 被 Bash 取代 )

axel
Télécharger la présentation

Shell Script

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 Script Sparker: 施創宏 Date:2010/7/7

  2. Outline • Shell 簡介 • Bash 介紹 • Shell 變數功能 • Shell Script • Reference

  3. 管理整個電腦硬體的其實是作業系統的核心(kernel),所以我們一般使用者就只能透過shell這個使用者介面 來跟核心溝通,讓核心達到我們所想要達到的工作。 Shell 簡介(一)

  4. Shell 簡介(二) • Shell的種類: • sh =Bourne Shell (被Bash取代) • csh=C shell (被tcsh取代) • bash= Bourne Again shell • korn shell 和tcsh • 檢視可用的Shell • /etc/shells • 使用者所定義的Shell • /etc/passwd

  5. Bash介紹(一) • 命令編修能力 (history) • 存放位置為~/.bash_history • !行數 (可以執行該行指令) • 命令與檔案補全功能(tab鍵) • 命令別名設定功能: (alias) • 程式化腳本: (shell scripts)

  6. Bash介紹(二) • 萬用字元: (Wildcard) • && 和 ||

  7. Bash介紹(三) • bash 環境中的特殊符號

  8. Bash介紹(四) • 管線命令 (pipe) • 這個管線命令『 | 』僅能處理經由前面一個指令傳來的正確資訊,也就是 standard output 的資訊。

  9. Bash介紹(五) • 管線命令 (pipe):擷取命令 • cut :可以把你所需要的字串切出來 • Grep :可以分析字串,把我們需要的那行選出來

  10. Bash介紹(六) • sort:將資料排序 • uniq:過濾重複資料

  11. Bash介紹(七) • wc:顯示檔案有幾行,多少字(單字),多少字元

  12. Shell變數功能(一) • 我們每個帳號的郵件信箱預設是以 MAIL 這個變數來進行存取的,當 monkey 這個使用者登入時,他便會取得 MAIL 這個變數,而這個變數的內容其實就是/var/spool/mail/monkey。 • env 查看環境變數 • 變數的取用:echo • Echo$MAIL

  13. Shell變數功能(二) • 變數的設定規則 • 等號兩邊不能直接接空白字元 例如:myname = monkey • 變數名稱只能是英文字母與數字,但是開頭字元不能是數字。 • 單引號內容僅為一般字元,雙引號內容特殊字元如$可保留它原有的特性 • 若該變數需要在其他子程序執行,則需要以 export 來使變數變成環境變數:『export PATH』 • 取消變數的方法為使用 unset :『unset 變數名稱』例如取消 myname 的設定:『unset myname』 • $?(關於上個執行指令的回傳值 )

  14. Shell變數功能(三) • export: 自訂變數轉成環境變數 • 環境變數才可以讓子程序繼續引用

  15. Shell變數功能(四) • Read:讀取來自鍵盤輸入的變數 • Declare:宣告變數的類型

  16. Shell Script(一) • Shell Script就是利用shell的功能寫出來的一個程式,搭配管線命令和正規表示法等功能,去執行我們想要的一個目的。 • Shell Script好處 • 連續指令單一化 • 簡易的資料處理 • 自動化管理

  17. Shell Script(二) • 第一行 #!/bin/bash 在宣告這個 script 使用的 shell 名稱 ,其餘後面的#後面所接的都是註解。 • script 的執行方式差異 • Source script • 不會使用一個新的bash來執行腳本內容(父程序) • sh script • 會使用一個新的bash來執行腳本內容(子程序)

  18. Shell Script(三) • Shell script 的預設變數 • $# :代表後接的參數『個數』 • $@ :代表『 “$1”“$2”“$3”“$4”…… 』之意 • $* :代表『 "$1c$2c$3c$4" 』,其中 c為分隔字元,預設為空白鍵

  19. Shell Script(四) • 利用 test 指令的測試功能

  20. Shell Script(五) • 利用 test 指令的測試功能

  21. Shell Script(六) • Shell Script條件判斷式(if else)

  22. Shell Script(七) • Shell Script條件判斷式(case ..... esac)

  23. Shell Script(八) • 迴圈 • for...do...done • while do don

  24. Shell Script(九) • shell script 的追蹤與 debug • sh 參數 script.sh • -n :不要執行 script,僅查詢語法的問題; • -v :再執行 sccript 前,先將 scripts 的內容輸出到螢幕上; • -x :將使用到的 script 內容顯示到螢幕上,這是很有用的參數!

  25. Exercise • 寫一個99乘法表的shell script? • 寫一個 (7行) • 使用指令“cat”, “uniq”, “sort” 將passwd2和passwd3合併成一個檔案,重複出現的行數只出現一行,應該會有14行 * *** *****

  26. Reference • http://linux.vbird.org/linux_basic/鳥哥 • Linux程式設計 江俊龍譯

More Related