情境模擬題二:使用管線命令配合正規(guī)表示法創(chuàng)建新命令與新變量。我想要創(chuàng)建一個(gè)新的命令名為 myip , 這個(gè)命令能夠?qū)⑽蚁到y(tǒng)的 IP 捉出來顯示。而我想要有個(gè)新變量,變量名為 MYIP ,這個(gè)變量可以記錄我的 IP 。
處理的方式很簡單,我們可以這樣試看看:
首先,我們依據(jù)本章內(nèi)的 ifconfig, sed 與 awk 來取得我們的 IP ,命令為:
[root@www scripts]# echo $firstname $lastname<==確認(rèn)了,這兩個(gè)變量并不存在喔!
[root@www scripts]# sh sh02.sh
Please input your first name: VBird<==這個(gè)名字是鳥哥自己輸入的
Please input your last name: Tsai Your full name is: VBird Tsai <==看吧!在 script 運(yùn)行中,這兩個(gè)變量有生效
[root@www scripts]# echo $firstname $lastname<==事實(shí)上,這兩個(gè)變量在父程序的 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 <==嘿嘿!有數(shù)據(jù)產(chǎn)生喔!
[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"
運(yùn)行結(jié)果如下:
[root@www scripts]# sh sh07.sh theone haha quot
The script name is ==> sh07.sh <==檔名
Total parameter number is ==> 3 <==果然有三個(gè)參數(shù)
Your whole parameter is ==> 'theone haha quot' <==參數(shù)的內(nèi)容全部
The 1st parameter ==> theone <==第一個(gè)參數(shù)
The 2nd parameter ==> haha <==第二個(gè)參數(shù)
[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 # 進(jìn)行第一次『一個(gè)變量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
shift 3 # 進(jìn)行第二次『三個(gè)變量的 shift 』
echo "Total parameter number is ==> $#"
echo "Your whole parameter is ==> '$@'"
這玩意的運(yùn)行成果如下:
[root@www scripts]# sh sh08.sh one two three four five six<==給予六個(gè)參數(shù)
Total parameter number is ==> 6 <==最原始的參數(shù)變量情況
Your whole parameter is ==> 'one two three four five six'
Total parameter number is ==> 5 <==第一次偏移,看底下發(fā)現(xiàn)第一個(gè) one 不見了
Your whole parameter is ==> 'two three four five six'
Total parameter number is ==> 2 <==第二次偏移掉三個(gè),two three four 不見了
Your whole parameter is ==> 'five six'
單層、簡單條件判斷式
如果你只有一個(gè)判斷式要進(jìn)行,那么我們可以簡單的這樣看:
if [ 條件判斷式 ]; then當(dāng)條件判斷式成立時(shí),可以進(jìn)行的命令工作內(nèi)容;
fi<==將 if 反過來寫,就成為 fi 啦!結(jié)束 if 之意!
date_d=$(echo $date2 |grep '[0-9]\{8\}')?? # 看看是否有八個(gè)數(shù)字 if [ "$date_d" == "" ]; then echo "You input the wrong date format...." exit 1 fi
case $變量名稱 in<==關(guān)鍵字為 case ,還有變量前有錢字號"第一個(gè)變量內(nèi)容")<==每個(gè)變量內(nèi)容建議用雙引號括起來,關(guān)鍵字則為小括號 )程序段;;<==每個(gè)類別結(jié)尾使用兩個(gè)連續(xù)的分號來處理!"第二個(gè)變量內(nèi)容")程序段;;*)<==最后一個(gè)變量內(nèi)容都會用 * 來代表所有其他值不包含第一個(gè)變量內(nèi)容與第二個(gè)變量內(nèi)容的其他程序運(yùn)行段exit 1;;esac<==最終的 case 結(jié)尾!『反過來寫』思考一下!
利用 function 功能
什么是『函數(shù) (function)』功能啊?簡單的說,其實(shí), 函數(shù)可以在 shell script 當(dāng)中做出一個(gè)類似自訂運(yùn)行命令的東西,最大的功能是, 可以簡化我們很多的程序碼~舉例來說,上面的 sh12.sh 當(dāng)中,每個(gè)輸入結(jié)果 one, two, three 其實(shí)輸出的內(nèi)容都一樣啊~那么我就可以使用 function 來簡化了! function 的語法是這樣的:
[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" # 這個(gè) $1 必須要參考底下命令的下達(dá)
}echo "This program will print your selection !"
case $1 in"one")printit 1# 請注意, printit 命令后面還有接參數(shù)!;;"two")printit 2;;"three")printit 3;;*)echo "Usage $0 {one|two|three}";;
esac
在上面的例子當(dāng)中,如果你輸入『 sh sh12-3.sh one 』就會出現(xiàn)『 Your choice is 1 』的字樣~ 為什么是 1 呢?因?yàn)樵诔绦蚨温洚?dāng)中,我們是寫了『 printit 1 』那個(gè) 1 就會成為 function 當(dāng)中的 $1 喔~
while do done, until do done (不定回圈)
一般來說,不定回圈最常見的就是底下這兩種狀態(tài)了:
while [ condition ] <==中括號內(nèi)的狀態(tài)就是判斷式
do<==do 是回圈的開始!程序段落
done<==done 是回圈的結(jié)束
while 的中文是『當(dāng)....時(shí)』,所以,這種方式說的是『當(dāng) condition 條件成立時(shí),就進(jìn)行回圈,直到 condition 的條件不成立才停止』的意思。還有另外一種不定回圈的方式:
until [ condition ]
do程序段落
done
for...do...done (固定回圈)
相對於 while, until 的回圈方式是必須要『符合某個(gè)條件』的狀態(tài), for 這種語法,則是『 已經(jīng)知道要進(jìn)行幾次回圈』的狀態(tài)!他的語法是: