From 48aeef052c93e604bbcd6eb48f9e7016f032a89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Tue, 24 Jan 2023 18:26:08 +0100 Subject: [PATCH] Improve shell flags --- ur/fortinet.vpn.sh | 49 +++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/ur/fortinet.vpn.sh b/ur/fortinet.vpn.sh index 8fbbebf..137e817 100755 --- a/ur/fortinet.vpn.sh +++ b/ur/fortinet.vpn.sh @@ -13,16 +13,28 @@ # 2023-01-24 # }}} # Flags {{{ -## Exit on error +## Exit on error {{{ set -o errexit - +## }}} +## Exit on unset var {{{ +### Use "${VARNAME-}" to test a var that may not have been set +set -o nounset +## }}} +## Pipeline command is treated as failed {{{ +### Not available in POSIX sh − https://github.com/koalaman/shellcheck/wiki/SC3040 +#set -o pipefail +## }}} +## Help with debugging {{{ +### Call the script by prefixing it with "TRACE=1 ./script.sh" +if [ "${TRACE-0}" -eq 1 ]; then set -o xtrace; fi +## }}} # }}} # Vars {{{ PROGNAME=$(basename "${0}"); readonly PROGNAME -PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR +PROGDIR=$(readlink --canonicalize-missing $(dirname "${0}")); readonly PROGDIR ARGS="${*}"; readonly ARGS readonly NBARGS="${#}" -[ -z "${DEBUG}" ] && DEBUG=1 +[ -z "${DEBUG-}" ] && DEBUG=1 ## Export DEBUG for sub-script export DEBUG @@ -72,7 +84,7 @@ OPTIONS : Define VPN user name to use. Default : ${VPN_USER_DEFAULT} -For a first connection, try to start `forticlient gui` first to configure EMS +For a first connection, try to start \`forticlient gui\` first to configure EMS and to check profile name. HELP @@ -96,7 +108,7 @@ error_message() { # {{{ local_error_code="${2}" ## Print message - printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}" + printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}" >&2 unset local_error_message @@ -106,19 +118,19 @@ error_message() { # {{{ define_vars() { # {{{ ## If request_status wasn't defined (argument) {{{ - if [ -z "${request_status}" ]; then + if [ -z "${request_status-}" ]; then ## Use default value readonly request_status="${REQUEST_STATUS_DEFAULT}" fi ## }}} ## If vpn_profile_name wasn't defined (argument) {{{ - if [ -z "${vpn_profile_name}" ]; then + if [ -z "${vpn_profile_name-}" ]; then ## Use default value readonly vpn_profile_name="${VPN_PROFILE_NAME_DEFAULT}" fi ## }}} ## If vpn_user wasn't defined (argument) {{{ - if [ -z "${vpn_user}" ]; then + if [ -z "${vpn_user-}" ]; then ## Use default value readonly vpn_user="${VPN_USER_DEFAULT}" fi @@ -200,7 +212,6 @@ main() { # {{{ ## If the VPN must be started {{{ if [ "${request_status}" = "start" ]; then debug_message "-- Connect VPN BEGIN" - #systemd_forticlient_status=$(sudo systemctl status forticlient.service | grep --word-regexp "Active:" | sed 's/Active: \(.*\)/\1/' || error_message "Error while requesting current VPN status." 22) ### If forticlient.service unit is not started {{{ systemd_forticlient_status=$(systemctl show forticlient.service | grep "^ActiveStat" | sed 's/.*=\(.*\)/\1/' || error_message "Error while requesting forticlient.service unit status." 21) if [ "${systemd_forticlient_status}" = "failed" ]; then @@ -221,7 +232,7 @@ main() { # {{{ if [ "${forticlient_status}" = "Not Running" ]; then ### If script was started from a launcher {{{ - if [ -n "${DISPLAY}" ] && [ "${TERM}" = "linux" ]; then + if [ -n "${DISPLAY-}" ] && [ "${TERM}" = "linux" ]; then ### Try to launch a new TERM_EMULATOR to ask the password "${TERM_EMULATOR}" -e forticlient vpn connect "${vpn_profile_name}" --password --user="${vpn_user}" \ || error_message "Error when connecting to VPN profile (${vpn_profile_name})." 24 @@ -250,9 +261,6 @@ main() { # {{{ } # }}} -true > /tmp/vpn.env -env | sort >> /tmp/vpn.env - # Manage arguments # {{{ # This code can't be in a function due to argument management @@ -260,19 +268,20 @@ if [ ! "${NBARGS}" -eq "0" ]; then manage_arg="0" + ## If the first argument ask for help (h|help|-h|-help|-*h|-*help) + if printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^-*h(elp)?$"; then + usage + exit 0 + fi + # Parse all argument one by one - while printf -- '%s' "${1}" | grep -q -- "."; do + while printf -- '%s' "${1-}" | grep --quiet -- "."; do case "${1}" in -d|--debug ) ## debug DEBUG=0 debug_message "--- Manage argument BEGIN" ;; - -h|--help ) ## help - usage - ## Exit after help informations - exit 0 - ;; --start|start ) ## Define request_status to start ## Define var readonly request_status="start"