Linux shell脚本示例(四)

Example No. 2
作为shell编写人员,经常面对数据格式不一致的问题,将数据标准话后输出是一个需要解决问题,本示例以MySQL的时间为例,脚本输入月、日、年三个参数,将其标准化后输出,月份以英文标准输出,年份如果是4个数字,直接输出,如果是0~69,则年份为2000~2069,如果是70~99,则年份为1970~1999;
脚本示例:

#!/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

运行结果:

转载于:https://blog.51cto.com/woonli/2147445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值