In your last code revision, the case pattern "[+/-][0-9]" for "$nvramVal" is incorrect because it causes the function to fail with valid values (e.g. "0" or "1") and to succeed with invalid values (e.g. "-1"), and that's because it requires a digit to have a preceding sign character (plus or minus) which is not required for valid values. The correct case pattern would be "[0-9]" for positive digits, making any negative values invalid (which is based on @Viktor Jaep's current code)....
If you want to give more consistency, cleaner error handling, and simpler way of handling using "a case base" condition to the original edits I did to the_VPN_GetClientState_
, I would suggest:
...Bash:_VPN_GetClientState_() { case "$1" in [1-5]) local nvramVal="$($timeoutcmd$timeoutsec nvram get "vpn_client${1}_state")" case "$nvramVal" in [+/-][0-9]) echo "$nvramVal" ;; "") echo "${2:-0}" # Use the provided default or default to 0 ;; *) echo "WARNING: Unrecognized value in NVRAM: $nvramVal. Using default value." >&2 echo "${2:-0}" # Use the provided default or default to 0 ;; esac return 0 ;; *) echo "ERROR: Invalid client number. Please provide a number from 1 to 5." >&2 return 1 ;; esac }
The other revisions look fine, although I still prefer to check for a "No arguments" error at the top of the function (just a minor defensive programming technique that I use in most functions for my own work - YCSMV*).
Bash:
if [ $# -lt 1 ] || [ -z "$1" ]
then
echo "ERROR: No argument(s) provided." >&2
return 1
fi
NOTE:
@Viktor Jaep, I don't know the actual range of positive values that are valid for the VPN client state so I just assumed "[0-9]" but you might want to adjust that based on what you know and have observed during your own testing.
*Your Coding Standards May Vary.