Help with a small script (figuring out user's age)
Hey guys,
I am writing a small script and one section I'm trying to figure out is how to figure someones age based on the user's input of their year of birth. I have something like: echo -n "what is your year of birth?: " read age echo "You are age=$((????-$age))" How should I fill in the ???? with the current year, to come out with the user's age? And also how do i set it up so that it doesn't display You are age= in the output? Thanks a lot TheNewVegas |
Sounds like homework assignment.
If you don't want age= displayed then do not echo it. As for the ?????? Code:
man date |
yes I looked at the man pages for Date. but when I enter: echo "You are age=$((date %Y-$age))" I doesnt work. (DATE %Y for just the year).
Also, no echoing this doesnt work, because I want "You are (an age)" to be displayed. How do I get it so that "You are 21" for example displays rather than "You are age=21"? |
Code:
shell# bday=1974 Inside the $(( )) I have subtracted the variable $bday from the output of $(date +%y). You pretty much have it correct in your last post; however, your echo statement is actually echoing the "=" sign, because it is simply part of the string you are echoing. It has no mathematical significance in the string. Sasha |
What if you have a variable $age then you go - echo "You are $age". In your script you should have $age as equal to current year minus year of birth.
|
Quote:
And again, if that specific 4 letters 'age=' bother you then do [b]NOT[b/] echo this specific 4 letters. |
date +%Y
|
Sorry for the double post, I'm learning BASH scripting as well. I just made this one:
Quote:
|
hey thanks alot for the replies guys!
this is how i finally figured it out tho: echo -n "what is your year of birth? " read year DATE=$(date +%Y) echo "Your are $(($DATE-$year)) years old" thanks for the replies again Vegas |
All times are GMT -5. The time now is 06:49 AM. |