Date Compare tool


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Compare tool
# 1  
Old 02-13-2018
Hammer & Screwdriver Date Compare tool

Hello Guys,
Just joined today with a simple task i guess.
I need to write a shell script which takes input from users which is a date and should be able to compare the date differences between the dates keeping in mind the leap year and all.

Kindly Guide Smilie Smilie
# 2  
Old 02-13-2018
Kindly think about what you are asking. Then give us a few more details:
  1. What operating system are you using?
  2. What shell do you want to use to run your shell script?
  3. In what format will dates be entered?
  4. Where will the dates be found? (Are they provided as variables in your shell script? Are they command-line parameters? Are they typed in in response to prompts? Are they found in known files? ...)
  5. In what way do you want dates to be compared? (Are you just trying to determine if they specify the same date? Are you trying to determine the number of days between two dates? If your dates includes hours, minutes, and seconds; are you trying to determine the number of seconds between the two dates?)
Please help us help you.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-13-2018
What operating system are you using?
---This is a Linux.
What shell do you want to use to run your shell script?
---Bash Shell
In what format will dates be entered?
---Date will be entered as YYYYMMDD whcih i have split into 3 variables.
Where will the dates be found? (Are they provided as variables in your shell script? Are they command-line parameters? Are they typed in in response to prompts? Are they found in known files? ...)
---Dates typed in response to prompts.
In what way do you want dates to be compared? (Are you just trying to determine if they specify the same date? Are you trying to determine the number of days between two dates? If your dates includes hours, minutes, and seconds; are you trying to determine the number of seconds between the two dates?).

---i Want to know the difference in Days and not using Datediff or Date. I just need to use simple if ...else and arithmetics. Also look for number of leap years
# 4  
Old 02-13-2018
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. And, explain why datediff and date cannot be used in your script!

If you did post homework in the main forums, please review the guidelines for posting homework and repost.
# 5  
Old 02-13-2018
I am doing this as a practice to learn looping Artithmetic and all
Could you debug this for me please . I am just calculating the year difference with leap year in this phase. I know i have messed up the Loops
Code:
#!/nairvigv/bin/bash
echo "ENTER FIRST DATE IN YYYYMMDD format"
read startdate



startyear=${startdate:0:4}
startmonth=${startdate:4:2}
startday=${startdate:6:2}




echo The date you Entered is $startyear"-"$startmonth"-"$startday


echo "ENTER SECOND DATE IN YYYYMMDD format"
read enddate
endyear=${enddate:0:4}
endmonth=${enddate:4:2}
endday=${enddate:6:2}



echo The date you Entered is $endyear"-"$endmonth"-"$endday



