Skip to content

Instantly share code, notes, and snippets.

@nikAizuddin
Last active August 29, 2015 14:11
Show Gist options
  • Save nikAizuddin/0712304e15d58556d343 to your computer and use it in GitHub Desktop.
Save nikAizuddin/0712304e15d58556d343 to your computer and use it in GitHub Desktop.
How to compare float numbers
#!/bin/bash
#=====================================================================
# FILE: compare_float.sh
#
# USAGE: bash compare_float.sh
#
# DESCRIPTION: This script shows how to compare float numbers.
#
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Nik Mohamad Aizuddin bin Nik Azmi
# EMAIL: [email protected]
# VERSION: 1.0
# CREATED: 12/DECEMBER/2014, 21:09
# REVISION: ---
#=====================================================================
#---------------------------------------------------------------------
# Get input from user
#---------------------------------------------------------------------
echo -n "Please insert number_1 (for example 3.125): "
read number_1
echo -n "Please insert number_2 (for example 6.129): "
read number_2
#---------------------------------------------------------------------
# Compare number_1 and number_2
#---------------------------------------------------------------------
is_less_than=`echo "$number_1 < $number_2" | bc -l`
is_equal=`echo "$number_1 == $number_2" | bc -l`
is_more_than=`echo "$number_1 > $number_2" | bc -l`
#---------------------------------------------------------------------
# Print results
#---------------------------------------------------------------------
if [[ $is_less_than -eq 1 ]]; then
echo "number_1 is less than number_2"
fi
if [[ $is_equal -eq 1 ]]; then
echo "number_1 is equal to number_2"
fi
if [[ $is_more_than -eq 1 ]]; then
echo "number_1 is more than number_2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment