目錄
??shell變量替換
??定義變量
??shell變量運(yùn)算
??整數(shù)運(yùn)算
??小數(shù)運(yùn)算
?????博客主頁:大蝦好吃嗎的博客
? ???專欄地址:Linux從入門到精通
shell變量替換
${變量#匹配規(guī)則} ????????????????????????????????????????#從頭開始匹配,最短刪除
${變量##匹配規(guī)則}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? #從頭開始匹配,最長刪除
${變量%匹配規(guī)則}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#從尾開始匹配,最短刪除
${變量%%匹配規(guī)則}? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #從尾開始匹配,最長刪除
${變量/舊字符串/新字符串}? ? ? ? ? ? ? ? ? ? ? ? ? #替換字符串,僅替換第一個(gè)
${變量//舊字符串/新字符串} ????????????????????????#替換字符串,替換全部
例1:從前往后刪變量內(nèi)容
[root@daxia ~]# Url=z3.www.baidu.com.cn
[root@daxia ~]# echo $Url
z3.www.baidu.com.cn
[root@daxia ~]# echo ${Url#*.} #刪除以 . 分割的第一個(gè)字段
www.baidu.com.cn
[root@daxia ~]# echo ${Url##*.} #僅保留最后一個(gè)字段
cn
例2:從后往前刪變量內(nèi)容
[root@daxia ~]# echo $Url
z3.www.baidu.com.cn
[root@daxia ~]# echo ${Url%.*} #刪除最后一個(gè)字段
z3.www.baidu.com
[root@daxia ~]# echo ${Url%%.*} #僅保留第一個(gè)字段
z3
例3:變量內(nèi)容替換
[root@daxia ~]# echo $Url
z3.www.baidu.com.cn
[root@daxia ~]# echo ${Url/z3/l4} #替換第一個(gè)z3為l4
l4.www.baidu.com.cn
[root@daxia ~]# echo ${Url//z3/l4} #替換全部z3為l4
l4.www.baidu.com.cn
腳本題: 把/data下所有.txt文件,改名.bak,并tar包備份到/backup,最后解壓還原文件名。合并起來做可能不理解,分開后嘗試一下。
[root@daxia ~]# mkdir /data
[root@daxia ~]# mkdir /backup
[root@daxia ~]# touch /data/file{1..9}.txt
[root@daxia ~]# vim rename.sh
#!/bin/bash
File_txt=$(find /data -type f -iname "*.txt" |xargs > /data/txt.f)
for a in $(cat /data/txt.f)
do
mv $a $a.bak
done
?
tar zcf /backup/file.tar.gz /data/*.bak
?
File_txt=$(find /data -type f -iname "*.bak" |xargs > /data/bak.f)
for b in $(cat /data/bak.f)
do
mv $b ${b%.*}
done
?
?
#執(zhí)行腳本
[root@daxia ~]# sh rename.sh
tar: 從成員名中刪除開頭的“/”
定義變量
happiness="towards the sea, with spring flowers blossoming,the the" 執(zhí)行腳本,輸出變量,并要求:
打印變量值字符串長度
刪除所有的the
替換第一個(gè)the為that
替換所有the為that 用戶按“1|2|3|4” ,輸出相應(yīng)選項(xiàng)內(nèi)容,q|Q 退出
[root@daxia ~]# vim test1.sh
#!/bin/bash
happiness="towards the sea, with spring flowers blossoming,the the"
?
echo $happiness
?
cat <<EOF
1) 打印happiness長度
2) 刪除所有的the
3) 替換第一個(gè)the為that
4) 替換所有the為that
EOF
?
read -p "請輸入數(shù)字 1|2|3|4,或 q|Q: " var
?
if [ $var == q ];then
exit
fi
?
if [ $var == Q ];then
exit
fi
?
if [ $var -eq 1 ];then
echo "當(dāng)前happiness變量的長度是:${#happiness}"
fi
?
if [ $var -eq 2 ];then
echo ${happiness//the/}
fi
?
if [ $var -eq 3 ];then
echo ${happiness/the/that}
fi
?
if [ $var -eq 4 ];then
echo ${happiness//the/that}
fi
shell變量運(yùn)算
整數(shù)運(yùn)算
expr、$(())、$[],不支持小數(shù)運(yùn)算
a+b 加 a-b 減 a*b 乘(expr計(jì)算時(shí),用 * ) a/b 除 a%b 余
例1:
[root@daxia ~]# a=20
[root@daxia ~]# b=10
[root@daxia ~]# expr $a + $b
30
[root@daxia ~]# echo $(($a+$b))
30
[root@daxia ~]# echo $[$a+$b]
30
例2:遞增和遞減
[root@daxia ~]# echo $((a++)) #表示先打印變量a,然后在加一位數(shù)值變成21,
20
[root@daxia ~]# echo $((a--))
21
[root@daxia ~]# echo $((++b)) #表示先加一位數(shù)值,在打印變量
11
[root@daxia ~]# echo $((--b))
10
[root@daxia ~]# echo $((100*(1+100)/2)) #求1加到100的和
5050
小數(shù)運(yùn)算
bc加”|“管道符使用,可以將前面打印的結(jié)果給bc工具處理。
[root@daxia ~]# yum -y install bc #安裝bc工具
?
[root@daxia ~]# echo "2*4"
2*4
[root@daxia ~]# echo "2*4" | bc
8
[root@daxia ~]# echo "2^4" | bc
16
[root@daxia ~]# echo "scale=2;3/2" | bc
1.50
[root@daxia ~]# awk 'BEGIN{print 3/2}' #awk也可以計(jì)算
1.5
腳本題: 例1:查看內(nèi)存使用率,僅保留整數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-457962.html
[root@daxia ~]# vim mem.sh
#!/bin/bash
Mem_use=$(free -m |grep ^M |awk '{print $3/$2*100}')
if [ ${Mem_use%.*} -ge 80 ];then
echo "memory is overfull: ${Mem_use%.*}%"
else
echo "memory is OK: ${Mem_use%.*}%"
fi
?
[root@daxia ~]# sh mem.sh
memory is OK: 47%
例2:查看磁盤使用狀態(tài),使用率超出80%就報(bào)警 思路: 怎么查看磁盤 怎么提取使用率 整數(shù)判斷文章來源地址http://www.zghlxwxcb.cn/news/detail-457962.html
[root@daxia ~]# vim disk.sh
?
#!/bin/bash
Disk=$(df -h |grep /$ |awk '{print $(NF-1)}')
?
if [ ${Disk%\%} -ge 80 ];then
echo "你的磁盤使用率過高:$Disk"
else
echo "你的磁盤使用率正常:$Disk"
fi
?
[root@daxia ~]# sh disk.sh
你的磁盤使用率正常:31%
到了這里,關(guān)于【Linux腳本篇】shell變量的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!