LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2010, 07:28 PM   #1
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Rep: Reputation: 0
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
 
Old 03-17-2010, 07:39 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Sounds like homework assignment.
If you don't want age= displayed then do not echo it.
As for the ??????
Code:
man date
 
Old 03-18-2010, 12:17 AM   #3
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
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"?
 
Old 03-18-2010, 12:25 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 03-18-2010, 12:31 AM   #5
ichauya
LQ Newbie
 
Registered: Feb 2008
Posts: 9

Rep: Reputation: 0
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.
 
Old 03-18-2010, 12:31 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by TheNewVegas View Post
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.
 
Old 03-18-2010, 12:42 AM   #7
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
date +%Y
 
Old 03-18-2010, 12:44 AM   #8
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
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
 
Old 03-18-2010, 11:31 PM   #9
TheNewVegas
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
  


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
Why I am not figuring the delete.sh bash script? acwbrat Programming 1 11-02-2007 01:37 AM
Using a perl script as a User's Shell john_b Linux - Security 4 07-28-2007 04:22 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
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
Figuring out yesterday's date within a script chrisk5527 Linux - General 1 02-04-2003 04:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:05 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