安徽理工大學_第1頁
安徽理工大學_第2頁
安徽理工大學_第3頁
安徽理工大學_第4頁
安徽理工大學_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Linux開發(fā)基礎(chǔ)Development Foundation on Linux OS, Ph.D & Associate Prof.1Outline Shell programming on Linux OS GNU C/C+ programming CGI programming in C/C+ language Perl programming CGI programming in Perl language2Section 1 Shell programming on Linux OS31.1 What is Shell? Shell is a command interpreter;

2、 Shell is a programming language, which includes variable, keywords and all kinds of control sentences;The generic Shell on Unix OS is Bourne Shell (for short, sh).There are other kinds of shell,C-shell, Korn Shell, etc.41.2 How to create and execute shell script? Create a shell Script using VI edit

3、orFor example:$vi testWhere vi is an editor, test is the of shell script.51.2 How to create and execute shell script? To execute a shell Script Using input redirection. Let “sh” read commands from shell script . E.g.$sh testScript is used as arguments of “sh” command. E,g.:$sh script script argument

4、sScript set as “execute attribute”, then it can execute directly$chmod +x test$./test61.3 shell中的變量 1.3.1 Environment variables 可寫的環(huán)境變量在用戶登陸的過程中執(zhí)行/etc/profile文件進行初始化各個用戶自身的啟動文件/home/username/.bash_profile可通過set命令顯示當前已經(jīng)設(shè)置的環(huán)境變量及其值。71.3 shell中的變量 1.3.1 Environment variables 只讀的環(huán)境變量環(huán)境變量含義$0Shell script

5、$1$9First-ninth command line arguments$*All value of command line arguments$#The number of command line arguments$The current process ID$?The exit status of the last command, “0” indicates “success”, “1” indicates “failure”$! the process ID of the last background processShift Shift position argument

6、81.3 shell中的變量 1.3.2 Shell variables defined by Usersdefinition:Variable name=string #delimiter may not be usedcitation$variable nameAs a part of a string#at the end of a string string $variable name#at the head of a string or in the middle of a stringstring1$variable namestring29101.4 shell中的特殊字符 1

7、.4.1 Wildcard characters in Shell 星號*問號?一對方括號 其作用是匹配該字符組所限定的任何一個字符,例如:fabcd可以匹配fa,fb,fc,fdfa-d與fabcd作用相同感嘆號表示否定,例如f!a-d.c表示以f打頭、第二個字符不是a-d的.c文件名10111.4 shell中的特殊字符 1.4.2 Shell中的引號 雙引號”由它括起來的字符,除$, , 仍保留其特殊功能外,其余字符通常作為普通字符單引號由它括起來的所有字符或字符串都作為普通字符出現(xiàn)。倒引號由此括起來的字符串被shell解釋為命令行,其執(zhí)行結(jié)果取代整個倒引號部分。例如:$echo cur

8、rent directory is pwd如果當前工作目錄為/home/wang的話則輸出結(jié)果為current directory is /home/wang11121.4 shell中的特殊字符 1.4.3 Shell命令執(zhí)行的順序操作符(1) 順序執(zhí)行順序分割符;例如:$cd /home/fang; ls l; cat a.txt管道線|例如: $who|wc l|write root(2)邏輯與Command1&command2&commandn例如:$cp test1 /home/wang&cat /home/wang/test1(3)邏輯或Command1|command2|comm

9、andn例如:$cp test1 /home/wang | ls -l12131.4 shell中的特殊字符 1.4.4 Shell中的注釋符、反斜線及后臺操作符(1)注釋符#(2)反斜線 例如要顯示字符$本身,就要用$ $echo “this is a $ dollar character”(3)后臺操作符& $gcc 13141.5 shell編程中的輸入輸出命令1.5.1 Shell中的輸入輸出標準文件stdoutStdinstderr1.5.2 Shell中的輸入輸出重定向$sh, $cat test.ctest2.c #顯示文件的內(nèi)容到另一個文件$cat #通過鍵盤輸入建立一個文件,

10、按z作為文件結(jié)束符14151.5 shell編程中的輸入輸出命令1.5.3 Shell中的輸入輸出命令Read Echo Echo 命令中使用的轉(zhuǎn)義字符:b backspace, c不將光標移動到下一行t table, “”本身, f換頁, n 換行, r回車0N ascii碼的八進制N的字符, 如101表示字符A例如:$echo “abcd” $t “efgh” 15161.6 Shell中的程序控制結(jié)構(gòu)語句1.6.1 if 語句 if 條件判斷then 命令1else 命令2 fi 條件判斷有“命令語句”和“測試語句”兩種形式,命令執(zhí)行成功則條件為真,返回值為“0”,否則為假,返回值非“0

11、”16171.6 shell中的程序控制結(jié)構(gòu)語句1.6.2 命令語句形式的條件判斷例1:下面的一段代碼首先判斷一個指定的用戶是否存在,若存在則給出信息,否則向操作系統(tǒng)中增加一個用戶。if cat /etc/passwd|grep $1 #$1為腳本的命令行參數(shù)then echo There has existed user $1 in OS!else useradd $1fi例2:下面代碼判斷在當前工作目錄中,一個指定的名字是目錄、文件還是不存在。if test -f $1 #$1為腳本的命令行參數(shù)then echo $1 is a generic file!else if test -d $

12、1 #else if 可以寫成elif then echo $1 is a directory! else echo $1 is not existed! fifi17181.6 shell中的程序控制結(jié)構(gòu)語句1.6.3 測試語句形式的條件判斷格式1:Test expression格式2: expression Example:read a#if test $a -gt 0 #用test測試if $a -gt 0 then echo $a is greater than 0!else echo $a is less than 0!fi18191.6 shell中的程序控制結(jié)構(gòu)語句1.6.3 測

13、試語句形式的條件判斷注意:如果使用shell變量,為保持完整,避免造成歧義,最好用雙引號將變量括起來在任何運算符、圓括號、或方括號前后至少需要一個空格條件測試需要另起一行時,用作為續(xù)行符。19201.6 shell中的程序控制結(jié)構(gòu)語句1.6.3 測試語句形式的條件判斷文件測試:-r file文件是否存在且是可讀的-w file文件是否存在且是可寫的-x file文件是否存在且是可執(zhí)行的-f file文件是否存在-d file是否是目錄-p file文件是否存在且是FIFO文件-s file文件是否存在且不是空文件20211.6 shell中的程序控制結(jié)構(gòu)語句1.6.3 測試語句形式的條件判斷字

14、符串測試:strStr是否為空str1 = str2str1是否與str2相等str1 != str2str1是否與str2不相等-n strstr的長度不為0則為真-z strstr的長度為0則為真21221.6 shell中的程序控制結(jié)構(gòu)語句1.6.3 測試語句形式的條件判斷數(shù)值測試:n1 eq n2n1=n2?n1 ne n2n1n2?n1 lt n2n1n2?n1 le n2n1=n2?22231.6 shell中的程序控制結(jié)構(gòu)語句1.6.4 CASE語句case string1 inStr1)command-list1;Str2)command-list2;Strn)command-

15、listn;esac23241.6 shell中的程序控制結(jié)構(gòu)語句1.6.4 CASE語句例子:echo =Menue=echo 1.Display calendar.echo 2.Display current time.echo 3.Display OS version.echo 4.Dislay online Users.echo 5.Exit.echo Please select:read choosecase $choose inAa1) cal; #可以選擇a,A,1執(zhí)行第一個菜單項,下同Bb2) date; #正則表達式也可以寫成B|b|2)Cc3) uname -a;Dd4)

16、w;Ee5) exit;esac24251.6 shell中的程序控制結(jié)構(gòu)語句1.6.5 for循環(huán)語句格式:for vairable in argument-listdocommand-listdone例1:argument-list為變量值列表。顯示110for i in 1 2 3 4 5 6 7 8 9 10do echo $idone25261.6 shell中的程序控制結(jié)構(gòu)語句1.6.5 for循環(huán)語句格式:for vairable in argument-listdocommand-listdone例2:argument-list為為文件表達式。打印當前目錄下的所有文件名以s打頭

17、的文件的內(nèi)容for i in s*do cat $i | pr #輸出重定向到打印機done26271.6 shell中的程序控制結(jié)構(gòu)語句1.6.5 for循環(huán)語句格式:for vairable in argument-listdocommand-listdone例3:argument-list為空,此種情況等價于循環(huán)變量依次取位置參數(shù)的值。for vairable 等價于for varialbe in $*,$*表示所有腳本參數(shù)的值。編寫一個shell,第一位置參數(shù)為指定的目錄名,其后的參數(shù)為第一個參數(shù)指定目錄下的文件,依次顯示這些文件的內(nèi)容。To be continued!27281.6

18、shell中的程序控制結(jié)構(gòu)語句1.6.5 for循環(huán)語句dir=$1;shift #取目錄名后,位置參數(shù)向左移位if -d $dir then cd $dir for name # in $* do if -f $name then cat $name echo =end of this else echo =invalid :$dir/$name!= fi doneelse echo =invalid directory name:$dir!=fi28291.6 shell中的程序控制結(jié)構(gòu)語句1.6.6 while循環(huán)語句當型循環(huán)While expressionDocommand-listD

19、one例:顯示1-100之間的整數(shù)i=1while $i -le 100 do echo $i i=expr $i + 1 #倒引號,expr是數(shù)值運算done29301.6 shell中的程序控制結(jié)構(gòu)語句1.6.7 until循環(huán)語句直到型循環(huán)until expressionDocommand-listDone例:顯示1-100之間的整數(shù)i=1until $i -gt 100 do echo $i i=expr $i + 1 #倒引號,expr是數(shù)值運算done30311.6 shell中的程序控制結(jié)構(gòu)語句1.6.8 break、continue語句、算術(shù)運算、退出腳本命令Break n表示

20、從while或until循環(huán)中跳出,n表示是跳出幾層循環(huán),默認是1 Continue n表示跳過循環(huán)體中在它之后的語句,回到循環(huán)開頭,進行下一次循環(huán)。算術(shù)運算:expr n1 運算符 n2 運算符有+,-, *, /,%退出腳本命令: exit n, n為設(shè)定的退出值31321.6 shell中的程序控制結(jié)構(gòu)語句1.6.8 break、continue語句、算術(shù)運算例:顯示出2-100之間的所有素數(shù)i=2while $i -le 100 do j=2 flag=1 #flag為1表示i是素數(shù) while $j -le expr $i / 2 do if expr $i % $j -eq 0 t

21、hen flag=0;break fi j=expr $j + 1 done if $flag -eq 1 then echo $i is a prime! fi i=expr $i + 1done32331.6 shell中的程序控制結(jié)構(gòu)語句1.6.9 自定義函數(shù)Functionname( )Command-listreturn n 編寫一個測試文件/目錄的函數(shù)testfile( ) #函數(shù)定義if -d $1 then echo $1 is a directory!else echo $1 is not a directory!fireturntestfile $1 #函數(shù)調(diào)用33341.6 shell中的程序控制結(jié)構(gòu)語句1.

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論