工作环境:Red Hat Enterprise Linux Server release 6.5 (Santiago) 、bash
[[]]
#!/bin/bash
#
until [[ $choice == "quit" ]]
do
read -p "input a character:" ch
if [ ${#ch} -eq 1 ];then
if [[ $ch == [0-9] ]];then
echo "num"
elif [[ $ch == [a-zA-Z] ]];then
echo "character"
else
echo "special character"
fi
elif [ ${#ch} -eq 0 ];then
echo "it's none"
else
echo "the system will select the first character"
ch=`echo $ch | cut -c 1`
if [[ $ch == [0-9] ]];then
echo "num"
elif [[ $ch == [a-zA-Z] ]];then
echo "character"
else
echo "special character"
fi
fi
read -p "if you want to out,just input quit:" choice
done
exit
[root@localhost test]# bash 49.sh
input a character:a
character
if you want to out,just input quit:n
input a character:4
num
if you want to out,just input quit:quit
[]
#!/bin/bash
#
until [[ $choice == "quit" ]]
do
read -p "input a character:" ch
if [ ${#ch} -eq 1 ];then
if [ $ch == [0-9] ];then
echo "num"
elif [ $ch == [a-zA-Z] ];then
echo "character"
else
echo "special character"
fi
elif [ ${#ch} -eq 0 ];then
echo "it's none"
else
echo "the system will select the first character"
ch=`echo $ch | cut -c 1`
if [ $ch == [0-9] ];then
echo "num"
elif [ $ch == [a-zA-Z] ];then
echo "character"
else
echo "special character"
fi
fi
read -p "if you want to out,just input quit:" choice
done
exit
[root@localhost test]# bash 49.sh
input a character:a
special character
if you want to out,just input quit:a
input a character:4
special character
if you want to out,just input quit:quit
[root@localhost test]# vim 49.sh
优化:对于有多个字符的情况,可以使用如下判断
[[ `echo "$ch"|cut -c1` == [0-9] ]]