From cc335ca6a52138dc39572f2901547c2dd700ac0f Mon Sep 17 00:00:00 2001 From: raul Date: Wed, 11 Dec 2024 09:50:07 +0100 Subject: [PATCH] Add calc.sh --- calc.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 calc.sh diff --git a/calc.sh b/calc.sh new file mode 100755 index 0000000..05a8341 --- /dev/null +++ b/calc.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +OPERADORES_DISPONIBLES=("+" "x" "-" "/") +VALID=false +FINAL_SEQUENCE="" +RE='^[0-9]+$' +NG='^-[0-9]+$' + +for arg in $@; do + valid_num=false + valid_oper=false + + for oper in "${OPERADORES_DISPONIBLES[@]}"; do + if [[ "$oper" == "$arg" ]] ; then + valid_oper=true + fi + done + + if [[ $arg =~ $RE || $arg =~ $NG ]] ; then + valid_num=true + fi + + if [[ "$valid_num" != true && "$valid_oper" != true ]] ; then + echo "INVALID CHARACTER DETECTED: $arg" + exit + fi + + if [[ $arg == "x" ]]; then + FINAL_SEQUENCE+="* " + else + FINAL_SEQUENCE+="$arg " + fi +done +#echo "$FINAL_SEQUENCE" +echo "$FINAL_SEQUENCE = $(($FINAL_SEQUENCE))"