#!/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
转载于:https://blog.51cto.com/7431686/1699701