由于最近学java写了一个类似功能,就想着用bash 也写个看看,练下手。代码放出:
#!/bin/bash
s=asffsdAd32DSsdi@#!\$fihiZFSF87768Z
a=0
A=0
b=0
length=`echo ${#s}`
for((i=0;i< lengthibr>do
e=`echo ${s:$i:1}`
if [[ $e = [[:lower:]] ]]
then
a=$[ $a+1 ]
elif [[ $e = [[:upper:]] ]]
then
A=$[ $A+1 ]
else
b=$[ $b+1 ]
fi
done
echo "a-z" have nuber: $a
echo "A-Z" have number: $A
echo "Other" have number: $b
如果字符串中有$字符必须转义,否则影响结果,下面给出运行结果。
[root@wangzi test]# sh charat
a-z have nuber: 14
A-Z have number: 8
Other have number: 11