【正文】
的表達(dá)式含空格,需加雙引號(hào) $let c=a+b $echo ―The value of c is $c.‖ The value of c is 21. $let ―a *= b‖ $echo ―The new value of a is $a。 測試文件狀態(tài) (續(xù)) r文件名:如果文件存在且可讀則為真 w文件名:如果文件存在且可寫則為真 x文件名:如果文件存在且可執(zhí)行則為真 s文件名:如果文件存在且至少有一個(gè)字符則為真 d文件名:如果文件存在且為目錄則為真 f文件名:如果文件存在且為普通文件則為真 e文件名:如果文件存在則為真 c文件名:如果文件存在且為字符型特殊文件則為真 b文件名:如果文件存在且為塊特殊文件則為真 測試文件 是否可寫 $ls –l rwr—r 1 dave admin 0 May 15 11:29 $[ w ] $echo $? 0 $test –w $echo $? 0 簡單的算術(shù)運(yùn)算 格式: $[expression] 例如: var1=2 var2=$[var1*10+1] 則: var2的值為 21。/usr/bin/aaa/bbb39。 0表示沒有錯(cuò)誤,其他任何值表明有錯(cuò)誤。 ? 常用的預(yù)定義變量: $ 傳遞到腳本的參數(shù)個(gè)數(shù) $* 以一個(gè)單字符串顯示所有向腳本傳遞的參數(shù) 。 位置變量參數(shù)(續(xù)) ? 如向腳本傳送 Did You See The Full Moon信息: $0 $1 $2 $3 $4 $5 $6 腳本名字 Did You See The Full Moon $7 $8 $9 (1) 在腳本中使用位置參數(shù) $cat param !/bin/sh param echo This is the script name : $0 echo This is the first parameter: $1 echo This is the 2nd parameter : $2 echo This is the third parameter: $3 echo This is the 6th parameter : $6 echo This is the 7th parameter : $7 輸出結(jié)果 執(zhí)行: $ ./param Did You See The Full Moon This is the script name : ./param This is the first parameter: Did This is the 2nd parameter : You This is the third parameter: See This is the 6th parameter : Moon This is the 7th parameter : (2) 向系統(tǒng)命令傳遞參數(shù) $cat findfile !/bin/sh findfile find / name $1 –print 預(yù)定義變量 ? 預(yù)定義變量是在 shell一開始時(shí)就定義了的變量,用戶不能重定義它。 ? 參數(shù)從第 1個(gè)開始,在第 9個(gè)結(jié)束;每個(gè)訪問參數(shù)前要加 $符號(hào)。 ( 5) 將變量導(dǎo)出到子進(jìn)程 $cat father !/bin/sh father script echo this is the father FILM=A Few Good Men echo I like the film :$FILM ./child call the child script echo back to father echo and the film is :$FILM 將變量導(dǎo)出到子進(jìn)程 (續(xù) ) $cat child !/bin/sh child script echo called from father..i am the child echo film is :$FILM FILM=Die Hard echo changing film to :$FILM 顯示結(jié)果 this is the father I like the film :A Few Good Men called from father..i am the child film is : changing film to :Die Hard back to father and the film is :A Few Good Men $cat father2 !/bin/sh father2 script echo this is the father FILM=A Few Good Men echo I like the film :$FILM call the child script but export varible first export FILM 在調(diào)用腳本前導(dǎo)出變量 ./child echo back to father echo and the film is :$FILM 輸出結(jié)果 $ ./father2 this is the father I like the film :A Few Good Men called from father..i am the child film is :A Few Good Men changing film to :Die Hard back to father and the film is :A Few Good Men 位置變量參數(shù) ? 如果要向一個(gè) shell腳本傳遞信息,可以使用位置參數(shù)完成此功能。 使用 env命令可以查看所有的環(huán)境變量 。 ? 環(huán)境變量應(yīng)用于用戶進(jìn)程前,必須用export命令導(dǎo)出。登錄進(jìn)程稱為父進(jìn)程。 ( 1)顯示變量 ? 使用 echo命令輸出單個(gè)變量的值,此時(shí)要在變量名前加 $ ? $ WEEK=Satur ? $ echo Today is $WEEKday 輸出: Today is 使用花括號(hào)來告訴 shell我們要顯示的變量: $ echo Today is ${WEEK}day 輸出: Today is Saturday ( 2)清除變量: ? 使用 unset命令刪除變量的賦值 $ Z=hello $ echo $Z hello $ unset Z $ echo $Z $ ( 3)顯示所有本地 shell變量: ? 使用 set命令顯示所有本地定義的 shell變量 ( 4)結(jié)合變量值 Echo ${variablename1}${variablename2} $A=‘Hello,‘ $B=‘World‘ $echo ${A}${B} Hello,World ( 5)設(shè)置變量的默認(rèn)值 Bourne Shell允許對變量設(shè)置默認(rèn)值 , 其格式如下: ${variable:defaultvalue} $color=blue $echo The sky is ${color:grey} today 輸出結(jié)果: The sky is blue today $color=blue $unset color $echo The sky is ${color:grey} today The sky is grey today $echo ${color} $ 改變變量的值,格式如下: ${variable:=value} $color=blue $unset color $echo The sky is ${color:=grey} today The sky is grey today