3
\$\begingroup\$
#!/bin/bash
# Change the environment in which you are currently working.
# Actually, it calls the relevant 'lettus.sh' script

if [ "${BASH_SOURCE[0]}" == "$0" ]; then
    echo "Try running this as \". chenv $1\""
    exit 0
fi

usage(){
    echo "Usage: . ${PROG}        -- Shows a list of user-selectable environments."
    echo "       . ${PROG} [env]  -- Select environment."
    echo "       . ${PROG} -h     -- Shows this usage screen."
    return
}

showEnv(){
    # check if index0 exists, assume we have at least the first (zeroth) element
    #if [ -z "${envList}" ]; then
    if [ -z "${envList[0]}" ]; then
        echo "array \$envList is empty! " >&2
        return 1
    fi

    # Show all elements in array (0 -> n-1)
    for i in $(seq 0 $((${#envList[@]} - 1))); do
        echo ${envList[$i]}
    done
    return
}
setEnv(){
    if [ -z "$1" ]; then
        usage; return
    fi

    case $1 in
        cold)       FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_cold.sh;;
        coles)      FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_coles.sh;;
        fc)         FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_fc.sh;;
        fcrm)       FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_fcrm.sh;;
        stable)     FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_stable.sh;;
        tip)        FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_tip.sh;;
        uat)        FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_uat.sh;;
        wellmdc)    FILE_TO_SOURCE=/u2/tip/conf/ctrl/lettus_wellmdc.sh;;
        *)          usage; return;;
    esac

    if $IS_SOURCED; then
        echo "Environment \"$1\" selected."
        echo "Now sourcing file \"$FILE_TO_SOURCE\"..."
        . ${FILE_TO_SOURCE}
        return
    else
        return 1
    fi
}

main(){
    if [ -z "$1" ]; then
        showEnv; return
    fi

    case $1 in
        -h) usage;;
         *) setEnv $1;;
    esac
    return
}

PROG="chenv"

# create array of user-selectable environments
envList=( cold coles fc fcrm stable tip uat wellmdc )

main "$@"
return

If I could, I'd like to get some feedback on a better way to accomplish any of the following:

  1. run through the case statement
  2. make script trivally simple to maintain/upgrade/update
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

In showEnv, why do you need to check that envList is empty when you define it right in the program?

I'd write the array elements one-per-line with:

# Show all elements in array
for elem in "${envList[@]}"; do 
    echo "$elem"
done

or, more succinctly

printf "%s\n" "${envList[@]}"

In setEnv:

case $1 in
    cold|coles|fc|fcrm|stable|tip|uat|wellmdc)
        FILE_TO_SOURCE="/u2/tip/conf/ctrl/lettus_$1.sh" ;;
    *)  usage; return;;
esac

or, without duplicating the contents of envList:

FILE_TO_SOURCE=""
for elem in "${envList[@]}"; do
    if [ "$elem" = "$1" ]; then
        FILE_TO_SOURCE="/u2/tip/conf/ctrl/lettus_$1.sh"
        break
    fi
done
if [ -z "$FILE_TO_SOURCE" ];then 
    usage
    return
fi

You might want to check that the relevant file exists

if [ ! -f "$FILE_TO_SOURCE" ]; then
    echo "missing config file '$FILE_TO_SOURCE'!"
    echo "contact $maintainer for assistance"
    return 1
fi

What is $IS_SOURCED? You might want to document how that environment variable is defined.

You don't need the return at the end of main or at the end of the program.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.