diff --git a/dabo/analyze.sh b/dabo/analyze.sh index f77b824..19f4c66 100755 --- a/dabo/analyze.sh +++ b/dabo/analyze.sh @@ -22,6 +22,7 @@ function analyze { . dabo/functions/g_num_is_approx.sh . dabo/functions/g_num_is_between.sh . dabo/functions/g_num_is_higher.sh + . dabo/functions/g_num_valid_number.sh . dabo/dabo-bot.conf . dabo-bot.conf . analyze.conf diff --git a/dabo/functions/g_num_is_approx.sh b/dabo/functions/g_num_is_approx.sh index 7e842b7..3b5ebf7 100644 --- a/dabo/functions/g_num_is_approx.sh +++ b/dabo/functions/g_num_is_approx.sh @@ -7,14 +7,8 @@ function g_num_is_approx { local f_percentage_down=$4 # Check for valid decimal number - for f_numtest in "${f_num}" "${f_base}" "${f_percentage_up}" "${f_percentage_down}" - do - if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] - then - g_echo "\"${f_numtest}\"Not a valid number" - return 1 - fi - done + + g_num_valid_number "${f_num}" "${f_base}" "${f_percentage_up}" "${f_percentage_down}" || return 1 local f_from=$(echo "${f_base} - (${f_base} / 100 * ${f_percentage_down})" | bc -l) local f_to=$(echo "${f_base} + (${f_base} / 100 * ${f_percentage_up})" | bc -l) diff --git a/dabo/functions/g_num_is_between.sh b/dabo/functions/g_num_is_between.sh index 18df763..3c0cc2d 100644 --- a/dabo/functions/g_num_is_between.sh +++ b/dabo/functions/g_num_is_between.sh @@ -25,16 +25,9 @@ function g_num_is_between { fi fi - # Check for valid decimal number - for f_numtest in "$f_num" "$f_between1" "$f_between2" - do - if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] - then - g_echo "\"${f_numtest}\"Not a valid number" - return 1 - fi - done - + # Check for valid number + g_num_valid_number "$f_num" "$f_between1" "$f_between2" || return 1 + # Check which is the low (from) and the high (to) number if [ $(echo "${f_between1} < ${f_between2}" | bc -l) -ne 0 ] then diff --git a/dabo/functions/g_num_is_higher.sh b/dabo/functions/g_num_is_higher.sh index 53c4816..4a54535 100644 --- a/dabo/functions/g_num_is_higher.sh +++ b/dabo/functions/g_num_is_higher.sh @@ -15,15 +15,8 @@ function g_num_is_higher { fi fi - # Check for valid decimal number - for f_numtest in "$f_num" "$f_checkhigher" - do - if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] - then - g_echo "\"${f_numtest}\"Not a valid number" - return 1 - fi - done + # Check for valid number + g_num_valid_number "$f_num" "$f_checkhigher" || return 1 # Check which is the low (from) and the high (to) number # Check if given number is in or out range diff --git a/dabo/functions/g_num_valid_number.sh b/dabo/functions/g_num_valid_number.sh new file mode 100644 index 0000000..c00c23d --- /dev/null +++ b/dabo/functions/g_num_valid_number.sh @@ -0,0 +1,12 @@ +function g_num_valid_number { + local f_num + for f_num in $@ + do + if ! [[ ${f_num} =~ ^(-\.)?[0-9]+(\.[0-9]+)?$ ]] + then + g_echo "\"${f_num}\": Not a valid number" + return 1 + fi + done +} +