1、1,第 7 章 Shell Script 程式設計,內容 Shell 命令稿簡介 基本敘述句 選擇性程式結構 重複性程式結構 函數,2,7-1 Shell Script 程式簡介 (1),何謂 Shell script 批次檔案 連續執行多個命令 Shell script (命令稿) 多個外殼命令 + 變數 + 程式控制結構 外殼命令 ls, cp, cat, finger, . 程式控制結構 if, while, case, for, select, 變數 環境變數, 自訂變數, 位置變數,3,7-1 Shell Script 程式簡介 (2),Shell script 執行 步驟 1:設定
2、命令搜尋路徑 $ export PATH=$PATH:. 步驟 2:利用 vi 編輯程式 $ vi shell.scr 步驟 3:設定成可執行檔 $ chmod u+x shell.scr 步驟 4:執行 shell script 程式 $ shell.scr,4,7-1 Shell Script 程式簡介 (3),Shell script 變數 自訂變數 (variable 程式範例) int=5 echo “intger = $int” 位置變數 (args 程式範例) $1, ,5,7-2 基本敘述句,輸出敘述 echo echo “Good lucky to you” 輸入敘述 rea
3、d echo n “What is your name =” read name 設定位置變數 set date 數學運算expr 3 + 4,6,7-3 選擇性結構敘述 (1),條件敘述 (1) 數值比較 test integer1 eq integer2 integer1 eq integer2,7,7-3 選擇性結構敘述 (2),條件敘述 (2) 字串比較,8,7-3 選擇性結構敘述 (3),條件敘述 (3) 檔案屬性測試 Test d file_1 -d file_1,9,7-3 選擇性結構敘述 (4),if 程式結構 標準格式簡單格式最簡潔格式,if 條件判斷 then命令敘述區段
4、fi,if 條件判斷; then命令敘述區段 fi,if 條件判斷; then; 命令敘述區段; fi,10,7-3 選擇性結構敘述 (5),case 程式結構,case variable in pattern_1) 命令敘述區塊 ; pattern_2)statement 1;statement 2;statement 3; pattern_3) 命令敘述區塊 ; esac,11,7-4 重複性結構敘述 (1),for 迴圈結構,for var in List docommandscommands done,12,7-4 重複性結構敘述 (2),select 迴圈結構,select var in List docommandscommands. done,13,7-4 重複性結構敘述 (3),while 迴圈結構,while 條件判斷 do命令區塊 done,14,7-4 重複性結構敘述 (4),until 迴圈結構,until 條件判斷 do命令區塊 done,15,7-5 函數,函數結構,Function function_name 命令或程式區塊 或 function_name() 命令或程式區塊 ,