How to calculate BMI in shell script. | How to calculate Body Mass Index in Linux.

 

Body Mass Index
Body Mass Index


Software is required for a small smart device (any embedded system e.g. smartwatch) for sports/health care services particularly in the time of Quarantine. The smart device has a lightweight version of the Linux operating system installed on it with bourne shell. You are required to develop software for monitoring health by writing code in Shell Scripting ONLY. The requirements of the software, divided into 3 modules, are described below in detail.

 

Question # 1                                                                             

The first module of the software requires implementation for the calculation of BMI (Body Mass Index) to tell the user if their weight falls in healthy category or not.

BMI = Weight / (height)2

 

Take value for weight in Kilograms and height in meters (i.e. SI units) from user.

After calculation of BMI, check-in which range/category does the value fall in the following table?

The guideline for ranges of healthy BMI set by WHO are given below:

15 – 16: Severely underweight

16 – 18.5: Underweight

18.5 – 25: Normal Range (healthy)

25 – 30: Overweight

30 – 35: Obese class I – Moderately obese

35 – 40: Obese class II – Severely obese

At the end display the BMI and category on screen. (It should be visible in a snapshot of your output.)

 

Solution:

Required Code:

#!/bin/sh
startRange=15.0;
SeverelyUnderweigh=16.0;
Underweight=18.5;
NormalRange=25.0;
Overweight=30.0;
ObeseClass1=35.0;
ObeseClass2=40.0;
severelyUnderweigh="15 – 16: Severely underweight";
underweight="16 – 18.5: Underweight";
normalRange="18.5 – 25: Normal Range (healthy)";
overweight="25 – 30: Overweight";
obeseClass1="30 – 35: Obese class I – Moderately obese";
obeseClass2="35 – 40: Obese class II – Severely obese";
echo "Enter Height In Meter.";
read height;
echo "Enter Weight In Killogram.";
read weight;
height=`expr $height \* $height`;
bodyMassIndex=$(echo "scale=1; $weight / $height" | bc);
echo "<<------------------------>>*<<------------------------>>*<<------------------------>>"
if [ $(echo "$bodyMassIndex>=$startRange"|bc) -gt 0 -a $(echo "$bodyMassIndex<$SeverelyUnderweigh"|bc) -gt 0 ]
then
   echo "$severelyUnderweigh = $bodyMassIndex"
elif [ $(echo "$bodyMassIndex>=$SeverelyUnderweigh"|bc) -gt 0 -a $(echo "$bodyMassIndex<$Underweight"|bc) -gt 0 ]
then
    echo "$underweight = $bodyMassIndex";
elif [ $(echo "$bodyMassIndex>=$Underweight"|bc) -gt 0 -a $(echo "$bodyMassIndex<$NormalRange"|bc) -gt 0 ]
then
    echo "$normalRange = $bodyMassIndex";
elif [ $(echo "$bodyMassIndex>=$NormalRange"|bc) -gt 0 -a $(echo "$bodyMassIndex<$Overweight"|bc) -gt 0 ]
then
    echo "$overweight = $bodyMassIndex";
elif [ $(echo "$bodyMassIndex>=$Overweight"|bc) -gt 0 -a $(echo "$bodyMassIndex<$ObeseClass1"|bc) -gt 0 ]
then
    echo "$obeseClass1 = $bodyMassIndex";
elif [ $(echo "$bodyMassIndex>=$ObeseClass1"|bc) -gt 0 -a $(echo "$bodyMassIndex<$ObeseClass2"|bc) -gt 0 ]
then
    echo "$obeseClass2 = $bodyMassIndex";
else
   echo "Invalid Body Mass Index"
fi
echo "<<------------------------>>*<<------------------------>>*<<------------------------>>"
echo "$severelyUnderweigh.";
echo "$underweight.";
echo "$normalRange.";
echo "$overweight.";
echo "$obeseClass1.";
echo "$obeseClass2.";


