Consider the following bash code:
echo -e "\nThe following versions are available for install:"
declare -A VERS
CNT=0
for FILE in `ls ${ASSET_DIR}/enterprise`; do
let "CNT++"
VERS_STR=`sed -e 's/^file-\([0-9].[0-9].[0-9]\).zip/\1/' <<< ${FILE}`
VERS[${CNT}]=${VER_STR}
done
for i in ${!VERS[@]}; do
echo " ${i} - ${VERS[${i}]}"
done
echo -n "Which version do you want to install?: "
read VERS_INPUT
$VERS_INPUT should equate to one of the keys in the associative array $VERS. How can I verify that the value of $VERS_INPUT equals any of the keys of $VERS. As it stands right now, with our current setup, there's 6 available options generated from the for loop(s), but this has potential to grow, thus I don't want to make it a static check and have to change the script every time an additional file gets put in ${ASSET_DIR}/enterprise. I presume an if statement is in order, but I'm not certain how I would check that it matches a key from the associative array.
EDIT:
To give an example, lets say the first for loop dynamically generated the associative array $VERS with four values:
$ echo $ENV["1"]
3.4.3
$ echo $ENV["2"]
3.4.4
$ echo $ENV["3"]
3.4.5
$ echo $ENV["4"]
3.4.6
The user is prompted to enter a value (assigned to variable $VERS_INPUT) that should hopefully be 1, 2, 3, or 4. How can I check that the value of $VERS_INPUT is either 1, 2, 3, or 4. Obviously this is an arbitrary example, since my list is currently at 6 available options, and is only going to grow further. I want to make this as dynamic as possible, since 1, 2, 3, 4, etc... is dynamically generated from the first for loop and is not manually generated.
lsif you can avoid it. Whitespace is our enemy. Whitespace in filenames is a massive enemy.for FILE in ${ASSET_DIR}/enterprise/*; doshould do it. Take care on hidden files.