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.