LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-26-2013, 07:33 AM   #1
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Rep: Reputation: Disabled
Seeking help with date calculations in bash (on MacOS-X)


Hy guys y have to make a script which when the user type ./file.sh 05-12-1993 or another date the script must say how old is the person and how many days is resting to his next birthday.Thanks for help or some hints
 
Old 09-26-2013, 08:00 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,166

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
What have you tried so far? We're not here to do your homework for you.
 
Old 09-26-2013, 08:08 AM   #3
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
i didn't say to do my homeork i want a little help.The fact is cuz i cant split the paramrter to assign at 3 variables because if i start my script with year=$3 mounth=$2 and day=$1 he will fill the day with the entire parameter. Thanks
 
Old 09-26-2013, 08:08 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Read the manual page for the date command
Code:
man date
links for introduction to bash scripting

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/

http://mywiki.wooledge.org/BashGuide
 
Old 09-26-2013, 08:13 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by sparky90ful View Post
i didn't say to do my homeork i want a little help.The fact is cuz i cant split the paramrter to assign at 3 variables because if i start my script with year=$3 mounth=$2 and day=$1 he will fill the day with the entire parameter. Thanks
The thing is.. that looks like homework

Because it involves a number of fundamental but 'basic' 'skills' to achieve it.

Show us what you have, we can tell you where you are going wrong, and give a few hints.

Edit: when you post your code, please 'wrap' with [code][/code] tags
e.g.
[code]
#!/bin/bash
First line of code
Second Line
Third
indent
drihT
dnoceS
[/code]

https://www.linuxquestions.org/quest....php?do=bbcode

Last edited by Firerat; 09-26-2013 at 08:20 AM.
 
Old 09-26-2013, 08:17 AM   #6
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
When i do this ./file.sh 05-12-1993 or this ./file.sh 05-12-2000 or this ./file.sh 02-06-1993 in terminal that file.sh must say cuz it has .... years and cuz it rest ...... days until to his next birthday.I think fhirst i must to assing to 3 variables the date which he will enter but i don't know which date will be is only the format that i know.

ps: yes it is a homework but i want a hint not a solution

ps2: i have no code because i must start with the reading of the parameter i think

Last edited by sparky90ful; 09-26-2013 at 08:22 AM.
 
Old 09-26-2013, 08:22 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by sparky90ful View Post
ps2: i have no code because i must start with the reading of the parameter
Well, when you have something, come back and post it

Read the links I gave
AND the date manpage
 
Old 09-26-2013, 08:27 AM   #8
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Well, when you have something, come back and post it

Read the links I gave
AND the date manpage
how to have something if can't start it??
 
Old 09-26-2013, 08:30 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
This is homework right?

do you have any class notes?

I can tell you how I would do it, but it will likely be different to how you have been taught.
 
Old 09-26-2013, 08:37 AM   #10
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
This is homework right?

do you have any class notes?

I can tell you how I would do it, but it will likely be different to how you have been taught.

The thing is cuz they give us exercises without any information and they tell us to do it using the man-s and the internet but i can't find how to split the parameter in 3 variables because he has - if he was having spaces between numbers was easy i coud do like year=$3 mounth=$2 and day=$1 but this doesn't work.We started the school with shell but with a tutorial for svn for putting the result of exercises and cuz they correct them.The rest of information we must take it from man-s or internet
 
Old 09-26-2013, 08:49 AM   #11
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,163
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
show us the contents of ./file.sh
 
Old 09-26-2013, 08:53 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Well, the point is that you have a single positional parameters (argument) and you want to split it into three parts and assign each of them to three different variables (year, month and day), right? Ok, assume the format of the input date is fixed and it is dd-mm-yyyy and try to retrieve three different substrings from $1.

Substring replacement is one of the basics of bash variables manipulation. You can read about it in the bash reference manual here (look for the syntax ${parameter:offset:length}).
 
Old 09-26-2013, 08:54 AM   #13
sparky90ful
Member
 
Registered: Sep 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
show us the contents of ./file.sh
is what i must create file.sh is the script writed by me and when user is run it with some parameter i must have the result for nomather which will be the parameter for now i have this
Code:
string=$1
IFS=-
set $string
year=$3
mounth=$2
day=$1
y must compare with the actual date to find the age
 
Old 09-26-2013, 09:00 AM   #14
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,323
Blog Entries: 1

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
You can use "date" and "bc" to do some math. "awk" can help you isolate fields. "sed" can help you remove formatting characters. You don't need to use export to set variables in a bash script. It's not that complex, but bash by itself, is probably incapable of doing it, without some massive data definition of an internal table. For days until you probably want something like the julian date. YYJJJ where JJJ is the day of the year.

For accuracy sake, every 4 years is a leap year, except every one hundred years. With a further exception with every thousandth year that IS a leap year. Gotta love Y2K learning.
 
Old 09-26-2013, 09:02 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
That's a good start. Now you have to compare dates and compute the differences. Use the date command as suggested above and take in mind that time additions and subtraction in bash have to be made using the "seconds since epoch" units.
 
  


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] Date and Time Calculations in Perl (or other) taylor_ma Programming 9 05-03-2012 09:27 PM
[SOLVED] Date Calculations using script!! anishkumarv Linux - Newbie 9 09-24-2011 04:36 PM
Using Perl to perform calculations in Bash w1k0 Programming 5 06-07-2009 09:10 PM
Date calculations on Sun sparc ukursat Linux - Newbie 1 12-05-2008 11:01 PM
Date calculations in BASH script Crashman Programming 4 07-03-2004 11:15 AM


All times are GMT -5. The time now is 10:17 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