8个实用的Shell脚本分享
几个Shell脚本的例子,觉得还不错。
【例子:001】判断输入为数字,字符或其他
#!/bin/bash
read-p"Enteranumberorstringhere:"input
case$inputin
[0-9])echo-e"Goodjob,Yourinputisanumberic!\n";;
[a-zA-Z])echo-e"Goodjob,Yourinputisacharacter!\n";;
*)echo-e"Yourinputiswrong,inputagain! \n" ;;
esac
【例子:002】求平均数
#!/bin/bash
#Calculatetheaverageofaseriesofnumbers.
SCORE="0"
AVERAGE="0"
SUM="0"
NUM="0"
whiletrue;do
echo-n"Enteryourscore[0-100%]('q'forquit):";readSCORE;
if(("$SCORE"<"0")) ||(("$SCORE">"100"));then
echo"Beserious. Common,tryagain:"
elif["$SCORE"=="q"];then
echo"Averagerating:$AVERAGE%."
break
else
SUM=$[$SUM+$SCORE]
NUM=$[$NUM+1]
AVERAGE=$[$SUM/$NUM]
fi
done
echo"Exiting."
【例子:003】自减输出
[scriptname:doit.sh]
while(($#>0))
do
echo$*
shift
done
/>./doit.shabcde
abcde
bcde
cde
de
e
【例子:004】在文件中添加前缀
#人名列表
#catnamelist
Jame
Bob
Tom
Jerry
Sherry
Alice
John
#脚本程序
#catnamelist.sh
#!/bin/bash
fornamein$(catnamelist)
do
echo"name="$name
done
echo"Thenameisoutofnamelistfile"
#输出结果
#./namelist.sh
name= Jame
name= Bob
name= Tom
name= Jerry
name= Sherry
name= Alice
name= John
【例子:005】批量测试文件是否存在
[root@host~]#cattestfile.sh
#!/bin/bash
forfileintest*.sh
do
if[-f$file];then
echo"$fileexisted."
fi
done
[root@host~]#./testfile.sh
test.shexisted.
test1.shexisted.
test2.shexisted.
test3.shexisted.
test4.shexisted.
test5.shexisted.
test78.shexisted.
test_dev_null.shexisted.
testfile.shexisted.
【例子:005】用指定大小文件填充硬盘
[root@host~]#df-ih/tmp
Filesystem Inodes IUsed IFreeIUse%Mountedon
/dev/mapper/vg00-lvol5
1000K 3.8K 997K 1%/tmp
[root@host~]#catcover_disk.sh
#!/bin/envbash
counter=0
max=3800
remainder=0
whiletrue
do
((counter=counter+1))
if[${#counter}-gt$max];then
break
fi
((remainder=counter%1000))
if[$remainder-eq0];then
echo-e"counter=$counter\tdate="$(date)
fi
mkdir-p/tmp/temp
cat<testfile>"/tmp/temp/myfile.$counter"
if[$?-ne0];then
echo"FailedtowritefiletoDisk."
exit1
fi
done
echo"Done!"
[root@host~]#./cover_disk.sh
counter=1000 date=WedSep1009:20:39HKT2014
counter=2000 date=WedSep1009:20:48HKT2014
counter=3000 date=WedSep1009:20:56HKT2014
cat:writeerror:Nospaceleftondevice
FailedtowritefiletoDisk.
ddif=/dev/zeroof=testfilebs=1Mcount=1
【例子:006】通过遍历的方法读取配置文件
[root@host~]#cathosts.allow
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
[root@host~]#catreadlines.sh
#!/bin/envbash
i=0
whilereadLINE;do
hosts_allow[$i]=$LINE
((i++))
done<hosts.allow
for((i=1;i<=${#hosts_allow[@]};i++));do
echo${hosts_allow[$i]}
done
echo"Done"
[root@host~]#./readlines.sh
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
Done
【例子:007】简单正则表达式应用
[root@host~]#catregex.sh
#!/bin/envsh
#Filename:regex.sh
regex="[A-Za-z0-9]{6}"
if[[$1=~$regex]]
then
num=$1
echo$num
else
echo"Invalidentry"
exit1
fi
[root@host~]#./regex.sh123abc
123abc
#!/bin/envbash
#Filename:validint.sh
validint(){
ret=`echo$1|awk'{start=match($1,/^-?[0-9]+$/);if(start==0)print"1";elseprint"0"}'`
return$ret
}
validint$1
if[$?-ne0];then
echo"WrongEntry"
exit1
else
echo"OK!Inputnumberis:"$1
fi
【例子:008】简单的按日期备份文件
#!/bin/bash NOW=$(date+"%m-%d-%Y") #当前日期 FILE="backup.$NOW.tar.gz" #备份文件 echo"Backingupdatato/tmp/backup.$NOW.tar.gzfile,pleasewait..." #打印信息 tarxcvf/tmp/backup.$NOW.tar.gz/home//etc//var #同时备份多个文件到指定的tar压缩文件中 echo"Done..."