bash-exercises/calc.sh

36 lines
656 B
Bash
Executable File

#!/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))"