【文章內(nèi)容簡(jiǎn)介】
or一般為: eq: 數(shù)值相等 ne: 數(shù)值不相等 gt: 大于 lt: 小于 le: 小于等于 ge: 大于等于 測(cè)試兩個(gè)數(shù)是否相等 $NUMBER=130 $[ ―NUMBER‖ –eq ―130‖ ] $echo $? 0 測(cè)試兩個(gè)表達(dá)式 $[ “ 990” –le ―995‖ –a ―123‖ –gt ―33‖] $echo $? 0 控制流結(jié)構(gòu) ( 1) if 語(yǔ)句 if [expression1] then mandlist [elif [expression2] then thenmandlist] … [else elsemandlist] fi 在這里,一個(gè) expression是一系列命令。 也可采用如下格式: if [expression1]。then mandlist [elif [expression2]。then thenmandlist] … [else elsemandlist] fi 三種典型的 if結(jié)構(gòu) 第一種 if語(yǔ)句 : if expression then thenmands fi 第二種 if語(yǔ)句 : if expression then thenmands else elsemands fi ? 第 3種 if語(yǔ)句 if expression1 then thenmands elif expression2 then elif1mands elif expression3 then elif2mands … else elsemands fi 例如: $cat !/bin/sh Usage: ./grade_evaluate grade grade should be between 0 and 100, including 0 and 100 if [ $1 –lt 39。039。 ] 。 then echo Error: invalid grade elif [ $1 –lt 39。6039。 ] 。 then echo 39。no pass39。 elif [ $1 –lt 39。7039。 ] 。 then echo pass elif [ $1 –lt 39。8039。 ] 。 then echo 39。good39。 elif [ $1 –le 39。10039。 ] 。 then echo 39。excellent39。 else echo39。Error: invalid grade39。 fi ( 2) case 語(yǔ)句 格式: case teststring in 模式 1) mandlist1 。 模式 2) mandlist2 。 … *) mandlistN+1 。 esac case 語(yǔ)句(續(xù)) in 的每一模式必須以右括號(hào) )結(jié)束 ,取值可以為變量或常數(shù),匹配發(fā)現(xiàn) string1取值符合某一模式后,其間所有命令開(kāi)始執(zhí)行直至 。.如果模式 模式 2中沒(méi)有和strings1相符合的,則星號(hào)( *)下面的語(yǔ)句被執(zhí)行。模式匹配符 *代表任意字符 ,?表示任意單個(gè)字符 ,[..]表示類或范圍中任意字符。 $cat !/bin/sh echo –n Do you want to continue this operation? [n] read yesno case $yesno in y | Y | Yes | yes) echo system will continue this operation 。 n | N | no | NO) echo system will skip this operation 。 *) echo Incorrect input exit 1 。 esac ( 3) for語(yǔ)句 有兩種使用 for 語(yǔ)句的表達(dá)式: 第一種形式是: for var in list do mands done 第二種形式是: for var do statements Done 使用這種形式時(shí) , 變量 var的取值是位置參數(shù) 。 $cat !/bin/sh for i in 1 3 5 7 9 do echo $I done ( 4) while和 until循環(huán) while循環(huán)的一般格式為: while expression do statements done $cat !/bin/sh while read string do echo ‖your string is $string‖ done echo 39。Thank you, Bye!39。 ! /bin/bash program test_while count=1 while [n $*] do echo This is parameter number $count $1 shift let count=count+1 done until語(yǔ)句的格式為: until expression do mands done ! /bin/bash program test_until count=1 until [z $*] do echo This is parameter number $count $1 shift let count=count+1 done $cat !/bin/sh until read string do echo ‖your string is $string‖ done echo 39。Thank you, Bye!39。 輸出結(jié)果 $ctrl +D your string is (程序繼續(xù) ) $abc Thank you, Bye! Shell還提供了 true和 false兩條命令用于建立無(wú)限循環(huán)結(jié)構(gòu)的需要,它們的返回狀態(tài)分別是總為 0或總為非 0 (5) break and continue break 退出循環(huán) $cat !/bin/sh while : do read string if [ $? != 0 ]。then break fi done $cat !/bin/sh for ((i=0 。i20 。i=$i+1)) do echo –n $i if [ $i –lt 10 ] 。 then echo39。39。 continue fi echo 39。This is a two digital numeric39。 done shell 內(nèi)嵌命令 命令 功能 : 空 ,永遠(yuǎn)返回為 t ru e . 從當(dāng)前 she l l 中執(zhí)行操作 break 退出 for、 while、 until或 case語(yǔ)句 cd 改變當(dāng)前目錄 pwd 顯示當(dāng)前目錄 continue 結(jié)束本次循環(huán),進(jìn)行下一次循環(huán) echo 反饋信息到標(biāo)準(zhǔn)輸出 eval 讀取參數(shù) ,執(zhí)行結(jié)果命令 exec 執(zhí)行命令替代 shell ,它不創(chuàng)建新的進(jìn)程 ,而是用被執(zhí)行的命令取代當(dāng)前的 shell進(jìn)程 exit 退出當(dāng)前 shell export 導(dǎo)出變量,使當(dāng)前 shell可利用它 read 從標(biāo)準(zhǔn)輸入讀取一行文本 readonly 使變量變成只