[root@www scripts]# echo $firstname $lastname<==確認了,這兩個變量并不存在喔!
[root@www scripts]# sh sh02.sh
Please input your first name: VBird<==這個名字是鳥哥自己輸入的
Please input your last name: Tsai Your full name is: VBird Tsai <==看吧!在 script 運行中,這兩個變量有生效
[root@www scripts]# echo $firstname $lastname<==事實上,這兩個變量在父程序的 bash 中還是不存在的!
[root@www scripts]# source sh02.sh
Please input your first name: VBird
Please input your last name: TsaiYour full name is: VBird Tsai
[root@www scripts]# echo $firstname $lastname
VBird Tsai <==嘿嘿!有數據產生喔!
[root@www scripts]# vi sh07.sh#!/bin/bash
# Program:
# Program shows the script name, parameters...
# History:
# 2009/02/17 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATHecho "The script name is ==> $0"
echo "Total parameter number is ==> $#"
[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \&& exit 0
echo "Your whole parameter is ==> '$@'"
echo "The 1st parameter ==> $1"
echo "The 2nd parameter ==> $2"
運行結果如下:
[root@www scripts]# sh sh07.sh theone haha quot
The script name is ==> sh07.sh <==檔名
Total parameter number is ==> 3 <==果然有三個參數
Your whole parameter is ==> 'theone haha quot' <==參數的內容全部
The 1st parameter ==> theone <==第一個參數
The 2nd parameter ==> haha <==第二個參數
[root@www scripts]# vi sh08.sh#!/bin/bash
# Program:
# Program shows the effect of shift function.
# History:
# 2009/02/17 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATHecho "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift # 進行第一次『一個變量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 # 進行第二次『三個變量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
這玩意的運行成果如下:
[root@www scripts]# sh sh08.sh one two three four five six<==給予六個參數
Total parameter number is ==> 6 <==最原始的參數變量情況
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5 <==第一次偏移,看底下發現第一個 one 不見了
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2 <==第二次偏移掉三個,two three four 不見了
Your whole parameter is ==> 'five six'
單層、簡單條件判斷式
如果你只有一個判斷式要進行,那么我們可以簡單的這樣看:
if [ 條件判斷式 ]; then當條件判斷式成立時,可以進行的命令工作內容;
fi<==將 if 反過來寫,就成為 fi 啦!結束 if 之意!
date_d=$(echo $date2 |grep '[0-9]\{8\}')?? # 看看是否有八個數字 if [ "$date_d" == "" ]; then echo "You input the wrong date format...." exit 1 fi
case $變量名稱 in<==關鍵字為 case ,還有變量前有錢字號"第一個變量內容")<==每個變量內容建議用雙引號括起來,關鍵字則為小括號 )程序段;;<==每個類別結尾使用兩個連續的分號來處理!"第二個變量內容")程序段;;*)<==最后一個變量內容都會用 * 來代表所有其他值不包含第一個變量內容與第二個變量內容的其他程序運行段exit 1;;esac<==最終的 case 結尾!『反過來寫』思考一下!
利用 function 功能
什么是『函數 (function)』功能啊?簡單的說,其實, 函數可以在 shell script 當中做出一個類似自訂運行命令的東西,最大的功能是, 可以簡化我們很多的程序碼~舉例來說,上面的 sh12.sh 當中,每個輸入結果 one, two, three 其實輸出的內容都一樣啊~那么我就可以使用 function 來簡化了! function 的語法是這樣的:
function fname() {程序段
}
function 也是擁有內建變量的~他的內建變量與 shell script 很類似, 函數名稱代表示 $0 ,而后續接的變量也是以 $1, $2... 來取代的~ 這里很容易搞錯喔~因為『 function fname() { 程序段 } 』內的 $0, $1... 等等與 shell script 的 $0 是不同的。以上面 sh12-2.sh 來說,假如我下達:『 sh sh12-2.sh one 』 這表示在 shell script 內的 $1 為 "one" 這個字串。但是在 printit() 內的 $1 則與這個 one 無關。 我們將上面的例子再次的改寫一下,讓你更清楚!
[root@www scripts]# vi sh12-3.sh#!/bin/bash
# Program:
# Use function to repeat information.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATHfunction printit(){echo "Your choice is $1" # 這個 $1 必須要參考底下命令的下達
}echo "This program will print your selection !"
case $1 in"one")printit 1# 請注意, printit 命令后面還有接參數!;;"two")printit 2;;"three")printit 3;;*)echo "Usage $0 {one|two|three}";;
esac
在上面的例子當中,如果你輸入『 sh sh12-3.sh one 』就會出現『 Your choice is 1 』的字樣~ 為什么是 1 呢?因為在程序段落當中,我們是寫了『 printit 1 』那個 1 就會成為 function 當中的 $1 喔~
while do done, until do done (不定回圈)
一般來說,不定回圈最常見的就是底下這兩種狀態了:
while [ condition ] <==中括號內的狀態就是判斷式
do<==do 是回圈的開始!程序段落
done<==done 是回圈的結束
while 的中文是『當....時』,所以,這種方式說的是『當 condition 條件成立時,就進行回圈,直到 condition 的條件不成立才停止』的意思。還有另外一種不定回圈的方式:
until [ condition ]
do程序段落
done
for...do...done (固定回圈)
相對於 while, until 的回圈方式是必須要『符合某個條件』的狀態, for 這種語法,則是『 已經知道要進行幾次回圈』的狀態!他的語法是: