




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Linux 操作系統Shell 腳本編程主要內容和學習要求 掌握創建 shell 腳本的基本步驟 學會使用條件測試 掌握 if 條件結構與 case 選擇結構 掌握 for 循環、while 循環和 until 循環結構 學會 shift 命令的使用 學會 shell 腳本的調試 Shell 腳本Shell 腳本如果有一系列你經常使用的Linux命令,你可以把它們存儲在一個文件里,shell可以讀取這個文件并順序執行其中的命令,這樣的文件被稱為腳本文件。shell 腳本按行解釋。 Shell 腳本的編寫 Shell 腳本是純文本文件,可以使用任何文本編輯器編寫 Shell 腳本通常是以 .sh
2、 作為后綴名 Shell 腳本的執行chmod +x script_name./script_namebash script_name 第一行:指定用哪個程序來編譯和執行腳本。 Shell 腳本的格式#!/bin/bash 可執行語句和 shell 控制結構 注釋:以 “ # ” 開頭,可獨占一行,或跟在語句的后面。Shell 腳本#!/bin/sh#!/bin/csh一個 shell 腳本通常由一組 Linux 命令、shell 命令、控制結構和注釋語句構成。在腳本中多寫注釋語句是一個很好的編程習慣 #!/bin/bash# This is the first Bash shell prog
3、ram # ScriptName: greetings.shechoecho e Hello $LOGNAME, cecho its nice talking to you.echo Your present working directory is:pwd # Show the name of present directoryechoecho e The time is date +%T!. nByeechobash greetings.shchmod +x greetings.sh./greetingsShell 腳本舉例echo命令功能說明:顯示文字。 語 法:echo -ne字符串
4、或 echo -help-version 補充說明:echo會將輸入的字符串送往標準輸出。輸出的字符串間以空白字符隔開, 并在最后加上換行號。-n 不進行換行-e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出 n 換行 b 空格.參 數:-n 不要在最后自動換行 -e 若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出:a 發出警告聲; b 刪除前一個字符; c 最后不加上換行符號; f 換行但光標仍舊停留在原來的位置; n 換行且光標移至行首; r 光標移至行首,但不換行; t 插入tab; v 與f相同; 插入字符; nnn 插入nnn(八進制)所代表
5、的ASCII字符; -help 顯示幫助 -version 顯示版本信息#!/bin/bash# This script is to test the usage of read# Scriptname: ex4read.shecho = examples for testing read =echo -e What is your name? cread nameecho Hello $nameechoecho -n Where do you work? readecho I guess $REPLY keeps you busy!echoread -p Enter your job tit
6、le: #自動讀給REPLYecho I thought you might be an $REPLY.echoecho = End of the script =Shell 腳本舉例read命令read variable #讀取變量給variableread x y #可同時讀取多個變量read #自動讀給REPLYread p “Please input: ” #自動讀給REPLY 狀態變量 $? 中保存命令退出狀態的值grep $USER /etc/passwdecho $?grep hello /etc/passwd; echo $?條件測試 條件測試可以根據某個特定條件是否滿足,來選
7、擇執行相應的任務。 Bash 中允許測試兩種類型的條件: 命令成功或失敗,表達式為真或假 任何一種測試中,都要有退出狀態(返回值),退出狀態為 0 表示命令成功或表達式為真,非0 則表示命令失敗或表達式為假。 內置測試命令 test 通常用 test 命令來測試表達式的值x=5; y=10test $x -gt $yecho $? test 命令可以用 方括號 來代替x=5; y=10 $x -gt $y echo $? 表達式測試包括字符串測試、整數測試和文件測試。測試表達式的值方括號前后要留空格!name=Tom $name = Tt? echo $? 2.x 版本以上的 Bash 中可以
8、用雙方括號來測試表達式的值,此時可以使用通配符進行模式匹配。測試表達式的值 $name = Tt? echo $? 字符串測試 -z str 如果字符串 str 長度為 0,返回真 -n str 如果字符串 str 長度不為 0,返回真 str1 = str2 兩字符串相等 str1 != str2 兩字符串不等name=Tom; -z $name ; echo $?操作符兩邊必須留空格!字符串測試name2=Andy; $name = $name2 ; echo $? 整數測試,即比較大小 int1 -eq int2 int1 等于 int2 int1 -ne int2 int1 不等于 i
9、nt2 int1 -gt int2 int1 大于 int2 int1 -ge int2 int1 大于或等于 int2 int1 -lt int2 int1 小于 int2 int1 -le int2 int1 小于或等于 int2x=1; $x -eq 1 ; echo $?x=a; $x -eq 1 ; echo $?整數測試操作符兩邊必須留空格!X 整數測試也可以使用 let 命令或雙圓括號x=1; let $x = 1; echo $?x=1; ($x+1= 2 ); echo $? 只能用于整數測試!整數測試 相應的操作符為:= 、!= 、 、= 、 、 y ); then ech
10、o x is larger than yelif ( x = y); then echo x is equal to yelse echo x is less than yfichkperm.sh#!/bin/bash# Using the old style test command: # filename: perm_check.sh#file=testingif -d $file then echo $file is a directory elif -f $file then if -r $file -a -w $file -a -x $file then # nested if co
11、mmand echo You have read,write,and execute permission on $file. fielse echo $file is neither a file nor a directory. fichkperm2.sh#!/bin/bash# Using the new style test command: # filename: perm_check2.sh#file=./testingif -d $file then echo $file is a directory elif -f $file then if -r $file & -w $fi
12、le & -x $file then # nested if command echo You have read,write,and execute permission on $file. fielse echo $file is neither a file nor a directory. finame_grep#!/bin/bash# filename: name_grep#name=Tomif grep $name /etc/passwd & /dev/null then :else echo $name not found in /etc/passwd exit 2fitellm
13、e#!/bin/bashecho -n How old are you? read ageif $age -lt 0 -o $age -gt 120 thenecho Welcome to our planet! exit 1 fiif $age -ge 0 -a $age -le 12 thenecho Children is the flowers of the countryelif $age -gt 12 -a $age -le 19 thenecho Rebel without a causeelif $age -gt 19 -a $age -le 29 then echo You
14、got the world by the tail!elif $age -ge 30 -a $age -le 39 thenecho Thirty something.elseecho Sorry I askedfitellme2#!/bin/bashecho -n How old are you? read ageif ( age 120 ) thenecho Welcome to our planet! exit 1 fiif (age = 0 & age = 13 & age = 19 & age = 30 & age 0 ) # or $# -gt 0 do echo $* shift
15、doneshft.sh#!/bin/bash# Using shift to step through all the positional parameters.until -z $1 # Until all parameters used up.do echo $1 shiftdoneecho # Extra line feed.exit 0 生成隨機數的特殊變量echo $RANDOM 范圍是: 0, 32767 expr:通用的表達式計算命令表達式中參數與操作符必須以空格分開,表達式中的運算可以是算術運算,比較運算,字符串運算和邏輯運算。expr 5 % 3expr 5 * 3 # 乘
16、法符號必須被轉義隨機數和 expr 命令 字符串操作$#var返回字符串變量 var 的長度$var:m返回$var中從第m個字符到最后的部分$var:m:len返回$var中從第m個字符開始,長度為len的部分$var#pattern刪除$var中開頭部分與pattern匹配的最小部分$var#pattern刪除$var中開頭部分與pattern匹配的最大部分$var%pattern刪除$var中結尾部分與pattern匹配的最小部分$var%pattern刪除$var中結尾部分與pattern匹配的最大部分$var/old/new用new替換$var中第一次出現的old$var/old/n
17、ew用new替換$var中所有的old(全局替換)注:pattern,old 中可以使用通配符。例:ex4strm 的取值從 0 到 $#var-1字符串操作ex4str#!/bin/bashdirname=/usr/bin/local/bin; echo dirname=$dirnameecho -n $#dirname=; sleep 4;echo $#dirnameechoecho -n $dirname:4=; sleep 4;echo $dirname:4echoecho -n $dirname:8:6=; sleep 4; echo $dirname:8:6echoecho -n
18、$dirname#*bin=; sleep 4; echo $dirname#*binechoecho -n $dirname#*bin=; sleep 4;echo $dirname#*binechoecho -n $dirname%bin=; sleep 4;echo $dirname%binechoecho -n $dirname%bin=; sleep 4;echo $dirname%binechoecho -n $dirname%bin*=; sleep 4;echo $dirname%bin*echoecho -n $dirname%bin*=; echo $dirname%bin
19、*echoecho -n $dirname/bin/sbin=; echo $dirname/bin/sbinechoecho -n $dirname/bin/lib=; echo $dirname/bin/libechoecho -n $dirname/bin*/lib=; echo $dirname/bin*/libsh x 腳本名該選項可以使用戶跟蹤腳本的執行,此時 shell 對腳本中每條命令的處理過程為:先執行替換,然后顯示,再執行它。shell 顯示腳本中的行時,會在行首添加一個加號 “ + ”。sh v 腳本名在執行腳本之前,按輸入的原樣打印腳本中的各行,打印一行執行一行。sh
20、n 腳本名對腳本進行語法檢查,但不執行腳本。如果存在語法錯誤,shell 會報錯,如果沒有錯誤,則不顯示任何內容。腳本調試編程小結:變量 局部變量、環境變量(export、declare -x) 只讀變量、整型變量例:declare -i x; x=hello; echo $x0 位置參量($0,$1,.,$*,$,$#,$,$?) 變量的間接引用(eval, $!str)例:name=hello; x=name; echo $!xhello 命令替換(cmd、$(cmd)) 整數運算 declare 定義的整型變量可以直接進行運算, 否則需用 let 命令或 $.、$(.) 進行整數運算。編
21、程小結:輸入輸出 輸入:readread var1 var2 .readread p 提示 輸出:printfprintf %-12.5f t %d n 123.45 8format以%開頭flagfield widthprecision格式符-:左對齊+:輸出符號0:空白處添0空格:前面加一空格字段寬度小數點后輸出位數cdefgsoxbnrtv”% REPLY REPLY輸出參數用空格隔開 字符串測試編程小結:條件測試 -z string 如果字符串string長度為0,返回真 -n string 如果字符串string長度不為0,返回真 str1 = str2 兩字符串相等(也可以使用 =
22、 ) str1 != str2 兩字符串不等操作符兩邊必須留空格! str1 = str2 兩字符串相等(也可以使用 = ) str1 != str2 兩字符串不等 str1 str2 str1大于str2,按ASCII碼比較 str1 Tom ; echo $?編程小結:條件測試 整數測試 int1 -eq int2 int1 等于 int2 int1 -ne int2 int1 不等于 int2 int1 -gt int2 int1 大于 int2 int1 -ge int2 int1 大于或等于 int2 int1 -lt int2 int1 小于 int2 int1 -le int2
23、int1 小于或等于 int2注意這兩種方法的區別!int1 = int2 int1 等于 int2int1 != int2 int1 不等于 int2int1 int2 int1 大于 int2int1 = int2 int1 大于或等于 int2int1 int2 int1 小于 int2int1 echo -ARE- echo echo let i+=1 done# Now, call the functions.funexit 0ex4fun3.sh# f1 # Will give an error message, since function f1 not yet defined.
24、# declare -f f1 # This doesnt help either.# f1 # Still an error message.# However.f1 () echo Calling function f2 from within function f1.f2f2 () echo Function f2.# f1 # Function f2 is not actually called until this point# although it is referenced before its definition.# This is permissible. 向函數傳遞參數
25、函數的調用例:ex4fun4.sh 函數與命令行參數例:ex4fun5.sh return 與 exit例:ex4fun6.sh向函數傳遞參數 例:ex4fun4.sh#!/bin/bash# Functions and parametersDEFAULT=default # Default param value.func2 () if -z $1 # Is parameter #1 zero length? then echo -Parameter #1 is zero length - else echo -Param #1 is $1 -fivariable=$1:-$DEFAULT
26、echo variable = $variable if -n $2 then echo - Parameter #2 is $2 -fireturn 0echoecho Nothing passedfunc2 # Called with no paramsechoecho One parameter passed.func2 first # Called with one paramechoecho Two parameters passed.func2 first second # Called with two paramsechoecho second passed.func2 second # The fir
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中小學公共安全教育-救起溺水者后的急救
- 2024年裁判員法規試題及答案
- 2024年游泳救生員考試的指南及試題答案
- 農作物種子經濟利益分析試題及答案
- 電力企業安全培訓課件
- 人教版初中八年級英語上冊期末復習完形填空30篇
- 電業系統培訓課件
- 2024年籃球裁判員等級考試重要知識點及試題答案
- 2024年農作物繁育員的技能提升試題及答案
- 項目管理知識體系的構建試題及答案
- 石景山區行政事業單位資產清查業務培訓
- 《今天怎樣做教師-點評100個教育案例》讀書分享會PPT模板
- 高效節水灌溉技術與灌溉排水工程設計及案例分析
- 《將軍胡同》閱讀試題及答案
- 2022年常德市漢壽縣社區工作者招聘考試試題
- 小學畢業班數學老師家長會完美版資料
- 福建土樓介紹
- 文藝復興時期服裝風格
- 中華茶文化智慧樹知到答案章節測試2023年青島職業技術學院
- 《愛麗絲漫游奇境》閱讀指導
- 非物質文化遺產代表性項目申報書
評論
0/150
提交評論