Help with a small script (figuring out user's age)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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"?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.