[]和[[]]的示例_shell脚本
工作环境: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] ]]
转载于:https://blog.51cto.com/8776055/1834963
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