Required Output:

Body Mass Index example
Body Mass Index example

 

Question # 2                                                                                (7)

The Second module of the software involves the implementation of counting the number of steps taken by the user per day and the calculation of Calories burned due to this movement. Since this assignment does not involve any hardware at this level, therefore, you will take the number of steps as  user input (otherwise in real-time the count of steps will be provided by the sensors). The algorithm for calculation, divided into six steps for your ease, is as follows.

 

1.    Calories burned per mile = 0.57 x Weight (in lbs.)

2.    Pace length (step length) = Height (in cm) * 0.415.

3.    Steps taken in 1 mile = 160934.4 / pace length.

4.    Conversion Factor = Calories burned per mile / Steps taken in 1 mile.

5.    Calories Burned = Steps Count * Conversion Factor.

6.    Distance Covered = Steps Count * Pace length (cm).

 

Make sure to use ONLY those units of measurements that are mentioned. Weight and Height are already taken from the user in the first question. Use the same value for this question too. Convert height into cm and weight into lbs. before your calculations.

 

At the end display the following on-screen. (It should be visible in snapshot of output)

Calories burned are: ___________cal.

Distance covered is: ____________km.

 

Solution:

Required Code:

#!/bin/sh
echo "Enter Height In Meter.";
read height;
echo "Enter Weight In Killogram.";
read weight;
##Standared Value One kg is equal to 2.20462
poundInOneKg=2.20462;
centimetreInMeter=100;
oneKm=1000; ##sta.
Mile=160934.4; ##Given Value in paper.
Calorise=0.57; ##Given Value in paper.
weightInPound=$(echo "scale=1; $weight*$poundInOneKg" | bc)
CaloriesBurnedPerMile=$(echo "scale=3; $Calorise*$weightInPound" | bc)
heightInCm=$(echo "$height*$centimetreInMeter"|bc)
Pacelength=$(echo "$heightInCm*0.415"|bc);
stepsInOneMile=$(echo "$Mile/$Pacelength"|bc)
conversionFactor=$(echo "scale=5; $CaloriesBurnedPerMile/$stepsInOneMile"|bc)
caloriesBurned=$(echo "scale=3; $stepsInOneMile*$conversionFactor"|bc)
distanceCovered=$(echo "scale=3; $stepsInOneMile*$Pacelength"|bc)
distanceInMeter=$(echo "scale=3; $distanceCovered/$centimetreInMeter"|bc)
distanceInKm=$(echo "scale=3; $distanceInMeter/$oneKm"|bc)
echo "<<-------------------------------------->><<-------------------------------------->>;
echo "Calories Burned are = $caloriesBurned .cal"
echo "Distance Covered is = $distanceInKm .km"
Body Mass Index example
Body Mass Index example

Required Output:


Question # 3                    

In the last part, you are required to analyze the activity and health rate using above modules.

a) First, make a menu of the program as follows:

Press 1 to calculate your Body Mass Index

Press 2 to calculate number of calories burned today.

Press 3 to calculate your Health and Activity rate.

Press 4 to exit.

b) Second, write code for the calculation of activity rate using the following steps.

  1. Take data (burned calories) of at least 5-10 days using module 2. [Hint: Run the code of question 2 in loop and get the values of calories from there.]
  2. Store values of burned calories in an array in sequence e.g.

 

Calories burned on Day 1

Calories burned on Day 2

Calories burned on Day 3

 

  1. Declare and initialize a count/rate variable as zero. 
  2. Traverse that array from start to end by comparing the element on one index with the element on the next index. If the element on the next index is greater than the previous one, then increment the count by 1. If it is smaller than the previous, then decrement the count. If it is equal, then do nothing. Do this for all elements.
  3. Find out the average calories burned by taking an average of the array.
  4. In the end, Categorize your user activity according to the following table.

 

Count

Less than -20

activity rate decreasing

