堅持知識分享,該文章由Alopex編著, 轉載請註明源地址:
http://www.cnblogs.com/alopex/
索引:
- 什麼是shell
- shell的分類
- shell腳本的執行方式
摘要:
- 瞭解shell的本質是什麼
- 瞭解除bash這個shell以外,還有哪些shell
- shell 腳本以及shell腳本的執行方式/區別
![](http://static.javashuo.com/static/loading.gif)
什麼是shell
通常來講,操做系統由用戶接口(User Interface)和系統內核(kernel)兩部分構成
從名字能夠看出,內核是操做系統的核心, 它是一個操做系統的靈魂
內核的主要做用有如下幾點
- 文件管理 (File manager) 協調調度計算機中的大容量存儲
- 設備驅動 (Device drivers) 使接入計算機的周邊設備能相互通訊
- 內存管理 (Memory manager) 協調調度計算機中的內存
- 進程調度 (Scheduler) 協調調度系統中的進程
- 時間調度 (Dispatcher) 給予進程CPU的使用時間
- 系統調用 (System calls) 進程請求系統內核去執行一些它(普通進程)沒有權限執行的命令
- 簡單歸納, 內核負責管理計算機硬件資源(CPU, 內存, 硬盤, 周邊設備I/O)的使用
內核是如此的重要, 它對系統的穩定高效運行起到相當重要的做用.
因如此, 操做系統是不容許用戶直接和內核進行交互的
它(OS)給予用戶另一個對操做系統訪問的方式, 被稱爲用戶接口(UI)
專門用於系統和用戶進行交互
早期的計算機中,操做系統只有命令行界面(command-line interface) 簡稱爲CLI
在英文單詞中shell的中文翻譯爲 [殼] 表示的是用堅硬的東西保護保護內部柔軟部分
用SHELL來描述命令行界面, 這個 "時髦"的說法 最先(1964 - 1965)誕生於 Multics 其中一位研發者Louis Pouzin
關於 SHELL 起源的故事, 能夠點擊這裏瀏覽更多
http://www.multicians.org/shell.html
感謝Louis Pouzin 爲CLI 賦予了一個形象生動的名字 : )
- 簡單地歸納, shell 本質應該是CLI, 它是一個讓用戶經過命令行來實現和系統交互的接口.
shell的分類
操做系統: CentOS release 6.9 (Final)
命令提示符 PS1 : \[\e[31m\]>\[\e[m\] (下圖中紅色的> 爲命令提示符)
來介紹shell的分類以前, 先來介紹一下關於shell查看的幾個經常使用操做
查看系統默認的shell
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
查看系統中擁有的shell
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
查看bash的版本
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
2014年9月, bash曾曝出一個嚴重的漏洞CVE-2014-6271 html
ShellShock Bug (特製環境變量注入攻擊)
咱們使用如下命令對系統進行測試
env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
若是測試的結果只顯示了 "this is a test" 表示你的系統正常, 不然你須要對bash打補丁 (如下僅針對CentOS/redhat)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
Linux shell的大類有如下兩種 Bourne shell 以及 C shell
首先介紹 Bourne shell
Bourne shell 家族中有三大成員, [sh, ksh, bash]
[SH]
sh 其的開發者是 Stephen Bourne.
它在 V7 Unix 中做爲默認shell 替代了以前使用的 Thompson shell
有意思的是,sh設置sh的許多特性依然沿用至今
Features of the Bourne shell versions since 1979 include:
shell
- Built-in
test
command – System III shell (1981) - # as comment character – System III shell (1981)
- Colon in parameter substitutions "${parameter:=word}" – System III shell (1981)
continue
with argument – System III shell (1981)cat <<-EOF
for indented here documents – System III shell (1981)- Functions and the
return
builtin – SVR2 shell (1984) - Built-ins
unset
,echo
,type
– SVR2 shell (1984) - Source code de-ALGOL68-ized – SVR2 shell (1984)
- Modern "
$@
" – SVR3 shell (1986) - Built-in
getopts
– SVR3 shell (1986) - Cleaned up parameter handling allows recursively callable functions – SVR3 shell (1986)
- 8-bit clean – SVR3 shell (1986)
- Job control – SVR4 shell (1989)
- Multi-byte support – SVR4 shell (1989)
[Ksh]
Ksh 全稱是 KornShell , 其研發者是 David Korn
它最初的開發是基於Bourne shell的, 所以隸屬於 Bourne shell的陣列中
與 sh相比, 它擁有如下的特性
- Job control, command aliasing, and command history designed after the corresponding C shell features. Job control was added to the Bourne Shell in 1989.
- A choice of three command line editing styles based on vi, Emacs, and XEmacs.
- Associative arrays and built-in floating point arithmeticoperations (only available in the ksh93 version of KornShell).
- Dynamic extensibility of built-in commands (as of ksh93)
[bash]
bash 的全稱是 Bourne-again shell, 其開發則是GUN計劃的 Brian Fox
目的是爲了替代 Bourne shell
它是Linux衆多發行版和apple (最先是 OSX) 默認的shell, 現現在微軟Windows10也有對其的支持
雖然bash是用做sh的替代品, 可是bash並不能徹底兼容sh, 這是須要注意的.
在CentOS 6.9 中, sh 是一個鏈接到 bash 上的鏈接文件, 所以使用sh 命令和 bash命令效果是同樣的
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
C Shell 家族有兩個成員 [csh, tcsh]bash
[csh]
csh 其設計者是 Bill Joy
因爲csh擁有交互的特性, 在1980年, csh以其易於上手的特性, 贏得了青睞
csh的設計理念是使得它開起來更像是 C語言, 而且擁有更好的用戶交互性
Bourne shellapp #!/bin/shif [ $days -gt 365 ]then echo This is over a year. fi |
C shell函數
#!/bin/cshif ( $days > 365 ) then echo This is over a year.
endif
|
以上這個例子, 能夠說明csh 與 bash 中腳本書寫的差別性
就以上的腳本而言, csh的可讀性略勝一籌
[tcsh]
因爲sh有進階版的bash, csh也有其進階版,名字就叫tcsh
它是BSD 相關係統的tcsh
簡單整理一下
shell 有兩大陣型 Bourne shell 與 C shell
Bourne shell (sh) --> Korn shell (ksh)
Bourne shell (sh) --> Bourne again shell (bash) 總多Linux發行版上最流行的shell
csh --> tcsh 現在在BSD系統上最流行的shell
shell 腳本的執行方式
![](http://static.javashuo.com/static/loading.gif)
# 執行的腳本
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
[第一種方式 sh script.sh]
這是一種十分經常使用的 shell 腳本執行方式. 由於它不須要用戶給予腳本執行權限
sh 命令接收 script.sh 這個文件做爲傳入參數, 開啓一個子shell進行命令的解析與執行
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
[第二種方式 source(.) script.sh]
這種方式, 也不須要用戶給予腳本執行權限, 腳本將直接在當前shell中執行
這意味着, 在script.sh 中定義的變量或函數, 在腳本執行結束後, 變量的值或函數返回值等將被保留
(須要注意的是, "." script.sh 這種方式只能在 bash 類型的shell中執行
而 source script.sh 既能在 bash類型 也能在 csh 類型的shell中執行)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
[第三種方式 ./script.sh]
這種方式, 用戶必須賦予腳本執行權限(chmod a+x script.sh)
才能運行腳本, 該方式也是經過子shell對命令進行解析與執行
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
[關於 子shell 和 父shell]
簡單來講,子shell和父shell的關係有以兩點
1.子shell 會複製 父shell的變量和函數, 就如同子女繼承父母的基因同樣,
2.子shell中設置的變量和函數只有子shell本身知道, 子shell沒法改變父shell設定的變量和函數
腳本執行中, 若是使用 source / . 方式執行腳本, 命令會於當前shell中解析執行
下面用一個簡單的腳原本體會一下, 當前shell中執行腳本, 和子shell執行腳本的取本
腳本內容
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
使用當前shell執行完cd命令後, 當前shell的目錄發生了變化
![](http://static.javashuo.com/static/loading.gif)
使用子shell執行完cd命令後, 當前shell的目錄並無發生改變
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
------------------------------
參考資料:
// Computer Science: An Overview (12th Edition)
// Kernel (operating system)
// The Origin of the Shell
// Command-line interface
// Why was the word 「shell」 used to descibe a command-line interface?
// Bash specially-crafted environment variables code injection attack
// What is the ShellShock Bug? (September 2014)
// ShellShock Attack Demonstration
// Bourne shell
// KornShell
// Bash (Unix shell)
// C shell
// tcsh
// Different ways to execute a shell script