Improve shell flags
This commit is contained in:
parent
8533dfaa36
commit
48aeef052c
|
@ -13,16 +13,28 @@
|
||||||
# 2023-01-24
|
# 2023-01-24
|
||||||
# }}}
|
# }}}
|
||||||
# Flags {{{
|
# Flags {{{
|
||||||
## Exit on error
|
## Exit on error {{{
|
||||||
set -o errexit
|
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 {{{
|
# Vars {{{
|
||||||
PROGNAME=$(basename "${0}"); readonly PROGNAME
|
PROGNAME=$(basename "${0}"); readonly PROGNAME
|
||||||
PROGDIR=$(readlink -m $(dirname "${0}")); readonly PROGDIR
|
PROGDIR=$(readlink --canonicalize-missing $(dirname "${0}")); readonly PROGDIR
|
||||||
ARGS="${*}"; readonly ARGS
|
ARGS="${*}"; readonly ARGS
|
||||||
readonly NBARGS="${#}"
|
readonly NBARGS="${#}"
|
||||||
[ -z "${DEBUG}" ] && DEBUG=1
|
[ -z "${DEBUG-}" ] && DEBUG=1
|
||||||
## Export DEBUG for sub-script
|
## Export DEBUG for sub-script
|
||||||
export DEBUG
|
export DEBUG
|
||||||
|
|
||||||
|
@ -72,7 +84,7 @@ OPTIONS :
|
||||||
Define VPN user name to use.
|
Define VPN user name to use.
|
||||||
Default : ${VPN_USER_DEFAULT}
|
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.
|
and to check profile name.
|
||||||
|
|
||||||
HELP
|
HELP
|
||||||
|
@ -96,7 +108,7 @@ error_message() { # {{{
|
||||||
local_error_code="${2}"
|
local_error_code="${2}"
|
||||||
|
|
||||||
## Print message
|
## 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
|
unset local_error_message
|
||||||
|
|
||||||
|
@ -106,19 +118,19 @@ error_message() { # {{{
|
||||||
define_vars() { # {{{
|
define_vars() { # {{{
|
||||||
|
|
||||||
## If request_status wasn't defined (argument) {{{
|
## If request_status wasn't defined (argument) {{{
|
||||||
if [ -z "${request_status}" ]; then
|
if [ -z "${request_status-}" ]; then
|
||||||
## Use default value
|
## Use default value
|
||||||
readonly request_status="${REQUEST_STATUS_DEFAULT}"
|
readonly request_status="${REQUEST_STATUS_DEFAULT}"
|
||||||
fi
|
fi
|
||||||
## }}}
|
## }}}
|
||||||
## If vpn_profile_name wasn't defined (argument) {{{
|
## If vpn_profile_name wasn't defined (argument) {{{
|
||||||
if [ -z "${vpn_profile_name}" ]; then
|
if [ -z "${vpn_profile_name-}" ]; then
|
||||||
## Use default value
|
## Use default value
|
||||||
readonly vpn_profile_name="${VPN_PROFILE_NAME_DEFAULT}"
|
readonly vpn_profile_name="${VPN_PROFILE_NAME_DEFAULT}"
|
||||||
fi
|
fi
|
||||||
## }}}
|
## }}}
|
||||||
## If vpn_user wasn't defined (argument) {{{
|
## If vpn_user wasn't defined (argument) {{{
|
||||||
if [ -z "${vpn_user}" ]; then
|
if [ -z "${vpn_user-}" ]; then
|
||||||
## Use default value
|
## Use default value
|
||||||
readonly vpn_user="${VPN_USER_DEFAULT}"
|
readonly vpn_user="${VPN_USER_DEFAULT}"
|
||||||
fi
|
fi
|
||||||
|
@ -200,7 +212,6 @@ main() { # {{{
|
||||||
## If the VPN must be started {{{
|
## If the VPN must be started {{{
|
||||||
if [ "${request_status}" = "start" ]; then
|
if [ "${request_status}" = "start" ]; then
|
||||||
debug_message "-- Connect VPN BEGIN"
|
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 {{{
|
### 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)
|
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
|
if [ "${systemd_forticlient_status}" = "failed" ]; then
|
||||||
|
@ -221,7 +232,7 @@ main() { # {{{
|
||||||
|
|
||||||
if [ "${forticlient_status}" = "Not Running" ]; then
|
if [ "${forticlient_status}" = "Not Running" ]; then
|
||||||
### If script was started from a launcher {{{
|
### 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
|
### Try to launch a new TERM_EMULATOR to ask the password
|
||||||
"${TERM_EMULATOR}" -e forticlient vpn connect "${vpn_profile_name}" --password --user="${vpn_user}" \
|
"${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
|
|| 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 # {{{
|
# Manage arguments # {{{
|
||||||
# This code can't be in a function due to argument management
|
# This code can't be in a function due to argument management
|
||||||
|
|
||||||
|
@ -260,19 +268,20 @@ if [ ! "${NBARGS}" -eq "0" ]; then
|
||||||
|
|
||||||
manage_arg="0"
|
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
|
# Parse all argument one by one
|
||||||
while printf -- '%s' "${1}" | grep -q -- "."; do
|
while printf -- '%s' "${1-}" | grep --quiet -- "."; do
|
||||||
|
|
||||||
case "${1}" in
|
case "${1}" in
|
||||||
-d|--debug ) ## debug
|
-d|--debug ) ## debug
|
||||||
DEBUG=0
|
DEBUG=0
|
||||||
debug_message "--- Manage argument BEGIN"
|
debug_message "--- Manage argument BEGIN"
|
||||||
;;
|
;;
|
||||||
-h|--help ) ## help
|
|
||||||
usage
|
|
||||||
## Exit after help informations
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
--start|start ) ## Define request_status to start
|
--start|start ) ## Define request_status to start
|
||||||
## Define var
|
## Define var
|
||||||
readonly request_status="start"
|
readonly request_status="start"
|
||||||
|
|
Loading…
Reference in New Issue