LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with a small script (figuring out user's age) (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-a-small-script-figuring-out-users-age-796155/)

TheNewVegas 03-17-2010 07:28 PM

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

crts 03-17-2010 07:39 PM

Sounds like homework assignment.
If you don't want age= displayed then do not echo it.
As for the ??????
Code:

man date

TheNewVegas 03-18-2010 12:17 AM

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"?

GrapefruiTgirl 03-18-2010 12:25 AM

Code:

shell# bday=1974
shell# echo "you are $(( $(date +%Y)-bday )) years old"

So, by putting the entire calculation inside $(( )) makes it interpreted as a numerical (mathematical) operation.

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

ichauya 03-18-2010 12:31 AM

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.

crts 03-18-2010 12:31 AM

Quote:

Originally Posted by TheNewVegas (Post 3902709)
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"?

What is the error message? Read the man page again and pay attention to the usage right at the beginning. What does it say about the format?
And again, if that specific 4 letters 'age=' bother you then do [b]NOT[b/] echo this specific 4 letters.

lupusarcanus 03-18-2010 12:42 AM

date +%Y

lupusarcanus 03-18-2010 12:44 AM

Sorry for the double post, I'm learning BASH scripting as well. I just made this one:

Quote:

#! /bin/bash

year=$(date +%Y)

echo -n "What is your birth year? "
read age

old=$(($year-$age))

echo "Your age is: $old"

if [[ "$old" -gt "60" ]] ; then
echo "Think about retirement..."
else
echo "How's life young one?"
fi

TheNewVegas 03-18-2010 11:31 PM

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.