LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-03-2013, 08:22 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
Script for validating a date e.g. 11291985


This script whould be able to determine if a date that has been entered in as 8 digits is valid or not.

It will first check that there are 8 digits, if so everything is good.

It will then theck that the month value is > 0 & < 13, if so everything is good.

In order to get the day I need to fist find out if it's leap year or not to determin if the maximum number of days is 28 or 29

I use a function "is_leap_year" to determine if it is or is not leap year.

I use a function "get_day" to check that the day is > 0 & <= CALENDER[MM]

For some reason my program is running the "get_day" function even when the function "is_leap_year" returns true, in that case it would only call "get_day" in the else clause of that code block. Please help I am stumped.
Code:
#! /bin/bash -x


#############################
### FUNCTION-is_leap_year ###
#############################

is_leap_year()
{
        PART1=`expr ${YY} % 4`
        PART2=`expr ${YY} % 100`
        if [[ 0 -eq ${PART1} ]] && [[ 0 -ne ${PART2} ]] || [[ 0 -eq `expr ${YY} % 400` ]]; then
                result=true
	else
		result=false
        fi
}



####################
### END FUNCTION ###
####################

########################
### FUNCTION-get_day ###
########################

get_day()
{
	if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${MM}]} ]]; then #{
		DAY="GOOD"
	else
		DAY="BAD"
	fi #}
}

####################
### END FUNCTION ###
####################

# 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
	MM=${DATE:0:2}
	DD=${DATE:2:2}
	YY=${DATE:4:4}

	if [ ${YY} -gt 0 ]; then #{
		if [ ${MM} -gt 0 ] && [ ${MM} -lt 13 ]; then
			if [ "${MM} -ne 02" ]; then
				get_day ${DD}
				if [ ${DAY} == GOOD ]; then
					echo "${DATE} is a valid date!"
				else
					echo "${DD} is invalid!"
				fi #}

			else
				is_leap_year ${YY} 
				if [ "${result} == true" ]; then
					if [ ${DD} -gt 0 ] && [ ${DD} -le 29 ]; then
						echo "${DATE} is a valid date"
					else
						echo "The day you entered for this date is invalid"
					fi #}
				else
					get_day ${DD}
					if [ ${DAY} == GOOD ]; then
						echo "${Date} is a valid date"
					else
						echo "Date ${DD} is invalid"
					fi #}
				fi #}
			fi #}

		else
			echo "${MM} is invalid!"
		fi #}


	else
		echo "${YY} is invalid!"
	fi #}

else
	echo "Incalid number of digits for a valid date"
fi #}
 
Old 01-03-2013, 09:17 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Garrett85 View Post
This script whould be able to determine if a date that has been entered in as 8 digits is valid or not.
As long as you figure out how to determine / restrict input as MMDDYYYY or whatever else format 'date' can figure out if a date is OK on its own:
Code:
#!/bin/bash
set -vxe; LANG=C; LC_ALL=C; export LANG LC_ALL; 
INPUT="$1"; M=${INPUT:0:2}; D=${INPUT:2:2}; Y=${INPUT:4:4}; 
date --date="${Y}-${M}-${D}" >/dev/null 2>&1; 
case $? in 0) echo "ERANU!";; *) echo "Uvavu"; esac; exit 0
 
Old 01-04-2013, 11:55 AM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
date

This is a learning project I have been given. I need to write the script.


Quote:
Originally Posted by unSpawn View Post
As long as you figure out how to determine / restrict input as MMDDYYYY or whatever else format 'date' can figure out if a date is OK on its own:
Code:
#!/bin/bash
set -vxe; LANG=C; LC_ALL=C; export LANG LC_ALL; 
INPUT="$1"; M=${INPUT:0:2}; D=${INPUT:2:2}; Y=${INPUT:4:4}; 
date --date="${Y}-${M}-${D}" >/dev/null 2>&1; 
case $? in 0) echo "ERANU!";; *) echo "Uvavu"; esac; exit 0
 
Old 01-04-2013, 12:32 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
A couple of things

What's with all of the C-style { } blocks around your if statements that are commented out? It makes the code difficult to read and doesn't add anything

You put the entire leap year test and february test in quotes, eg: [ "${result} == true" ], which is probably why it's not evaluating correctly

You should use $() rather than ``, such as: part1=$(expr $yy % 4)

Why did you put part1 and part2 of the leap year test in their own variables, then rolled part3 into the if statement? It makes it awkward to read.

All caps variable names are generally reserved for environment variables. Variables inside your code should probably use lower case or mixed upper/lower case to prevent confusion

There's no need to use the echo in your len assignment, just do: len=${#date}

Variables substitutions rarely need to be surrounded by {}. There's no harm in it, but I only see a couple of instances in your script where it was actually necessary, just FYI

A simpler way to do your leap year test would be to have the is_leap_year function return a 1 or a 0, rather than the strings "true" or "false", and simply add the result to calendar[2] to make it 29 in the event of a leap year. Then you only need one test in your main code, it would cut the number of lines significantly.

Last edited by suicidaleggroll; 01-04-2013 at 12:37 PM.
 
Old 01-04-2013, 12:59 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Then run the script as
Code:
/bin/bash -vx /path/to/script [args] 2>&1 > /path/to/logfile.0
twice with a different date one of which you know represents a leap year, then compare output with
Code:
diff --side-by-side /path/to/logfile.0 /path/to/logfile.1 | less
and spot the differences. BTW you shouldn't do
Code:
if [ "${result} == true" ]; then
but use one style of variable naming and quoting
Code:
if [ "${RESULT}" = "TRUE" ]; then
and use that consistently.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Each time a script is called. A log file is created with time and date + Bash Script. y0_gesh Programming 6 08-17-2012 03:16 AM
[SOLVED] validating mandatory column value in csv file using script sukhdip Linux - Newbie 2 01-31-2012 02:45 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
Shell script, validating email and website Joey^s Programming 5 12-04-2004 05:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration