菜鳥的《Linux程式設計》學習—shell script

來源:互聯網
上載者:User

標籤:linux   shell   bash   

1. 認識shell scriptshell script是利用shell的功能縮寫的一個“程式”,這個程式是使用純文字檔案,將一些shell的文法與命令(含外部命令)寫在裡面,搭配Regex,管道命令與資料流重新導向等功能,以達到我們想要的處理目的。
shell script有很廣泛的應用:(1)自動化管理的重要依據(2)追蹤與管理系統的重要工作(3)簡單入侵檢測功能(4)連續命令單一化(5)簡易的資料處理(6)支援跨平台
所以說,shell script用在系統管理上面是很好的一項工具,但是其也有一定的缺點,在處理大量的數值計算時,速度較慢,佔用CPU資源較多,會造成主機資源分派不良。
2. shell script程式編寫對於shell script程式的編寫,使用編輯工具vim,這是一個很好用的文本編輯命令。首先,使用bash命令,建立一個指令碼程式
[[email protected] shellscript]$ vim helloworld.sh//在開啟的文本中,編輯程式,helloworld.sh#!/bin/bash#Program:#       This is my first shell script program. It will show "Hello World!" on#       the screen.PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "Hello World! \a \n"exit 0
程式內容解析:(1)第一行 #!/bin/bash 是聲明這個script所使用的shell名稱,每一個shell script指令碼內容,必須在第一行進行對此聲明。也就是說,我們使用的是bash命令,必須聲明這個檔案,來使用bash的文法;(2)除了第一行外,以#開頭的語句均是批註的作用,也就是類似於普通C程式中的注釋;(3)主要環境變數的聲明,鳥哥建議,PATH與LANG是最重要的,如有使用,建議務必將一些重要的環境變數設定好,這樣可以讓程式進行時,直接執行外部命令,不必寫絕對路徑;(4)主要程式內容,在本檔案中只有echo這一行;(5)告知執行結果,利用exit命令,讓程式中斷,並回傳一個數值給系統,0值代表成功,非0值則是代表錯誤;3. 程式的編譯運行對於shell script有兩種運行方式:(1)使用 sh命令執行,
[[email protected] shellscript]$ sh helloworld.sh Hello World!  

(2)使用chmod改變檔案許可權,直接用./helloworld.sh 執行
[[email protected] shellscript]$ chmod +x helloworld.sh [[email protected] shellscript]$ ./helloworld.sh Hello World!  

我們看到,以上兩種方式,都可以順利成功的將指令碼和helloworld.sh執行完畢。4. 兩種編譯方式詳解(1)利用直接執行的方式來執行指令碼:在子進程中運行(2)使用source來執行指令碼:在父進程中執行下面用一個實際例子詳細說明:我們建立一個新的指令碼test.sh
[[email protected] shellscript]$ vim test.sh//下面是程式內容#!/bin/bash#Program#       User inputs his first name and last name. Program shows his full name.#History:# 2015/05/14 shine_yr First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:usr/local/sbin:~/binexport PATHread -p "Please input your first name: "firstname #提示使用者輸入read -p "Please input your last name: "lastname #提示使用者輸入echo -e "\nYour full name is: $firstname $lastname" #結果在螢幕輸出exit 0
首先,利用直接執行的方式來執行指令碼:
[[email protected] shellscript]$ sh test.shPlease input your first name: shinePlease input your last name: yrYour full name is: shine yr[[email protected] shellscript]$ echo $firstname[[email protected] shellscript]$ echo $lastname[[email protected] shellscript]$ 
從上面可以看出,程式順利執行,然後我利用echo命令打算輸出firstname以及lastname的內容,結果卻輸出為空白。這是什麼原因呢?這就是說此方式在當子進程完成後,子進程中的各項變數或者操作都將會結束而不會傳回到父進程中,如所示:
然後,使用source的方式執行指令碼:
[[email protected] shellscript]$ source test.shPlease input your first name: shinePlease input your last name: yrYour full name is: shine yr[[email protected] shellscript]$ echo $firstnameshine[[email protected] shellscript]$ echo $lastnameyr[[email protected] shellscript]$ 
我們可以看到,此時變數firstname以及lastname 中是有確切值的。也就是說,此時test.sh會在父進程中執行,因此各項操作都會在原本的bash中生效。

5. 編寫shell script的良好習慣在標頭檔處記好:(1)script的功能(2)script的版本資訊(3)script的作者與聯絡方式(4)script的著作權聲明方式(5)script的History(記錄)(6)script內較特殊的命令,使用“絕對路徑”的方式來執行(7)script執行時需要的環境變數預先聲明與設定
除此之外,我們還應該習慣用[Tab]鍵來控制格式,另外,使用vim而不是vi作為編寫工具,因為vim的功能更強大,不僅有顏色提示,還有文法檢測功能。

菜鳥的《Linux程式設計》學習—shell script

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.