Birthday Calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Birthday Calculation
# 1  
Old 12-29-2008
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  
Old 12-29-2008
Quote:
Originally Posted by mustaine85
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  
Old 12-29-2008
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  
Old 12-29-2008
Quote:
Originally Posted by Franklin52
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  
Old 12-30-2008
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  
Old 12-30-2008
Quote:
Originally Posted by fpmurphy
]
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  
Old 12-30-2008
I agree with fpmurphy. [[ ]] is supported in Posix and required though I personally have only found it useful in the context of "while [[ condition ]]".

[[ and ]] are keywords with strict syntax rules. A keyword starts a command and should not be used in the wrong context or be quoted (or you can get unpredictable results).

[ and ] are commands. (On early unix they were executables).

More importantly.
What goes between [[ and ]] is a "conditional expression" with one set of syntax.
What goes between [ and ] is a simple "test" with a simpler syntax. See "man test" where it you'll see that [ ] is an equivalent to "test".

The two syntaxes are covered in the man page for a Posix shell. As it happens some of the basic syntax is common between the two types of condition giving the (wrong) impression that the syntax is always interchangable.

The big syntax difference is with boolean expressions.


BTW. fpmurphy script gives syntax errors with both Posix sh and ksh because the substring variables are from bash. I'm not convinced that the line starting "if [[" will give the right result either (not tested).
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. What is on Your Mind?

Happy birthday Neo

Wish you many many happy returns of the day, stay blessed. (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

2. Linux

Happy Birthday Linux :)

The Linux kernel was originally created by Linus Torvalds, a Finnish computer science student, and first announced to the world on August 25, 1991-exactly 20 years ago today. At the time, Torvalds described his work as a "hobby" and contended that it would not be "big and professional" like the GNU... (2 Replies)
Discussion started by: itkamaraj
2 Replies

3. What is on Your Mind?

UNIX 40th Birthday

A little off topic, as far as not being a critical help issue, but I have been searching about for the OFFICIAL date that is considered the 40th Birthday of UNIX. Some folks mentioned it in March, I think do to a mention on Slashdot of the 40th birthday being this year, but I thought I recall... (1 Reply)
Discussion started by: scotbuff
1 Replies

4. News, Links, Events and Announcements

Happy birthday Linux

Linux has turned 10 . Happy birthday and congrats to the hackers whose labor pains keep giving us new tarballs. (From /. ) (1 Reply)
Discussion started by: mib
1 Replies
Login or Register to Ask a Question