LinuxQuestions.org
Review your favorite Linux distribution.
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 06-22-2006, 05:59 PM   #1
jachba
LQ Newbie
 
Registered: Jun 2006
Posts: 3

Rep: Reputation: 0
Calculating age in days and month in a bash script


I could use some help. I'm working in a bash script and need help with calculating an individual's age in years and months. Calculating the years was simple but months are proving to be a challenge. Thanks in advance for anyone who can help.
 
Old 06-23-2006, 08:13 AM   #2
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
What does it look like so far? (Hint: use [code] tags).

Also, what will the input look like? Your choice of input format is very important.

How do you define a month? This is not a trivial Q, ask any banker or anyone else calculating interest & principal.
 
Old 06-23-2006, 12:01 PM   #3
jachba
LQ Newbie
 
Registered: Jun 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Here's what I have in the section I'm working on. I know that the if statement doesn't work properly (it calculates correctly but still expresses the month category as a negative number) but I'm stuck on how to fix it.

agey=$(($yearnow-$year))
agem=$(($monthnow-$month))

if [ $agem < 0 ]
then
agem=$(($monthnow-$month+12))



else
agem=$(($monthnow-$month))
fi
 
Old 06-23-2006, 12:15 PM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Please re-post using the [code] tags -- code doesn't look right displayed as text.

Also answer the rest of the Q's -- I can't do a thing for you if I don't know what your data looks like or what your variables hold.
 
Old 06-23-2006, 12:42 PM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I think your biggest problem came from this part ....
Code:
if [ $agem < 0 ] instead of this 
if [ $agem -lt 0 ]
Code:
#!/bin/bash

read -p "Enter your birth year.  Example: 1900 : " year
read -p "Enter your birht month. Example: 06 " month

yearnow=$(date '+%Y')
monthnow=$(date '+%m')

agey=$(($yearnow-$year))
agem=$(($monthnow-$month))

if [ $agem -lt 0 ] ; then
   agem=$(($monthnow-$month+12))
else
   agem=$(($monthnow-$month))
fi
echo "You are $agey years and $agem months old!"
 
Old 06-23-2006, 01:37 PM   #6
jachba
LQ Newbie
 
Registered: Jun 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks, homey. It works fine now.
 
  


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
How to calculate file age ? "File xy is 2 days 3 hours old" fluppi Linux - Software 5 06-03-2013 10:27 AM
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Can't get Age of Empires II: Age of Kings started (I've could it before !!!) vegetassj2 Linux - Games 44 08-28-2005 04:59 PM
bash month variable johniccp Linux - Newbie 1 12-06-2004 01:41 PM
getting last locale's abbreviated month name through bash scripting markus1982 Programming 2 05-10-2003 08:31 AM

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

All times are GMT -5. The time now is 08:02 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
Open Source Consulting | Domain Registration