From 55098fb8532d2175b0190ba5835cb15a8e411623 Mon Sep 17 00:00:00 2001 From: TimForbes <> Date: Mon, 10 Apr 2023 08:46:08 +0200 Subject: [PATCH] Optimization: Use a function + case statement to print the error message and exit when a required OS version is not met --- openvpn-install.sh | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/openvpn-install.sh b/openvpn-install.sh index baa92d7..64bad9e 100644 --- a/openvpn-install.sh +++ b/openvpn-install.sh @@ -53,17 +53,28 @@ Supported distros are Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS and Fedora. esac -if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then - echo "Debian 9 or higher is required to use this installer. -This version of Debian is too old and unsupported." - exit -fi +function check_os_version { + local required_version=$1 + if [[ "$os_version" -lt "$required_version" ]]; then + echo "$os_name $required_version or higher is required to use this installer. +This version of $os_name is too old and unsupported." + exit 1 + fi +} + +case $os in + ubuntu) + check_os_version 1804 + ;; + debian) + check_os_version 9 + ;; + centos) + check_os_version 7 + ;; +esac + -if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then - echo "CentOS 7 or higher is required to use this installer. -This version of CentOS is too old and unsupported." - exit -fi # Detect environments where $PATH does not include the sbin directories if ! grep -q sbin <<< "$PATH"; then