Linux Shell Script Example (iv)

Source: Internet
Author: User

Example No. 2
As a shell writer, often face the problem of inconsistent data format, the data standard session output is a need to solve the problem, this example takes MySQL time as an example, the script enters the month, the day, the year three parameters, standardized after the output, the month in English standard output, the year if the 4 digits, the direct output, If 0~69, the year is 2000~2069, and if it is 70~99, the year is 1970~1999;
Script Example:

#!/bin/bash#shell Name:shell_format.sh#author by woon# data Normalization output Example Month_to_name () {case $1) month= "Jan";;    2) month= "Feb";;  3) month= "Mar";;    4) month= "APR";;  5) month= "may";;    6) month= "June";;  7) month= "Jul";;    8) "Month=";  9) month= "Sep";;    Month= "OCT";;  One) month= "Nov";;    Month= "Dec";; *) echo "$: Wrong month Format: $" >&2; Exit 1 ESAC return 0} #脚本主体if [$#-ne 3]; Then echo "Usage: $ month Day year\nthe correct formats is 8 2018 or August 2018" Exit 1fi# Judging whether the year is a number and stitching the year # to determine if it is Single digit If [-Z $ (echo $ | sed ' s/^[0-9]//g ')];then year=200$3 #判断是否是10-69 digital Elif [-Z $ (echo $ | sed ' s/^[1-6][0-9]/ /g ')]; Then Year=20$3elif [-Z $ (echo $ | sed s '/^[7-9][0-9]//g ')]; Then Year=19$3elif [-Z $ (echo $ | sed ' s/^[0-9]\{4\}//g ')]; Then Year=$3else echo "The Yeat ' s formats is wrong:$3" Exit 2fi# script Body if [-Z $ (echo $ | sed ' s/[[:d igit:]]//g ' ) ]; Then Month_to_nAme $1else month=$ (${$1%${$1#?}}| TR ' [A-z] "[A-z] ') $ (echo $1|cut-c2-3 | tr ' [A-z] [a-z] ') Fiecho $month $ $yearexit 0

Operation Result:

Linux Shell Script Example (iv)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.