The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Happy Birthday to GNU iBot MySQL DevZone RSS 1 06-11-2009 06:17 AM
calculation aoussenko Shell Programming and Scripting 2 06-17-2008 09:09 AM
Birthday Mail Script Kumarsharad Shell Programming and Scripting 1 06-13-2008 10:41 AM
Celebrate GnuPG's 10th birthday! iBot UNIX and Linux RSS News 0 01-12-2008 04:10 PM
Happy birthday Linux mib News, Links, Events and Announcements 1 06-30-2005 06:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-29-2008
mustaine85 mustaine85 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 5
Birthday Calculation

Hi I have a simple question.
Is there an easy way to read a date of birth from a file and calculate how old that person is based on today's date? And would I need the make sure the birthdates are enterered in a particular format?
Thanks
  #2 (permalink)  
Old 12-29-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,954
Quote:
Originally Posted by mustaine85 View Post
Hi I have a simple question.
Is there an easy way to read a date of birth from a file and calculate how old that person is based on today's date? And would I need the make sure the birthdates are enterered in a particular format?
Thanks
You might have to take a look at datecalc script from the forum.
  #3 (permalink)  
Old 12-29-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
A sample to calculate the age with a given date, adapt it to read the date from a file or if you have a different date format.

Code:
#!/bin/sh

echo -n "Enter the birthdate (mm-dd-yyyy): "
read bdate

bmonth=${bdate:0:2}
bday=${bdate:3:2}
byear=${bdate:6:4}

cdate=`date +%m-%d-%Y`

cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}

if [[ "$cmonth" -lt "$bmonth" ]] || [[ "$cmonth" -eq "$bmonth" && "$cday" -lt "$bday" ]]
then
  let age=cyear-byear-1
else
  let age=cyear-byear
fi

echo "Age = "$age
Regards
  #4 (permalink)  
Old 12-29-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Franklin52 View Post
A sample to calculate the age with a given date, adapt it to read the date from a file or if you have a different date format.

Code:
#!/bin/sh

echo -n "Enter the birthdate (mm-dd-yyyy): "

The -n option to echo is not standard.
Quote:
Code:
read bdate

bmonth=${bdate:0:2}

That is not sh syntax; it is limited to bash and ksh.
Quote:
Code:
bday=${bdate:3:2}
byear=${bdate:6:4}

cdate=`date +%m-%d-%Y`

cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}

Simpler (and POSIX compliant) is:

Code:
eval "$( date "+cyear=%Y cmonth=%m cday=%d" )"
Quote:
Code:

if [[ "$cmonth" -lt "$bmonth" ]] || [[ "$cmonth" -eq "$bmonth" &&

[[ ... ]] is not sh syntax. Use [ .. ].
Quote:
Code:
"$cday" -lt "$bday" ]]
then
  let age=cyear-byear-1

let is not standard. Use POSIX arithmetic:

Code:
age=$(( $cyear - $byear - 1 ))
Quote:
Code:
else
  let age=cyear-byear
fi

echo "Age = "$age
Regards

This will run in any POSIX shell:

Code:
## Accept input on command line, or read interactively if none
case $# in
  3) bday=$1; bmonth=$2; byear=$3 ;;
  *) printf "Enter the birthdate (day month year): "
     read bday bmonth byear
     ;;
esac

## Ensure 2 digits for $bmonth and $bday
case $bmonth in ?) bmonth=0$bmonth;; esac
case $bday in ?) bday=0$bday;; esac

eval "$( date "+cyear=%Y cmonth=%m cday=%d" )"

[ $bmonth$bday -lt $cmonth$cday ] &&
 age=$(( $cyear - $byear )) ||
  age=$(( $cyear - $byear - 1 ))

echo "Age = $age"
  #5 (permalink)  
Old 12-30-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,927
Quote:
[[ ... ]] is not sh syntax. Use [ .. ].
]
Afraid it is supported by IEEE 1003.1:2004. Under shell command language reserved words section:

Reserved words are words that have special meaning to the shell
.....
The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:

[[ ]] function select
......
  #6 (permalink)  
Old 12-30-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by fpmurphy View Post
]
Afraid it is supported by IEEE 1003.1:2004.

No, It is neither supported nor required. In fact, it's behaviour is not even specified.
Quote:
Under shell command language reserved words section:

Reserved words are words that have special meaning to the shell
.....
The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:

[[ ]] function select
......

A POSIX shell is not required to support it, and if it does, it could do anything at all, including erase the contents of you hard drive.
  #7 (permalink)  
Old 01-01-2009
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,730
Well, the original poster did not say the solution had to be "strict POSIX compliance" so much of the discussion below is academic at best.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0