您的位置:首页 > 运维架构 > Shell

[]和[[]]的示例_shell脚本

2016-08-06 10:41 127 查看
工作环境: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] ]]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell