[SOLVED] bash script to calculate age difference with loop
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I essentially want to calculate this:
"calculate how much beer an 18+ user has drunk statistically (100 liters/year) and print this information for the user."
Not sure how to calculate it i.e. use a loop or other to use the difference in specified age versus user input age, multiply the result, I made an attempt in the last section of code.
Code:
#!/bin/bash
# This script check's the user's age and if they can legally drink alcohol
age=16
lage=18
echo "What age are you?"
read uage
years=$[$age - $uage]
beer=$[$years ge $lage]
if [ "$uage" -ge "$age" ] ; then
echo -e "You are $uage and are of legal age to drink alcohol\n" &&
echo -e "You have drank $ litres of beer since you turned 18 years of age."
exit 1
else
echo -e "You are $uage and are not of legal age to drink alcohol\n" &&
echo -e "You have to wait $years years before you can legally drink alcohol"
fi
while [$beer]; do
echo " "
let $beer = $beer+1 * 100
done
if [ "$uage" == "$lage" ] ; then
echo " You have drank $beer litres of beer since you were born"
else
echo "You have not drank much beer"
fi
beerperyear=100
legalage=18
read -p "What is your age? " age
beer=$(( (age - legalage) * beerperyear ))
if (( beer > 0 )); then
echo "You've drank $beer liters of beer."
else
beer=0
echo "You're not old enough to have drunk beer."
fi
For fractional years (but integral liters of beer) it's easiest to use awk:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.