From bbfbf67fcf44dbf1c19a5238fc80e1fb191a0095 Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Mon, 15 Jun 2020 22:52:24 +0100 Subject: [PATCH] further simplify the function, per @dl6er's suggestion Signed-off-by: Adam Warner --- automated install/basic-install.sh | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 0b2ddef1..6e05eda4 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1017,30 +1017,16 @@ valid_ip() { local ip=${1} local stat=1 - # If the IP matches the format xxx.xxx.xxx.xxx (optional port of range #0-65536), also ensure string ends with 0-9 - if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(#[0-9]{1,5})?$ ]]; then - # Save the old Internal Field Separator in a variable - OIFS=$IFS - # and set the new one to a dot (period) - IFS='.#' - # Put the IP into an array - read -r -a ip <<< "${ip}" - # Restore the IFS to what it was - IFS=${OIFS} + # One IPv4 element is 8bit: 0 - 256 + local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)"; + # optional port number starting '#' with range of 1-65536 + local portelem="(#([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6]))?" + # build a full regex string from the above parts + local regex="^${ipv4elem}\.${ipv4elem}\.${ipv4elem}\.${ipv4elem}${portelem}$" - ## Evaluate each octet by checking if it's less than or equal to 255 (the max for each octet) - [[ "${ip[0]}" -le 255 && "${ip[1]}" -le 255 && "${ip[2]}" -le 255 && "${ip[3]}" -le 255 ]] - # Save the exit code - stat=$? + [[ $ip =~ ${regex} ]] - # If there is a 5th part to the array, then it is a port number - check it is between 1 and 65536 - if [[ "${ip[4]}" ]]; then - [[ "${ip[4]}" -ge 1 && "${ip[4]}" -le 65536 ]] - # Save the exit code - stat=$? - fi - - fi + stat=$? # Return the exit code return "${stat}" }