Greater than 20

activity rate increasing

In between -20 and 20

activity rate constant

Average Calories burned

Greater than or equal to 300

Healthy

Less than 300 cal.

Unhealthy

 

At the end display the following on-screen. (It should be visible in your output):

i-         Your menu.

ii-        Your whole array.

iii-      Activity rate is __________________ and your activity status shows you are ___(healthy/unhealthy) ____.

Solution:

Required Code:

#!/bin/sh
caloriesBurnedArray=""
index=0;
while [ $index -lt 5 ]
do
    echo "Enter Height In Meter.";
    read height;
    echo "Enter Weight In Killogram.";
    read weight;
    ##Standared Value One kg is equal to 2.20462
    poundInOneKg=2.20462;
    centimetreInMeter=100;
    oneKm=1000; ##sta.
    Mile=160934.4; ##Given Value in paper.
    Calorise=0.57; ##Given Value in paper.
    weightInPound=$(echo "scale=1; $weight*$poundInOneKg" | bc)
    CaloriesBurnedPerMile=$(echo "scale=3; $Calorise*$weightInPound" | bc)
    heightInCm=$(echo "$height*$centimetreInMeter"|bc)
    Pacelength=$(echo "$heightInCm*0.415"|bc);
    stepsInOneMile=$(echo "$Mile/$Pacelength"|bc)
    conversionFactor=$(echo "scale=5; $CaloriesBurnedPerMile/$stepsInOneMile"|bc)
    caloriesBurned=$(echo "scale=3; $stepsInOneMile*$conversionFactor"|bc)
    distanceCovered=$(echo "scale=3; $stepsInOneMile*$Pacelength"|bc)
    distanceInMeter=$(echo "scale=3; $distanceCovered/$centimetreInMeter"|bc)
    distanceInKm=$(echo "scale=3; $distanceInMeter/$oneKm"|bc)
    echo "<<-------------------------------------->><<-------------------------------------->><<-------------------------------------->>";
    echo "Calories Burned are = $caloriesBurned .cal"
    caloriesBurnedArray="${caloriesBurnedArray} $caloriesBurned"
    index=`expr $index + 1`
done
counter=0;
i=0;
check=true;
previousValue=-1;
averageCaloriesBurned=0;
for i in $caloriesBurnedArray;
do
    if [ $(echo "$previousValue!=-1"|bc) -gt 0 ]
    then
        if [ $(echo "$previousValue<$i"|bc) -gt 0 ]
        then
            counter=`expr $counter + 1`
        elif [ $(echo "$previousValue>$i"|bc) -gt 0 ]
        then
            counter=`expr $counter - 1`
        fi
    fi
    previousValue=$i
    averageCaloriesBurned=$(echo "scale=2;$averageCaloriesBurned+$i"|bc)
done
echo "counter = $counter"
status=""
averageCaloriesBurned=$(echo "scale=2;$averageCaloriesBurned/5"|bc)
echo "Average Calories Burned = $averageCaloriesBurned"
if [ $(echo "$averageCaloriesBurned<-20"|bc) -gt 0 ]
then
    status="activity rate decreasing";
elif [ $(echo "$averageCaloriesBurned<-20"|bc) -gt 0 -a $(echo "$averageCaloriesBurned>20"|bc) -gt 0 ]
then
    status="activity rate constant";
elif [ $(echo "$averageCaloriesBurned>=300"|bc) -gt 0 ]
then
    status="Healthy";
elif [ $(echo "$averageCaloriesBurned<300"|bc) -gt 0 ]
then
    status="Unhealthy";
elif [ $(echo "$averageCaloriesBurned>20"|bc) -gt 0 ]
then
    status="activity rate increasing";
fi
status="Activity rate is $counter and your activity status shows you are $status."
echo "Status = $status."

Required Output

Body Mass Index example
Body Mass Index example


Body Mass Index example
Body Mass Index example

0 Comments