- 論壇徽章:
- 0
|
1.1 shell編程概述1.2 shell程序舉例1.3 傳遞數(shù)據(jù)給shell程序1.4 shell 程序的參數(shù)1.5 一些特殊shell變量- #和*1.6 shift 命令1.7 read 命令1.8 另外的技術(shù)
1.1 shell編程概述
shell程序是一個(gè)包含UNIX命令的普通文件。
這個(gè)文件的許可權(quán)限至少應(yīng)該為可讀和可執(zhí)行。
在shell提示符下鍵入文件名就可執(zhí)行shell程序。
shell程序可以通過(guò)三種方式接受數(shù)據(jù):
??-環(huán)境變量
??-命令行參數(shù)
??-用戶的輸入
shell是一個(gè)命令解釋器,它會(huì)解釋并執(zhí)行命令提示符下輸入的命令。但是,你可能想要多次執(zhí)行一組命令,shell提供了一種功能,讓你將這組命令存放在一個(gè)文件中,然后你可以象unix系統(tǒng)提供的其他程序一樣執(zhí)行這個(gè)文件,這個(gè)命令文件就叫做shell程序或者shell腳本。當(dāng)你運(yùn)行這個(gè)文件,它會(huì)象你在命令行輸入這些命令一樣地執(zhí)行這些命令。為了讓shell能讀取并且執(zhí)行你的shell程序,shell腳本的文件權(quán)限必須被設(shè)置為可讀和可執(zhí)行。為了讓shell可以找到你的程序,你可以選擇輸入完全路徑名,或者將這個(gè)腳本的路徑放在于你的PATH環(huán)境變量指定的路徑列表中。許多的用戶會(huì)在他們的HOME目錄下創(chuàng)建一個(gè)bin目錄來(lái)存放他們自己開(kāi)發(fā)的script,然后將$HOME/bin加入到他們的PATH環(huán)境變量中。你可以寫出非常復(fù)雜的shell腳本,因?yàn)閟hell腳本支持變量、命令行參數(shù)、交互式輸入、tests(判斷))、branches(分支),和loops(循環(huán))等復(fù)雜的結(jié)構(gòu)。
1.2 shell程序舉例
$ cat myprog
#this is the program myprog
date
ls –F
$ myprog
要?jiǎng)?chuàng)建一個(gè)shell程序,考慮進(jìn)行以下步驟:
$ vi myprog ?????一個(gè)包含shell命令的程序。
#this is the program myprog
date
ls –F
$ chmod +x myprog 增加文件的執(zhí)行模式
$ myprog
Thu Jul 11 11:10 EDT 1994
F1 f2 memo/ myprog*
首先使用一個(gè)文本編輯器創(chuàng)建一個(gè)shell程序myprog。在程序執(zhí)行之前,這個(gè)文件必須被賦予可執(zhí)行的權(quán)限。然后在命令提示符下輸入這個(gè)程序名,如上例所示,當(dāng)myprog執(zhí)行的時(shí)候,一個(gè)子shell會(huì)被創(chuàng)建。這個(gè)子shell會(huì)從shell程序文件myprog讀取輸入而不是從命令行讀取輸入,這個(gè)shell中的每個(gè)命令的執(zhí)行都會(huì)創(chuàng)建一個(gè)子shell。一旦所有的命令都被執(zhí)行,所有的子shell會(huì)中止,然后會(huì)返回到原始的父shell。
Shell程序中的注釋:
推薦在shell程序中提供注釋語(yǔ)句來(lái)注明程序的內(nèi)容。注釋由一個(gè)#符號(hào)開(kāi)始,Shell不會(huì)去執(zhí)行任何在#之后的語(yǔ)句。#能夠出現(xiàn)在命令行的任何位置。
注意:你不可以給shell程序取名為test因?yàn)閠est是一個(gè)內(nèi)部的shell命令。
1.3 傳遞數(shù)據(jù)給shell程序
$ color = lavender
$ cat color1
echo you are now running program: color1
echo the value of the variable color is : $color
$ chmod +x color1
$ color1
you ar now running program : color1
the value of the variable color is :
$ export color
$ color1
you are now running program : color1
the value of the variable color is : lavender
傳遞數(shù)據(jù)給shell腳本的一種方法就是通過(guò)環(huán)境。在上例中,本地變量color被賦值為lavender,然后創(chuàng)建了shell程序color1;然后更改為可執(zhí)行權(quán)限;然后這個(gè)shell程序被執(zhí)行,color1腳本的意圖是顯示color變量的值,但是由于color是一個(gè)本地變量,屬于父shell私有的,運(yùn)行color1產(chǎn)生的子shell不能識(shí)別這個(gè)變量,因此不能打印出它的值,而當(dāng)color被輸出到環(huán)境中就可以被子shell讀取。
同樣,由于shell進(jìn)程不能夠更改父進(jìn)程的環(huán)境,對(duì)一個(gè)子進(jìn)程中的環(huán)境變量重新賦值不會(huì)影響到父進(jìn)程環(huán)境中的值。如以下的shell腳本中的color2。
echo The original value of the variable color is $color
ech0 This program will set the value of color to amber
color=amber
echo The value of color is now $color
echo When your program concludes,display the value of the color variable
觀察在你設(shè)置了color的值后有什么變化。輸出這個(gè)變量,然后執(zhí)行color2:
$ export color=lavender
$ echo $color
lanvender
$ color2
The original value of the variable color is lavender
The program will set the value of color to amber
The value of volor is now amber
When your progam concludes, display the value of the color variable,
$ echo $color
lanvender
1.4 shell 程序的參數(shù)
命令行:
?$ sh_program arg1 arg2 . . . argx
???$0 ???$1?? $2 ....? $X
例子:
$ cat color3
echo you are now running program: $0
echo The value of command line argument #1 is: $1
echo The value of command line argument #2 is : $2
$ chmod +x color3
$ color3 red green
You are now running program: color3
The value of command line argument #1 is : red
The value of command line argument #2 is: green
大多數(shù)的UNIX系統(tǒng)命令可以接收命令行參數(shù),這些參數(shù)通常告訴命令它將要操作的文件或目錄(cp f1 f2),另外指定的參數(shù)擴(kuò)展命令的能力(ls –l),或者提供文本字符串(banner hi there)。
命令行參數(shù)對(duì)shell程序同樣有效,使用這種方式傳送信息給你的程序十分方便。通過(guò)開(kāi)發(fā)一個(gè)接收命令行參數(shù)的程序,你可以傳遞文件或者目錄命令名給你的程序處理,就像你運(yùn)行UNIX系統(tǒng)命令一樣,你也可以定義命令行選項(xiàng)來(lái)讓命令行使用shell程序額外的功能。
在shell程序中的命令行參數(shù)與參數(shù)在命令行的位置相關(guān),這樣的參數(shù)被稱為位置參數(shù),因?yàn)閷?duì)每一個(gè)特殊變量的賦值依靠一這些參數(shù)在命令行中的位置,變量的變量名對(duì)應(yīng)變量在命令行中的位置,因此這些特殊的變量名為數(shù)字0,1,2等,一直到最后的參數(shù)被傳遞,變量名的存取也通過(guò)同樣的方法,在名字前面加上$ 符號(hào),因此,為了存取你的shell程序中的命令行參數(shù),你可以應(yīng)用$0,$1,$2等等。在$9以后,必須使用括號(hào):$(10),$(11),否則,shell會(huì)將$10看成是$1后面跟一個(gè)0。而$0會(huì)一直保存程序或命令的名字
以下的shell程序會(huì)安裝一個(gè)程序,這個(gè)程序作為一個(gè)命令行參數(shù)被安裝到你的bin目錄:首先創(chuàng)建程序my_install,注意目錄$HOME/bin應(yīng)該預(yù)先存在。
$ cat > my_install
echo $0 will install $1 to your bin directory
chmod +x $1
mv $1 $HOME/bin
echo Installation of $1 is complete
ctrl + d
$ chmod +x my_intalll
$ my_install color3
my_install will install color3 to your bin directory
Installation of color3 is complete
$
這個(gè)例子中,程序指明第一個(gè)命令行參數(shù)為一個(gè)文件名,然后加上執(zhí)行權(quán)限,然后移動(dòng)到你當(dāng)前目錄下的bin目錄下。
記住UNIX系統(tǒng)的慣例是存貯程序在bin的目錄下。你也許想要在你的HOME目錄下創(chuàng)建一個(gè)bin目錄,在這個(gè)目錄下你可以存儲(chǔ)你的程序文件,記住要將你的bin目錄放在PATH環(huán)境變量中,這樣shell才會(huì)找到你的程序。
1.5 一些特殊shell變量- #和*
# ???命令行參數(shù)的數(shù)量
* ???完全的參數(shù)字符串
例子:
$ cat color4
echo There are $#?comand line argument
echo They are $*
ehco The first command line argument is $1
$ chmod +x color4
$ color4 red green yellow blue
They are 4 command line arguments
They are red green yellow blue
The first command line argument is red
$
至今為止我們看到的shell程序都不是很靈活,如color3需要輸入兩個(gè)正確的參數(shù)而my_install只需要一個(gè)。通常在創(chuàng)建一個(gè)接收命令行參數(shù)的shell程序的時(shí)候,你想要用戶輸入一個(gè)參數(shù)的變量號(hào)碼。你同時(shí)要程序執(zhí)行成功,不管用戶鍵入1個(gè)參數(shù)或是20個(gè)參數(shù)。當(dāng)處理變量參數(shù)列表的時(shí)候,特殊shell變量會(huì)提供你許多的靈活性。通過(guò)$#你可以知道有多少參數(shù)已經(jīng)被輸入,通過(guò)$*可以存取全部的參數(shù)列表,而不管參數(shù)的數(shù)量。請(qǐng)注意參數(shù)($0)不在$*這個(gè)參數(shù)列表里。
每一個(gè)命令行參數(shù)都是互相獨(dú)立的,你可以通過(guò)$*集中檢索這些參數(shù),也可以通過(guò)$1,$2,$3等等來(lái)獨(dú)立的檢索這些參數(shù)。
一個(gè)可以接收多個(gè)命令行參數(shù)的安裝程序的例子:
$ cat > my_install2
echo $0 will install $# files to your bin directory
echo The files to be installed are : $*
chmod +x $*
mv $* $HOME/bin
echo Installaton is complete
ctril + d
$ chmod +x my_install2
$ my_install2 color1 color2
my_intall2 will install 2 files to your bin directory
The files to be installed are: color1,color2
Intallaiton is complete
這個(gè)安裝程序更加靈活,如果你有多個(gè)文件要安裝,你僅需要執(zhí)行這個(gè)程序一次,只要一次輸入多個(gè)名字即可。
非常重要的是:如果你計(jì)劃傳遞整個(gè)參數(shù)的字符串給一個(gè)命令,這個(gè)命令必須能夠接收多個(gè)參數(shù)。
在以下的腳本中,用戶提供一個(gè)目錄名作為一個(gè)命令行參數(shù)。程序會(huì)更改到指定的目錄,顯示當(dāng)前的位置,并且列出內(nèi)容。
$ cat list_dir
cd $*
echo You are in the $(pwd) directory
echo The contents of the directory are:
ls –F
$ list_dir dir1 dir2 dir3
sh: cd: bad argument count
由于cd命令不能同時(shí)進(jìn)入到多個(gè)目錄中,這個(gè)程序執(zhí)行會(huì)出錯(cuò)。
1.6 shift 命令
向左移動(dòng)所有的在*中的字符串n個(gè)位置
#的數(shù)目減少n個(gè)(n的默認(rèn)值是1)
語(yǔ)法:shift [n]
例子:
$ cat color5
orig_args=$*
echo There are $# command line arguments
echo They are $*
echo Shifting two arguments
shift 2
echo There are $# comand line arguments
echo They are $*
echo Shifting two arguments
shift 2; final_args=$*
echo Original arguments are: $orig_args
echo Final arguments are: $final_args
shift命令會(huì)重新分配命令行參數(shù)對(duì)應(yīng)位置參數(shù),在shift n以后,所有的*中的參數(shù)會(huì)向左移動(dòng)n個(gè)位置。同時(shí)$#會(huì)減n。默認(rèn)的n為1。Shift命令不會(huì)影響到參數(shù)0的位置。一旦你完成一次移動(dòng),被移出命令行的參數(shù)會(huì)丟失。如果你想在你的程序中引用這個(gè)參數(shù),你需要在執(zhí)行shift之前存貯這個(gè)參數(shù)到一個(gè)變量中。
Shift命令可以用于:存取一組參數(shù)的位置,例如一系列的x,y的坐標(biāo)
從命令行刪除命令選項(xiàng),假定選項(xiàng)在參數(shù)之前。
例子:
$ color5 red green yellow orange black
There are 6 command line arguments
They are red green yellow blue orange black
Shifting two arguments
There are 4 command line arguments
They are yellow blue orange black
Shiftging two arguments
Original arguments are: red green yellow blue orange black
Final argument are : orange black
$
1.7 read 命令
語(yǔ)法:
read variable [variable......]
例子:
$ cat color6
echo This program prompts for user input
echo “please enter your favorite two colors -> c”
read color_a color_b
echo The colors you entered are: $color_b $color_a
$ chmod +x color6
$ color6
This program prompts for user input
Please enter your favorite two colors -> red blue
The colors you entered are: blue red
$ color6
This program prompts for user input
Please enter you favorite two colors -> red blue tan
The color you enterd are :blue tan red
如果使用命令行參數(shù)傳遞信息進(jìn)程序,在命令執(zhí)行之前用戶必須知道正確的語(yǔ)法。有一種情況,你想要在用戶執(zhí)行程序的時(shí)候提示他輸入這些參數(shù)。read命令就是用來(lái)在程序執(zhí)行的時(shí)候收集終端鍵入的信息。
通常會(huì)使用echo命令來(lái)給用戶一個(gè)提示,讓他知道程序正在等待一些輸入,同時(shí)通知用戶應(yīng)該輸入的類型。因此,每一個(gè)read命令應(yīng)該在echo命令后面。
read命令會(huì)給出一個(gè)變量名的列表,用戶在提示符下輸入會(huì)給這些變量賦值(變量之間以空格分隔)。如果read命令定義的變量比輸入的詞要多,多出的變量會(huì)被賦空值。如果用戶輸入的詞要比變量多,剩余的數(shù)據(jù)會(huì)賦給列表中的最后一個(gè)變量。
一旦被賦值,你就可以象其他的shell變量一樣存取這些變量。
注意:不要混淆位置參數(shù)和變量read。位置參數(shù)在命令被激活時(shí)直接在命令行中使用,而read命令給變量賦值是在程序執(zhí)行之中,用戶響應(yīng)輸入的提示而給變量賦值。
以下例子提示用戶輸入要被安裝的文件名:
$ cat > my_install3
echo $0 will install files into your bin directory
echo “Enter the names of the files -> c”
read filenames
mv $filenames $HOME/bin
echo Instllation is complete
ctrl + d
$ chmod +x my_install13
$ my_install13
my_install13 will install files into your bin directory
Enter the names of the files -> f1 f2
Installaton is complete
這個(gè)安裝腳本會(huì)提示用戶輸入需要chmod并移動(dòng)到$HOME/bin的文件的文件名。這個(gè)程序給用戶更多的關(guān)于應(yīng)該輸入數(shù)據(jù)情況的指引。而不像install2中用戶必須在命令行中提供文件名,用戶使用程序不需要特殊的語(yǔ)法,程序讓用戶確切地知道要輸入什么。所有的輸入的文件名都會(huì)被賦值給變量filenames。
1.8 另外的技術(shù)
#號(hào)開(kāi)始的文檔為注釋部分。
sh shell_program argumetns?
shell_program 的屬性可以不是可執(zhí)行的。
shell_program 必須是可讀的。
sh –x shell_program arguments
每一行在被執(zhí)行前被打印出來(lái)。
在調(diào)試程序時(shí)有用處。
在shell程序中,#符號(hào)的意思是后面是一段注釋,而shell會(huì)自動(dòng)忽略#符號(hào)以后直到一個(gè)回車符號(hào)為止的所有字符。<
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/5587/showart_18502.html |
|