标签:apr ase amp format shell 需要 linu shel cut
Example No. 2#!/bin/bash
#shell name:shell_format.sh
#author by woon
#数据标准化输出示例
Month_to_name()
{
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: 错误的月份格式:$1" >&2; exit 1
esac
return 0
}
#脚本主体
if [ $# -ne 3 ]; then
echo "Usage: $0 month day year\nThe correct formats are 8 21 2018 or August 21 2018"
exit 1
fi
#判断年份是否是数字,并拼接年份
#判断是否是个位数
if [ -z $(echo $3 | sed ‘s/^[0-9]//g‘) ];then
year=200$3
#判断是否是10-69之间数字
elif [ -z $(echo $3 | sed ‘s/^[1-6][0-9]//g‘) ]; then
year=20$3
elif [ -z $(echo $3 | sed s‘/^[7-9][0-9]//g‘) ]; then
year=19$3
elif [ -z $(echo $3 | sed ‘s/^[0-9]\{4\}//g‘) ]; then
year=$3
else
echo "The yeat‘s formats are wrong:$3"
exit 2
fi
#脚本主体
if [ -z $(echo $1 | sed ‘s/[[:digit:]]//g‘) ]; then
Month_to_name $1
else
month=$(${$1%${$1#?}}|tr ‘[a-z]‘ ‘[A-Z]‘)$(echo $1|cut -c2-3 | tr ‘[A-Z‘ ‘[a-z]‘)
fi
echo $month $2 $year
exit 0
运行结果:
标签:apr ase amp format shell 需要 linu shel cut
原文地址:http://blog.51cto.com/woonli/2147445