Skip to content

Instantly share code, notes, and snippets.

@harishkannarao
Last active August 11, 2023 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harishkannarao/2acc043e4f5d0a9bafe8253915ec36eb to your computer and use it in GitHub Desktop.
Save harishkannarao/2acc043e4f5d0a9bafe8253915ec36eb to your computer and use it in GitHub Desktop.
Basic bash script example to calculate addition, subtraction, multiplication and division of two numbers
#!/bin/bash
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command.
set -e
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging.
# set -x
# Function to print usage of the script with different options
function print_usage()
{
echo "Usage: calculate.sh --operation OPERATION --lhs VALUE --rhs VALUE"
echo "or"
echo "Usage: calculate.sh -o OPERATION -l VALUE -r VALUE"
echo ""
echo "-o,--operation: add, subtract, multiply, divide"
}
# Check if the arguments is empty
if [[ $# -eq 0 ]]
then
print_usage
exit 1
fi
function validate_value() {
if [[ $# -eq 1 ]]
then
echo "Value not provided for $1" >&2
exit 1
fi
}
# Parse the positional arguments
VARARGS=()
DISPLAY_HELP=no
while [[ $# -gt 0 ]]
do
KEY="$1"
case $KEY in
-o|--operation)
shift # past argument
validate_value "$KEY" "$@"
OPERATION="$1"
shift # past value
;;
-l|--lhs)
shift # past argument
validate_value "$KEY" "$@"
LHS="$1"
shift # past value
;;
-r|--rhs)
shift # past argument
validate_value "$KEY" "$@"
RHS="$1"
shift # past value
;;
-h|--help)
DISPLAY_HELP=yes
shift # past argument
;;
*) # unknown option
VARARGS+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
# Uncomment below line to restore positional parameters with only the unrecognised values
# set -- "${VARARGS[@]}"
# Display usage of the script
if [[ "$DISPLAY_HELP" == "yes" ]]
then
print_usage
exit 0
fi
# Error out for unexpected parameters
if [[ ${#VARARGS[@]} -ne 0 ]]
then
# get length of an array
VARARGS_LENGTH=${#VARARGS[@]}
VARARGS_INDEX=0
# iterate through the array
while [ $VARARGS_INDEX -lt $VARARGS_LENGTH ]
do
echo "Unknown option: ${VARARGS[$VARARGS_INDEX]}" >&2
VARARGS_INDEX=$(( $VARARGS_INDEX + 1 ))
done
exit 1
fi
function validate_number()
{
reg_ex_for_number='^[+-]?[0-9]+([.][0-9]+)?$'
if ! [[ $2 =~ $reg_ex_for_number ]] ; then
echo "error: $1 value '$2' is missing or not a valid number" >&2
exit 1
fi
}
validate_number "LHS" "$LHS"
validate_number "RHS" "$RHS"
function add()
{
echo "$1 + $2" | bc -l
}
function subtract()
{
echo "$1 - $2" | bc -l
}
function multiply()
{
echo "$1 * $2" | bc -l
}
function divide()
{
echo "$1 / $2" | bc -l
}
case $OPERATION in
add)
RESULT=$(add $LHS $RHS)
;;
subtract)
RESULT=$(subtract $LHS $RHS)
;;
multiply)
RESULT=$(multiply $LHS $RHS)
;;
divide)
RESULT=$(divide $LHS $RHS)
;;
*)
echo "error: '$OPERATION' is not a valid operation" >&2
exit 1
;;
esac
echo "$RESULT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment