变量,还是变量
測(cè)試變量“存在與否”的基本用法
語(yǔ)法${待測(cè)變量-默認(rèn)值}
判斷條件,如果變量不存在,則符合判斷,以傳回默認(rèn)值作為處置
意思是,有的傳就傳,如果沒有就傳回一個(gè)指定值
例子:
test=haha
r=${test-'testhaha'}
echo $r 返回haha
unset test
r=${test-'testhahah'}
echo $r 這適合test值為空,那么$r 傳回默認(rèn)值為testhaha
測(cè)試變量不存在或值為空,傳回一個(gè)默認(rèn)值
語(yǔ)法${待測(cè)變量:-默認(rèn)值}?? #多個(gè)個(gè)冒號(hào)意思是測(cè)試存在或者空值兩種情況
意思是 有就傳有,如果沒有,就傳一個(gè)指定值
test=
[root@centos129 ~]# r=${test-'testhaha'}
[root@centos129 ~]# echo $r
兩個(gè)的區(qū)別!上一個(gè)例子如果沒有或?yàn)榭?#xff0c;第一個(gè)返回空置
[root@centos129 ~]# r=${test:-'testhaha'}
[root@centos129 ~]# echo $r
testhaha
例子,一定要有信件文件名
?heiehi=${finename:-'test.txt'}
[root@centos129 tmp]# cat > test.txt << ENDF
> From: zhoutao@zhoutao.com
> To:heihei@zhoutao.com
> Subject:TEST ONCE
> this is a test email do not replay
> ENDF
[root@centos129 tmp]# echo $heiehi
test.txt
#!/bin/bash
fn=${1:?'錯(cuò)誤!請(qǐng)?zhí)峁┮獎(jiǎng)h除的目錄'} #對(duì)命令傳入的第一個(gè)參數(shù)$1作檢查,如為空,
則不在執(zhí)行
echo '你要執(zhí)行的目錄是'
echo "rm -rf ~/$fn"
bash test.sh
test.sh: line 2: 1: 錯(cuò)誤!請(qǐng)?zhí)峁┮獎(jiǎng)h除的目錄
[root@centos129 tmp]# bash test.sh /tmp123
你要執(zhí)行的目錄是
rm -rf ~//tmp123
測(cè)試變量的存在性
語(yǔ)法¥{待測(cè)變量:+真值}
如果變量存在且其值非空,那么傳回真值,不然傳回空值
如:whoamim="zhoutao"
r=${whoamim:+'true'}
echo $r
顯示為true
cat test.sh
#!/bin/bash
exec grep -F ${1+"$@"}
[root@centos129 tmp]# bash test.sh Mem /proc/meminfo
MemTotal:????? 1025740 kB
MemFree:??????? 506676 kB
取得字符串切片
語(yǔ)法一:
myname="zhoutao hahaha"
myname1=${myname:4}? #由指定位置開始,截取子字符串到字符串結(jié)束
echo myname1
tao hahaha
語(yǔ)法二:
${變量:位置起點(diǎn):長(zhǎng)度}
myname="zhoutao hahaha"
myname1=${myname:4:1}
echo $myname1
t
取得部分位置參數(shù)
命令行第一個(gè)參數(shù)用$1表示,跌入個(gè)用$2表示,其他以此類推,$@則代表所有的參數(shù)
例:test.sh a b c 第一個(gè)參數(shù)為a 第2個(gè)為y 第三個(gè)為z,那么$1=a;$2=y $3=z,命令本身為$0 值為test.sh
$0,$1,$2,$3,$4等為位置參數(shù)
取得部分位置參數(shù)的用法有2個(gè)
${@:起點(diǎn)}
cat test.sh
#!/bin/bash
echo $0 ## $0為命令本省的名字
echo ${@:1:2} 由第一個(gè)位置參數(shù)開始(注意:第0個(gè)是$0),取得1后面2個(gè)位置參數(shù)
計(jì)算字符長(zhǎng)度
¥{#變量名稱}
例:
filename="123"
echo ${#filename}
執(zhí)行結(jié)果顯示字符串長(zhǎng)度為3
外部程序expr計(jì)算字符串長(zhǎng)度
length是字符串expr的長(zhǎng)度,用來(lái)指定要做計(jì)算字符串長(zhǎng)度的這個(gè)操作
string=“i'm zhoutao”
string1=`expr length "$string"`
方法2
string1=`expr "$string" : ".*"`?? ##:后面接的是.*是代表任意多個(gè)字符串包括空格
[root@centos129 tmp]# echo $string1
11
變量擴(kuò)展:對(duì)比樣式
所謂對(duì)比樣式,目的是想截取變量值(字符串)的某以部分,其做法是,將符合樣式的部分字符串刪除或取代
語(yǔ)法${變量#樣式}
由前面開始,對(duì)比變量值,刪除最短項(xiàng)符合的字符串
ilename="/usr/local/games/"
[root@centos129 tmp]# r=${filename#/*/}? ##:/*/的意思是,飯一對(duì)斜線之間有字符串者,空字符串也可,對(duì)比符合;#號(hào)表示由前面最短的,所有,對(duì)比到最短的字符串是/usr/local。將其去除,然后傳回剩下的字符串,并設(shè)值給r
[root@centos129 tmp]# echo $r
local/games/
[root@centos129 tmp]# r=${filename#/*/*/}
[root@centos129 tmp]# echo $r
games/
[root@centos129 tmp]# filename="//usr/local/games/"
[root@centos129 tmp]# r=${filename#/*/}
[root@centos129 tmp]# echo $r
usr/local/games/
[root@centos129 tmp]# r=${filename#/*l}
[root@centos129 tmp]# echo $r
ocal/games/
由前面對(duì)比刪最長(zhǎng)的!
語(yǔ)法${變量##樣式}
解釋:由前面最左邊開始,對(duì)比變量值,刪除最長(zhǎng)相符合的字符串
[root@centos129 tmp]# filename="/usr/local/games/"
[root@centos129 tmp]# r=${filename##/*/}? 想要對(duì)比樣式是/*/,意思是說(shuō),凡一對(duì)斜線之間所有的字符串,對(duì)比符合,##取最長(zhǎng)的,所以,對(duì)比到最長(zhǎng)的字符串是/usr/local/。故將它刪除,然后傳回剩下的字符串,并設(shè)值給r
[root@centos129 tmp]# echo $r
[root@centos129 tmp]# filename="/usr/local/games"
[root@centos129 tmp]# r=${filename##/*/}
[root@centos129 tmp]# echo $r
games
由字符串后面對(duì)比,刪除相符者
1:由后面對(duì)比,刪最短的
語(yǔ)法:${變量%樣式}
解釋:由后面(最右邊)開始,對(duì)比變量值,刪除,最短相符的字符串
filename="/usr/local/games"
[root@centos129 tmp]# r=${filename%s*}
[root@centos129 tmp]# echo $r
/usr/local/game
[root@centos129 tmp]# r=${filename%/*}
[root@centos129 tmp]# echo $r
/usr/local
由后面對(duì)比刪最長(zhǎng)的!
語(yǔ)法:${變量%%樣式}
filename="www.zhoutao.name"
[root@centos129 tmp]# r=${filename%%.*}?? ##:從右邊開始,凡點(diǎn)后面的字符串或者是空都將它刪除掉
[root@centos129 tmp]# echo $r
www
取消或刪除部分字符串!
1:只替換第一個(gè)對(duì)比符合的字符串
語(yǔ)法:${變量/樣式/替換字符串}
解釋:如變量中,有符合樣式的字符串(取最長(zhǎng))則使用替換字符串予以取代
例子:filename="www.zhoutao.com"
[root@centos129 tmp]# r=${filename/www/qqq} ##:把www換成qqq
[root@centos129 tmp]# echo $r
qqq.zhoutao.com
2:替換全部對(duì)比符合的字符串
語(yǔ)法:${變量//樣式/替換字符串}
解釋:如果變量中,有符合樣式的字符串(取最長(zhǎng))則使用替換字符串全部予以取代
例:filename="www.zhoutao.com"
[root@centos129 tmp]# r=${filename//./$$} 把.替換城$$(shell本身的PID)
[root@centos129 tmp]# echo $r
www3651zhoutao3651com
把對(duì)比符合的字符串刪除
1:只刪除一個(gè)
語(yǔ)法:${變量/樣式/}
解釋:刪除第一個(gè)符合樣式的字符串
filename=www.zhoutao.com
r=${filename/www/}
[root@centos129 tmp]# echo $r
.zhoutao.com
2:刪除全部
filename="ww.w.zhoutao.com"
[root@centos129 tmp]# r=${filename//ww./}
[root@centos129 tmp]# echo $r
w.zhoutao.com
要求樣式在句首或句尾
在對(duì)比樣式時(shí),如在樣式前面加#,則該樣式要出現(xiàn)在變量值的開頭才算符合
str="Yes, This is a TITLE"
[root@centos129 tmp]# r=${str/#T* /}? ##:對(duì)比的是位置在句首,T字符后任意長(zhǎng)度的字符,最后以空格符結(jié)尾的字符串,因$str中沒有這樣的樣式,所以,真?zhèn)€str變量值被原封不動(dòng)的返回給r
[root@centos129 tmp]# echo $r
Yes, This is a TITLE
如果樣式在句首
例:str="TYes, This is a TITLE"
[root@centos129 tmp]# r=${str/#T* /}
[root@centos129 tmp]# echo $r
TITLE
如果改成${str/T*/},表示樣式不一定要位于句首,那么,在$str中就有符合的樣式
str="Yes, This is a TITLE"
[root@centos129 tmp]# r=${str/T* /}
[root@centos129 tmp]# echo $r
Yes, TITLE
在對(duì)比樣式時(shí),如在樣式前面加上%,則改樣式要出現(xiàn)在變量值的最后才算符合
取變量名稱列表,數(shù)組索引列表
1:取變量名稱列表
語(yǔ)法:${!開頭字符串@} 或者 ${!開頭字符串*}
解釋:把所有以指定字符串開頭的變量名稱列出,各變量之間,用$IFS,定義的第一個(gè)分隔符(通常是空格)隔開
?????????????????????? filename="test1"
[root@centos129 tmp]# filename1="test2"
[root@centos129 tmp]# filename3="test3"
[root@centos129 tmp]# echo ${!filenam@}
filename filename1 filename3
?
轉(zhuǎn)載于:https://blog.51cto.com/fghjk/782256
總結(jié)
- 上一篇: iscsi存储的简单配置
- 下一篇: 三维模型部件分离