450 likes | 560 Views
網路伺服器應用 Linux Server. Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology andres@dlit.edu.tw http://www.cse.dlit.edu.tw/~andres. Chapter 8 Shell scripts. 8-1 變數與變數的設定 8-2 Scripts 8-3 Declare 8-4 對談式 scripts : read
E N D
網路伺服器應用Linux Server Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology andres@dlit.edu.tw http://www.cse.dlit.edu.tw/~andres
Chapter 8 Shell scripts 8-1 變數與變數的設定 8-2 Scripts 8-3 Declare 8-4 對談式 scripts : read 8-5 scripts 邏輯判斷式與運算式 8-6 scripts迴圈
8-1 變數與變數的設定 • 變數就是以一組文字或符號等,來取代一些設定或者是一串保留的資料 • 環境變數 • 系統需要一些變數來提供他資料的存取 • 或者是一些環境的設定參數值 • $MAIL • $PATH
echo • 顯示變數內容 • echo $variable • echo $PATH • echo $HOME • echo $MAIL
env • env ENV=/root/.bashrc <==使用者自訂環境變數的設定檔案 HISTSIZE=1000 <==目前的指令記憶數量 HOME=/home/test <==登入者的家目錄 HOSTNAME=test.adsldns.org <==這部主機的主機名稱 HOSTTYPE=i386 <==這部主機的硬體等級大致狀態INPUTRC=/etc/inputrc <==一些 shell 載入的資料檔案設定處 LANGUAGE=C LANG=zh_TW.Big5 …… MAIL=/var/spool/mail/test <==登入者的郵件預設放置地點 OSTYPE=linux-gnu <==作業系統的形式(linux-gnu) PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/test/bin PWD=/home/test <==目前登入者所在的目錄(當下的目錄) SHELL=/bin/bash <==登入者使用的 shell 類型
set • 顯示目前系統中全部的變數內容 • 環境變數 • 自訂變數
set BASH=/bin/bash BASH_VERSION=$'2.05.8(1)-release' <==BASH 的版本 COLORS=/etc/DIR_COLORS <==使用顏色 COLUMNS=100 <==目前這個終端機使用的欄位有幾個字元距離 HISTFILE=/home/root/.bash_history <==目前用來存過往指令的檔案,為一個隱藏檔 HISTFILESIZE=1000 langfile=/home/root/.i18n <==語系選擇的檔案 LINES=40 <==目前游標所在的位置為第幾行 MAILCHECK=60 <==每隔多久檢查一次有無新信件(秒數) UID=500 <==登入者的使用者 ID (UID)
變數設定規則 • 變數與變數內容以等號『=』來連結 • 等號兩邊不能直接接空白字元 • 變數名稱只能是英文字母與數字,但是數字不能是開頭字元 • 若有空白字元可以使用雙引號『 “ 』或單引號『 ‘ 』來將變數內容結合起來 • 雙引號內的特殊字元可以保有變數特性 • 單引號內的特殊字元則僅為一般字元 • 必要時需要以跳脫字元『 \ 』來將特殊符號(如Enter, $, \, 空白字元, '等)變成一般符號
變數設定規則 • 在一串指令中,還需要藉由其他的指令提供的資訊,可以使用 quote • ` command` • 若該變數為擴增變數內容時,則需以雙引號及 $變數名稱 • "$PATH":/home • 若該變數需要在其他子程序執行,則需要以 export 來使變數可以動作 • export PATH • 通常大寫字元為系統預設變數,自行設定變數可以使用小寫字元,方便判斷
一般變數設定 • 12name=VBrid <==錯誤 • name = VBird <==錯誤的!因為等號兩邊不能直接接空白 • name=VBird <==正確的!echo $name 顯示 VBird • name=VBird name <==錯誤的!需要加上雙引號 • name="VBird name" <==正確的!echo $name 顯示 VBird name • name="VBird's name" <==正確的! 變數累加設定: • name=$nameisme <==錯誤的!需要以雙引號將原變數圈起來 • name="$name"isme <==正確的!echo $name 顯示 VBird's nameisme • PATH="$PATH":/home/test <==正確 PATH="$PATH:/home/test" <==正確
變數延伸到下一個子程序 • name="VBird's name" <==設定 name 這個變數 • echo $name <==顯示 name 變數的指令 VBird's name • /bin/bash <==另開一個 bash 的子程序 • echo $name <==顯示 name 這個變數 • exit <==退出子程序 bash shell • export name <==正確的!$name 可以用於下一個子程序中
指令中的指令 • cd /lib/modules/`uname –r`/kernel • 會先執行 `uname –r` 這個內含指令 • uname –r 會顯示出目前的核心版本 • 然後輸出的結果附加在 /lib/module… 裡面 • 所以執行這個指令,可以完成幾個附指令程序
export • 進入了該子程序,所以在父程序中的變數設定將不再繼續的存在 • 要讓該變數內容繼續的在子程序中使用 • 執行 export 變數 • 沒有接變數時,會把所有的環境變數列出 • export 可以將一般自訂的變數變成環境變數
unset • unset 變數 • 直接將該變數的內容拿掉
8-2 scripts • 寫scripts習慣 • 先宣告使用的 shell 為何? • 註明該 script 的內容功能、版本資訊、作者、建檔日期等等 • 每一個大步驟的主要功能
執行scripts的方法 • 將該檔案改成可以執行的屬性,然後執行該檔案 • chmod 755 scripts.file • 直接以 sh 執行檔來執行 script 的內容 • sh scripts.file
test01-hello.sh #!/bin/bash # 這個腳本的用途在於列出 Hello ! How are you 在螢幕上 hello=Hello\ \!\ How\ are\ you\ \? echo $hello • sh test01-hello.sh Hello ! How are you ?
test02-2var.sh #!/bin/bash # 這個腳本用途在於引用兩個變數name="V.Bird" myname1="My name is $name" myname2='My name is $name' echo $name echo $myname1 echo $myname2 • sh test02-2var.sh V.Bird My name is V.Bird My name is $name
8-3 declare • declare [-afirx] -a :定義為陣列 array -f :定義為函數 function -i :定義為整數 integer -r :定義為『唯讀』 -x :定義為透過環境輸出變數 • declare -i a=3 • declare -i b=5 • declare -i c=$a*$b • echo $c 15
test03-declare.sh #!/bin/bash # This program is used to "declare" variablesnumber1=2*3+5*13-32+25 declare -i number2=2*3+5*13-32+25 echo "Your result is ==> $number1" echo "Your result is ==> $number2" • sh test03-declare.sh Your result is ==> 2*3+5*13-32+25 Your result is ==> 64
8-4 對談式 scripts • 依據鍵盤輸入的結果 input 到變數內容中 • read name VBird <==這是鍵盤輸入的結果 • echo $name VBird
test04-read.sh #!/bin/bash # This program is used to "read" variables echo "Please keyin your name, and press Enter to start." read name echo "This is your keyin data ==> $name" • sh test04-read.sh Please keyin your name, and press Enter to start. VBird Tsai This is your keyin data ==> VBird Tsai
script 的參數的代號 • myscript opt1 opt2 opt3 opt4 $0$1$2$3 $4 • $0 : myscript 亦即是 script 的檔名 • $1 : opt1亦即是第一個附加的參數 • $2 : opt2 • $3 : opt3
test05-0123 #!/bin/bash # This program will define what is the parameters echo "This script's name => $0" echo "parameters $1 $2 $3" • sh test05-0123 pa1 pa2 pa3 This script's name => test05-0123 parameters pa1 pa2 pa3
8-5 scripts 邏輯判斷式 • if...then...fi • case ...esac
if...then...fi if [ 條件判斷一 ] && (||) [ 條件判斷二 ]; then elif [ 條件判斷三 ] && (||) [ 條件判斷四 ]; then else fi • 在 [ ] 當中,只能有一個判別式; • 在 [ ] 與 [ ] 當中,可以使用 && 或 || 來組織判別式; • 每一個獨立的元件之間『都需要有空白鍵來隔開』!
test06-ifthen.sh #!/bin/bash # This program is used to study if then echo "Press y to continue" read yn if [ "$yn" = "y" ]; then echo "script is running..." else echo "STOP!" fi • sh test06-ifthen.sh Press y to continue y script is running... • sh test06-ifthen.sh Press y to continue n STOP!
test07-ifthen.sh #!/bin/bash # This program is used to study if then echo "Press y to continue" read yn if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then echo "script is running..." else echo "STOP!" fi • sh test07-ifthen.sh Press y to continue y script is running... • sh test07-ifthen.sh Press y to continue Y script is running...
test08-ifthen.sh #!/bin/bash # set parameters in the if then # 需要加上 hello 這個參數才會顯示正確的! if [ "$1" = "hello" ]; then echo "Hello! How are you ?" elif [ "$1" = "" ]; then echo "You MUST input parameters" else echo "The only accept parameter is hello" fi • sh test08-ifthen.sh hello Hello! How are you ? • sh test08-ifthen.sh You MUST input parameters • sh test08-ifthen.sh djdkf The only accept parameter is hello
case ...esac case 種類方式(string) in 種類方式一) 程式執行段 ;; 種類方式二) 程式執行段 ;; *) echo "Usage: {種類方式一|種類方式二}" exit 1 esac
種類方式(string)的格式 • 直接下達式 • 執行檔案 + string • /etc/rc.d/init.d 裡頭的基本設定方式 • string 可以直接寫成『 $1 』 • 在執行檔案後面直接加入參數的第一個參數 • 互動式 • 由螢幕輸出可能的項目,然後讓使用者輸入 • 必須配合『 read variable 』 • 然後 string 寫成『 $variable 』的格式
vi test09-case.sh #!/bin/bash echo "This program will print your selection!" case $1 in one) echo "your choice is one" ;; two) echo "your choice is two" ;; three) echo "your choice is three" ;; *) echo "Usage {one|two|three}" exit 1 esac
vi test09-case.sh • sh test09-case.shThis program will print your selection! Usage {one|two|three} • sh test09-case.sh three This program will print your selection! your choice is three
test10-case.sh • #!/bin/bashecho "Press your select one, two, three" read number case $number in one) echo "your choice is one" ;; two) echo "your choice is two" ;; three) echo "your choice is three" ;; *) echo "Usage {one|two|three}" exit 1 esac
test10-case.sh • sh test10-case.sh Press your select one, two, three two your choice is two
8-6 scripts迴圈 • for((條件一; 條件二; 條件三 )) • for variable in variable1 variable2 ..... • while[ condition1 ] && { || } [ condition2 ] ... • until[ condition1 ] && { || } [ condition2 ] ...
test11-loop.sh #!/bin/bash # Using for and loopdeclare -i s # <==變數宣告 for (( i=1; i<=100; i=i+1 )) do s=s+i done echo "The count is ==> $s" • sh test11-loop.sh The count is ==> 5050
使用 while : test12-loop.sh #!/bin/bash # Using while and loop declare -i i declare -i s while [ "$i" != "101" ] do s=s+i i=i+1 done echo "The count is ==> $s"
使用 until:test13-loop.sh #!/bin/bash # Using until and loop declare -i i declare -i s until [ "$i" = "101" ] do s=s+i i=i+1 done echo "The count is ==> $s" • sh test12-loop.sh The count is ==> 5050
test14-for.sh #!/bin/bash # using for...do ....done # LIST="Tomy Jony Mary Geoge" for i in $LIST do echo $i done • sh test5.sh Tomy Jony Mary Geoge
test15-for.sh #!/bin/bash # Using for and loop to read the account of this linux serveraccount=`cut -d ":" -f1 /etc/passwd|sort` echo "The following is your linux server's account" for i in $account do echo $i done • sh test15-for.sh The following is your linux server's account adm aerosol alenchan amanda apache ....
test16-loop.sh #!/bin/bash # Using until # echo "Press Y/y to stop" until [ "$yn" = "Y" ] || [ "$yn" = "y" ] do read yn done echo "Stop here" • sh test16-for.sh Press Y/y to stop GDSG A Y Stop here
test17-ifthen.sh #!/bin/bash # using if and then to select file or directory if [ ! -e logical ]; then touch logical echo "Just make a file logical" exit 1 elif [ -e logical ] && [ -f logical ]; then rm logical mkdir logical echo "remove file ==> logical" echo "and make directory logical" exit 1 elif [ -e logical ] && [ -d logical ]; then rm -rf logical echo "remove directory ==> logical" exit 1 else echo "Does here have anything?" fi