LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2012, 08:13 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203
Blog Entries: 3

Rep: Reputation: 24
bash script to calculate age difference with loop


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
 
Old 05-25-2012, 08:26 AM   #2
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
Code:
lage=18
read -p "What is your age? " uage
(( beeryears = age - lage  ))
(( beer = $beeryears * 100 ))
echo $beer

Last edited by uhelp; 05-25-2012 at 08:27 AM.
 
1 members found this post helpful.
Old 05-25-2012, 09:05 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Quote:
Originally Posted by uhelp View Post
Code:
lage=18
read -p "What is your age? " uage
(( beeryears = age - lage  ))
(( beer = $beeryears * 100 ))
echo $beer
It only works if the user age is 18 and not if older. I ran this code separately without adding to my own.

What is your age? 19
-1800

What is your age? 78
-1800

What is your age? 23
-1800
 
Old 05-25-2012, 02:40 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Code:
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:
Code:
beer=$( echo "$age $legalage $beerperyear" | awk '{ printf("%.0f\n", ($1-$2)*$3) }' )
 
1 members found this post helpful.
Old 05-27-2012, 09:07 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,000

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
The bash code example from uhelp has mixed up uage and age variables, a simple change will fix it:
Code:
(( beeryears = uage - lage  ))
 
1 members found this post helpful.
Old 05-28-2012, 06:58 AM   #6
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 203

Original Poster
Blog Entries: 3

Rep: Reputation: 24
Great that worked!
 
  


Reply

Tags
age, bash scripting, calculate, loop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
calculate logarithm in bash script bluesmoke Programming 5 12-14-2008 01:41 AM
bash - difference between executing program in for loop and typing each command? jlarsen Programming 14 11-10-2008 05:38 PM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 09:26 PM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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