LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2014, 03:18 AM   #1
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Rep: Reputation: Disabled
help required with days calculation!!


Write a shell script to calculate the number of days after subtracting the two dates? using command line arguments.
This is what I have so far. I have just started working on shell scripting and have very less knowledge so far.Any help would be really appreciating.

#script to calculate the age
#take input from user in command line arguments as 20 04 1992 - 14 02 2014
#!/bin/bash

#checking if the date is correctly located in the following arguments

echo "$1 $2 $3 $4 $5 $6 $7"

#converting years into months after subtracting years.
months=$($1-$7)*($12))
#converting months to number of days in total.
days=$months+$(($2-$6)*$(31))

echo "Number of days are: $days"

Last edited by mehreen124; 02-28-2014 at 04:32 AM. Reason: forgot to include the code which i did so far
 
Old 02-28-2014, 03:43 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,414

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
-Homework Assignment Alert-

Ok, so what have you written so far?
 
Old 02-28-2014, 03:52 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,458

Rep: Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778
I wrote. I used the command date, see man page or google ....
 
Old 02-28-2014, 04:33 AM   #4
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
I have edited my question for better understanding.. Any help would be appreciated.
 
Old 02-28-2014, 04:52 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,414

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by mehreen124 View Post
Code:
#script to calculate the age
#take input from user in command line arguments as 20 04 1992 - 14 02 2014
#!/bin/bash

#checking if the date is correctly located in the following arguments

echo "$1 $2 $3 $4 $5 $6 $7"

#converting years into months after subtracting years.
months=$($1-$7)*($12))
#converting months to number of days in total.
days=$months+$(($2-$6)*$(31))

echo "Number of days are: $days"
So, in your months = line you're subtracting the first parameter ($1) from the seventh parameter ($7) and then multiplying by the twelfth parameter ($12) which doesn't exist. So you're subtracting the birth day from the current year? That's not going to work is it?

You need to start again and get a better understanding of how parameters work before you even think about getting started on arithmetic.

The Advanced Bash Scripting Guide available from here: http://tldp.org/guides.html would be a good place to start reading.
 
1 members found this post helpful.
Old 02-28-2014, 04:58 AM   #6
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
I wanted to subtract $1 and $7 and then multiply with 12. for conversion of years into months.
#converting years into months after subtracting years.
months=$(($1-$7)*(12))
#converting months into days
days=$months+$(($2-$6)*(31))
is this correct?
I simply want to get the difference of the two dates and convert into days.
 
Old 02-28-2014, 05:10 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,414

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by mehreen124 View Post
I wanted to subtract $1 and $7 and then multiply with 12. for conversion of years into months.
#converting years into months after subtracting years.
months=$(($1-$7)*(12))
#converting months into days
days=$months+$(($2-$6)*(31))
is this correct?
I simply want to get the difference of the two dates and convert into days.
I know what you are trying to get, and I'm also not going to do your homework for you.

What you should do is put this line in your script so it starts:
Code:
#!/bin/bash
set -x
The set -x will show you each line of your script as it's being run and with the parameters expanded.

So, if you're running your script with 20 04 1992 - 14 02 2014 That makes your parameters:
Code:
$1 = 20
$2 = 04
$3 = 1992
$4 = -
$5 = 14
$6 = 02
$7 = 2014
Your months= line is going to use 20 as $1, and 2014 as $7 then multiplying it by 12.

And then your days line is just going to totally compound your error.

Your calculations are fundamentally wrong, before you even get in to the scripting part of solving this problem.
 
1 members found this post helpful.
Old 02-28-2014, 05:19 AM   #8
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
got it thanx
 
Old 02-28-2014, 05:31 AM   #9
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,414

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
One of the easiest ways to solve this problem is to convert each of the dates into an integer value of seconds (based on the Unix epoch) subtract these and that'll give you the number of seconds difference between the two dates, then divide this by 86400 (60 (seconds) * 60 (minutes) * 24 (hours)) to give you the number of days.

As pan64 says in post #3 the date command is your friend
 
1 members found this post helpful.
Old 02-28-2014, 05:54 AM   #10
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
but I cannot use date command because I need the output stepwise. like this
21 years 11 months 8 days
 263 months 8 days
 8015 days
that's y I was using all the arguments separately.
 
Old 02-28-2014, 06:00 AM   #11
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,414

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by mehreen124 View Post
but I cannot use date command because I need the output stepwise. like this
21 years 11 months 8 days
 263 months 8 days
 8015 days
that's y I was using all the arguments separately.
Which you've not mentioned until now, all you've asked for before is the number of days.

Anything else you've not told us yet?
 
Old 02-28-2014, 06:12 AM   #12
mehreen124
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
I managed to calculate the number of days.Thankyou, i learned something atleast. And no nothing is left.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to find a file which is older than 5 days but youger than 6 days? thomas2004ch Linux - Software 1 10-29-2013 03:30 AM
How do I grep my /var/log/secure file for the past 7 days or so many days? johnmccarthy Linux - Newbie 5 01-04-2013 09:43 PM
Offline method required to find out number of days since a certain date! linustalman Linux - General 5 12-14-2010 03:52 AM
swap calculation mario.almeida Linux - General 5 08-03-2009 11:51 AM
Php days calculation dylan912 Programming 1 10-19-2006 05:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:14 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration