diff --git a/fix-kubectl b/fix-kubectl index 18ce27e..7ed8440 100755 --- a/fix-kubectl +++ b/fix-kubectl @@ -4,21 +4,25 @@ # - uneeded, as this checked whether script is sourced # SC1090 Can't follow non-constant source +# User basename instead of ${0:##*/} to be POSIX compliant +BASE_0=${BASE_0:-$(basename $0)} +SCRIPT_DIR=${SCRIPT_DIR:-"$( cd -- "$( dirname -- "$0" )" &> /dev/null && pwd )"} +BASE_SHELL=$(basename "$SHELL") + # Helper function is_sourced() { if [ -n "$ZSH_VERSION" ]; then case $ZSH_EVAL_CONTEXT in *:file:*) return 0;; esac - else # Add additional POSIX-compatible shell names here, if needed. - case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh) return 0;; esac + else + # case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh) return 0;; esac + case "$(basename $0)" in $BASE_SHELL|-$BASE_SHELL) return 0;; esac fi return 1; # NOT sourced. } -BASE_0=${BASE_0:-$0} -BASE_SHELL=$(basename "$SHELL") if is_sourced; then + # This part is sourced, and might run in a non-bash shell DBG_="Is Sourced" - SCRIPT_DIR=$( cd -- "$( dirname -- "${BASE_0}" )" &> /dev/null && pwd ) USER_HOME=$HOME [[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" @@ -101,7 +105,7 @@ if is_sourced; then } fi elif [[ "$1" == '-' ]]; then - printf 'BASE_0="%s"\n' "${BASE_0}" + printf 'BASE_0="%s"\nSCRIPT_DIR=%s\n' "${BASE_0}" "${SCRIPT_DIR}" cat "${BASH_SOURCE[0]}" else SCRIPT_NAME="$BASE_0"