3.格式化输出 年月日
2015-09-30 23:58
393 查看
#!/bin/bash # normdate -- Normalizes month field in date specification # to three letters, first letter capitalized. A helper # function for Script #7, valid-date. Exits w/ zero if no error. #格式化 输出日期 #BUG : 2月有30,31号不报错 , 输入字母时不报错 monthnoToName() { # Sets the variable 'month' to the appropriate value case $1 in 1 ) month="Jan" ;; 2 ) month="Feb" ;; 3 ) month="Mar" ;; 4 ) month="Apr" ;; 5 ) month="May" ;; 6 ) month="Jun" ;; 7 ) month="Jul" ;; 8 ) month="Aug" ;; 9 ) month="Sep" ;; 10) month="Oct" ;; 11) month="Nov" ;; 12) month="Dec" ;; * ) echo "$0: Unknown numeric month value $1" >&2; exit 1 esac return 0 } ## Begin main script if [ $# -ne 3 ] ; then #给脚本三个参数,月日,年 echo "Usage: $0 month day year" >&2 echo "Typical input formats are August 3 1962 and 8 3 2002" >&2 exit 1 fi if [ $2 -gt 31 ]; then #判断日不可大于31 echo "days input error --->1-31" exit 1 fi if [ $3 -lt 99 ] ; then #判断年份大于99 echo "$0: expected four-digit year value." >&2; exit 1 fi if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; then #判断输入是否为数字,如果数字返回true monthnoToName $1 #为空未知的数字月值 else # Normalize to first three letters, first upper, rest lowercase#如果是字母 取三个字母 第一个字母转为大写,第二三个字母转为小写 month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')" month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')" fi echo $month $2 $3 exit 0##############
测试:
[root@www ~]# ./check_month.sh aassssa 1 2222 #BUG
Aas 1 2222
[root@www ~]# ./check_month.sh AUG 1 2222
Aug 1 2222
[root@www ~]# ./check_month.sh 12 1 2222
Dec 1 2222
[root@www ~]# ./check_month.sh 2 31 2222 #bug
Feb 31 2222
相关文章推荐
- Android Native 绘图方法
- 访问Nginx发生SSL connection error的一种情况
- 星外虚拟主机访问被控出现Unspecified error解决方法
- IIS 错误 Server Application Error 详细解决方法
- On Error Resume Next 语句
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法
- Lua编程示例(一):select、debug、可变参数、table操作、error
- 收集整理的http/1.1 500 server error错误的解决方法
- SQL 2005 ERROR:3145 解决办法(备份集中的数据库备份与现有的数据库不同)
- VBScript中On Error语句用法小结
- javascript 面向对象编程 function是方法(函数)
- javascript 面向对象编程 function也是类
- IIS下PHP连接数据库提示mysql undefined function mysql_connect()
- 浅谈Javascript中Object与Function对象
- JavaScript通过function定义对象并给对象添加toString()方法实例分析
- IE 当eval遇上function的处理
- JavaScript中Object和Function的关系小结
- Javascript 使用function定义构造函数
- javascript Object与Function使用
- JavaScript中的Function函数