Find the answer to your Linux question:
Results 1 to 6 of 6
Below is a program I'M trying to write that will take an 8 digit date and validate whether or not it's a valid date. I'M very new to shell scripting ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122

    Small DateValidation bash script


    Below is a program I'M trying to write that will take an 8 digit date and validate whether or not it's a valid date. I'M very new to shell scripting so I really a have no idea what I'M going. Could you take a look and let me know what all I'M doing wrong here? Thanks.

    Code:
    #! /bin/bash -x
    
    # This is the calender array for the DateValidation program
    CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
    
    read -p "Enter a date for validation: " DATE
    
    # establish the varible LEN to hold the number of characters in Date, 8 is the only valid number
    LEN=$(echo ${#DATE})
    
    if [ $LEN -eq 8 ]; then #{
     
    	# set date dariables MM, DD, & YY
    	export MM=${DATE:0:2}
    	export DD=${DATE:2:2}
    	export YY=${DATE:4:4}
    
    	if [ ${YY} -gt 0 ]; then #{
    		if [ ${MM} -gt 0 & < 13 ]; then #{
    			if [ ${MM} -eq 2 ]; then #{
    				if [ is_leap_year ${YY} == 1 ]; then #{
    					if [ ${DD} -gt 0 & -le 29 ]; then #{
    			elif [ ${DATE} -gt 0 & -le ${CALENDER[${MM}]}] #}}}}}}
    
    	echo "${DATE} is a valid date"
    else
    	"${DATE} is an invalid date"
    fi #}
    
    #############################
    ### FUNCTION-is_leap_year ###
    #############################
    
    is_leap_year() {
    
    if [ 0 -eg ${YY} % 4 & 0 -ne {YY} % 100 or 0 -eq ${YY} % 400 ]; then #{
    
    	return true #}
    else #{
    	return false #}
    
    exit
    #END

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    function should be declared before it's used.

    Code:
    LEN=$(echo ${#DATE}) # echo is useless, so is command substitution
    export MM=${DATE:0:2} # exports are useless
    if [ ${MM} -gt 0 & < 13 ]; then # a single & puts the process in background, you need 2 to be an AND, but it doesn't work inside test [ ]
    # < is redirection from file, 13 is not a file
    #  if [ $MM -gt 0 ] && [ $MM -lt 13 ]
    if [ is_leap_year ${YY} == 1 ]; then # you need command substitution : if [ "$(is_leap_year "$YY")" -eq 1 ]; then

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,288
    Hi.

    Perhaps this skeleton will help:
    Code:
     11 if [ ... ; then
     18   if [ ... ; then
     19     if [ ... ; then
     20       if [ ... ; then
     21         if [ ... ; then
     22           if [ ... ; then
     23           elif [
     24           elif [
     Expected then, but got at line 24: 
     26         else
     36           if [ ... ; then
     39           else
     Unbalanced: (if - fi) count: 7
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    122
    Thanks guys. Still don't have my script working but what you gave me was some much needed info. Thanks again.

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    648
    remember that arrays are referenced from 0 and not 1.

  6. #6
    Just Joined!
    Join Date
    Feb 2005
    Posts
    5

    Human engineer your script.....

    1. I presume you have a reason for using the shell, rather than some more powerful scripting language such as perl or python?
    2. Once you get your script working, may I suggest a bit of human engineering?
    a) validate the input. i.e. months 1-12, etc.
    b) allow user to enter dates with / or - as in mm-dd-yyyy
    c) allow user to enter dates with yy
    d) It always annoys me (as a non USA resident) when programs assume the date is mm-dd-yyyy. This seems to me to be a very illogical format. It is neither ascending order (dd-mm-yyyy) nor descending order. It is not that difficult to allow yyyy/mm/dd or yyyy-mm-dd which AFAIK is the international ISO standard way of doing dates, (as well as mm-dd-yyyy which is what your USA users will probably assume.).

    Just my two cents worth.......
    pgmer6809

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •