From 52e77c4e57fd499e6d1d1ea92ddf39d26ea67ea9 Mon Sep 17 00:00:00 2001 From: eslopfer Date: Thu, 5 Jan 2023 11:19:22 +0000 Subject: [PATCH] feat(division): add solution for exercise --- sh/tests/solutions/division.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sh/tests/solutions/division.sh diff --git a/sh/tests/solutions/division.sh b/sh/tests/solutions/division.sh new file mode 100644 index 00000000..253cc2c0 --- /dev/null +++ b/sh/tests/solutions/division.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# Check if two arguments were provided +if [ $# -ne 2 ] +then + echo "Error: two numbers must be provided" + exit 1 +fi + +# Check if the arguments are numeric +if ! [[ $1 =~ ^-?[0-9]*\.?[0-9]+$ ]] || ! [[ $2 =~ ^-?[0-9]*\.?[0-9]+$ ]] +then + echo "Error: both arguments must be numeric" + exit 1 +fi + +# Check if the second argument is not 0 +if [ $(echo "$2 == 0" | bc) -eq 1 ] +then + echo "Error: division by zero is not allowed" + exit 1 +fi + +# Divide the first argument by the second using bc +result=$(echo "$1 / $2" | bc -l) + +# Output the result +echo $result