[]和[[]]的示例_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] ]]
[[]]
#!/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] ]]
相关文章推荐
- 数组unset的示例_shell脚本
- Shell脚本对文件中的行、单词、字符进行迭代输出示例
- 1011.在线视频―shell脚本系列讲座(一)shell脚本与应用示例
- 用Jbuilder8做一个简单的struts示例
- Http Server的一个示例
- ASP.NET自定义控件开发示例(二)
- C#中trim()函数用法示例
- phpmyadmin config.inc.php配置示例
- ASP.NET Web API使用示例
- 【jQuery】jQuery自定义插件开发 —— 示例
- Linux中find常见用法示例
- KnockOut.js入门示例详解
- String类方法示例
- java中如何处理异常代码示例
- Python 爬虫项目示例
- python通过urllib2爬网页上种子下载示例
- 编程更改公式字段示例代码
- 存储过程示例
- QQ登录PHP Oauth示例
- Android示例程序剖析之Snake贪吃蛇(一:暂停/继续、穿墙和全屏)