Linux shell Implementation Transform input date format _linux Shell

Source: Internet
Author: User
Tags numeric

For the user to enter the validity of the date test is a very important issue, this example is simple to get the user entered the date, and converted to the appropriate format, but not perfect, why please read the text.

#!/bin/sh # normdate-normalizes month field in date specification # to three letters. A Helper # function for Script #7, Valid-date.

Exits W/zero if no error. Monthnotoname () {# Sets the variable ' month ' to the appropriate value case $ in 1) month= "a".;
  2) month= "Feb"; 3) month= "Mar";;
  4) month= "APR";; 5) month= "may";;
  6) month= "June";; 7) month= "June";;
  8) month= "Aug"; 9) month= "Sep";;
  Month= "OCT";; Month= "Nov";;
  Month= "Dec";; *) echo "$0:unknown numeric month value $ >&2;" Exit 1 Esac return 0} # # Begin main script if [$#-ne 3];
 Then echo "Usage: $ month Day" >&2 echo "typical input formats are August 3 1962 and 8 3 2002" >&2 Exit 1 fi if [$3-lt 99]; Then echo "$0:expected four-digit year value." >&2; Exit 1 fi if [-Z $ (echo $1|sed ' s/[[:d igit:]]//g ')]; Then Monthnotoname ' Else # normalize to-three letters, a-upper, rest lowercase month= ' $ (echo $1|cut-c1|tr ' [: Lower:] ' [: Upper:] ') ' Month= ' $month $ (echo $1|cut-c2-3 | tr ' [: Upper:] ' ' [: Lower:] ' "fi echo $month $ $ exit 0

Scripting Analysis:
1 defines the function monthnotoname (), which is used to convert the numeric month entered by the user
2 first determine whether the number of parameters is 3, second to judge the year, followed by the conversion of the month format.
3 If [-Z $ (echo $1|sed ' s/[[:d igit:]]//g ')]; If you were to perform a sed substitution, that would mean that there are numbers in
The function monthnotoname () is executed to convert the numeric month.
4) month= "$ (Echo $1|cut-c1|tr ' [: Lower:] ' [: Upper:] ')"
Month= "$month $ (echo $1|cut-c2-3 | tr ' [: Upper:] ' [: Lower:] ')"
Converts the character month entered into a standard format.
5 The biggest flaw of this script is that although the format of the date is converted, it cannot detect the date that the filter does not exist.

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.