HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Bash arrays and case statements - review my script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
casescriptarraysstatementsandreviewbash

Problem

```
#!/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 "$

Solution

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.

Code Snippets

# Show all elements in array
for elem in "${envList[@]}"; do 
    echo "$elem"
done
printf "%s\n" "${envList[@]}"
case $1 in
    cold|coles|fc|fcrm|stable|tip|uat|wellmdc)
        FILE_TO_SOURCE="/u2/tip/conf/ctrl/lettus_$1.sh" ;;
    *)  usage; return;;
esac
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
if [ ! -f "$FILE_TO_SOURCE" ]; then
    echo "missing config file '$FILE_TO_SOURCE'!"
    echo "contact $maintainer for assistance"
    return 1
fi

Context

StackExchange Code Review Q#12520, answer score: 5

Revisions (0)

No revisions yet.