days_months=(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

echo ${days_months[*]}
##################### YEARS######################################################
if [ $startyear -eq $endyear ]

then
        yeardifference=0;
        echo $yeardifference

 else
        if [ $startyear % 4 == 0 && $startyear % 100 != 0 || $startyear % 400 == 0 ] then
                if[ $endyear % 4 == 0 && $endyear % 100 != 0 || $endyear % 400 == 0 ] then
                        if[ $startyear -gt $endyear ] then
                        yeardifference=`expr $startyear - $endyear * 365 + 2`
                        else
                        yeardifference=`expr $endyear - $startyear * 365 + 2`

                        fi
                        if[ $endmonth -gt $startmonth ]then
                                if( "${days_months[$startmonth - 1]}" -gt  "${days_months[1]}" )then
                                        yeardifference=yeardifference-1;
                                fi
                        fi
                fi
 else
                   if[ $startyear -gt $endyear ] then
                        yeardifference=`expr $startyear - $endyear * 365 + 1`
                        else
                        yeardifference=`expr $endyear - $startyear * 365 + 1`
                        
                        fi
                        if[ $startmonth -gt $endmonth ]then
                                if( "${days_months[$endmonth - 1]}" -gt  "${days_months[1]}" )then
                                        yeardifference=yeardifference-1;
                                fi
                        fi
                fi


else

                if[ $startyear -gt $endyear ]then
                         yeardifference=`expr $startyear - $endyear * 365`

                        else
                         yeardifference=`expr $endyear - $start * 365`
                fi



fi


Last edited by barryallen; 02-13-2018 at 07:33 AM..
# 6  
Old 02-13-2018
Sure this DOES NOT belong to a class nor course?
What does "and all" mean - you use it twice ("leap year and all", "looping Artithmetic and all"), but it doesn't help understand your requirements.

You say "I know i have messed up the Loops", but I'm afraid it's more than the loops. WHAT is your expected result? It can't be derived from your code, as that is faulty, misleading, and overcomplicated, having syntax and logics errors. You assign a variable "yeardifference" in several lines which makes me believe that it should finally hold something like a result. But - once you have the expr syntax errors sorted out - you assign large negative values to it which makes me think there's a semantic error as well.

The are plenty of syntax errors, of which I at first glance found:
- if[ $startyear -gt $endyear ] then has two sytax errors in it - it needs a space between if and [ , and a ; between ] and then, plus it occurs twice in different branches, the second in a dangling (belonging nowhere) else if I count correctly.
- if with parentheses would work if a comand were given inside but would need a space as well.
- yeardifference=yeardifference-1 will assign the unexpanded, unevaluated string yeardifference-1

I stopped here. Please get the syntaxy right. And, again - what exactly is the desired result?
# 7  
Old 02-13-2018
In addition to what RudiC has already said, you say that you have messed up the loops; but there aren't any loops in your code! There are only nested if statements that seem to try to calculate the number of days in years without accounting for the number of days in months or the number of days between days within a month. You are correct in thinking that you need a couple of nested loops (one looping through months and one looping through years, and depending on how you structure your code, you might also want a loop to loop through days in a month), but your code does not contain any loops at all. Loops start with keywords like for, until, and while; not with the keyword if.

One might note that the prompts given to your users asks for 10 character inputs in the format YYYY-MM-DD, but when you extract the year, month, and day fields from the entered strings, you only look at the first 8 of those 10 characters.

RudiC mentioned that you have a dangling else. I'm not sure it that is true or not. Your lack of consistent indentation makes it impossible to line up ifs with their corresponding elses and fis. But it is clear that you have more fis than you have ifs; and that has to be an error.

Instead of partially checking whether the start date comes before or after the end date so many times, you might want to check that just after you get the two dates from your user and switch their values if the end comes before the start.

Note also that if you have a date like December 31 in one year and January 1 in the next year, there is one day between them whether or not either of those years is a leap year. And, if you go from February 28 to March 1 that is going to be one day or two days depending on what year is being processed. Your code only calculates leap days if the years are different. And, if you go from March 1 in one leap year to February 28 in the next leap year; even though both years are leap years, there are no leap days in the four or eight years between those two dates.

And, finally, you might also note that you use a variable named start, but you never assign any value to it. I haven't verified that there aren't any other variables that are used without being set.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. UNIX for Beginners Questions & Answers

Compare 2 files with different keywords : use server health-check tool

I have two files to be compared to get the output of the differences. File1 has a lot more lists than File2. After searching a lot on this thread I'am unable to find the exact code that im willing to get. This will be used as 'pre-check'/post-check utility (health check Tool) to compare... (1 Reply)
Discussion started by: GeekyJimmy
1 Replies

3. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

4. Shell Programming and Scripting

Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this, Not After : Jul 28 14:09:57 2017 GMT So I only crop out the date, with this command echo $dateFile | cut -d ':' -f 2,4The result would be Jul 28 14:57 2017 GMT How do I... (3 Replies)
Discussion started by: Loc
3 Replies

5. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

6. Shell Programming and Scripting

Tool to compare two Linux machines

Hi ALL, I was looking out for any freeware tool which can compare config properties of 2 linux machines for ex java properties. Tried in Google but no luck. Any help would be greatly appreciated :) (3 Replies)
Discussion started by: nikhil jain
3 Replies

7. UNIX for Dummies Questions & Answers

using date tool in bash

date --date='10:30am + 1 hour' +%H:%M 11:30 produces date --date='10:30pm + 1 hour' +%H:%M produces 23:30 I want to do the following: TIME="1:30pm" date --date='$TIME + 1 hour' + %H:%M to produce 14:30 (1 Reply)
Discussion started by: efittery
1 Replies

8. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

9. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question