Note that there are some explanatory texts on larger screens.

plurals
  1. PObash - compare string against keys of an associative array
    primarykey
    data
    text
    <p>Consider the following bash code:</p> <pre><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/' &lt;&lt;&lt; ${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 </code></pre> <p><code>$VERS_INPUT</code> should equate to one of the keys in the associative array <code>$VERS</code>. How can I verify that the value of <code>$VERS_INPUT</code> equals any of the keys of <code>$VERS</code>. 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 <code>${ASSET_DIR}/enterprise</code>. 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.</p> <hr> <p><strong>EDIT:</strong></p> <p>To give an example, lets say the first for loop dynamically generated the associative array <code>$VERS</code> with four values:</p> <pre><code>$ echo $ENV["1"] 3.4.3 $ echo $ENV["2"] 3.4.4 $ echo $ENV["3"] 3.4.5 $ echo $ENV["4"] 3.4.6 </code></pre> <p>The user is prompted to enter a value (assigned to variable <code>$VERS_INPUT</code>) that should hopefully be 1, 2, 3, or 4. How can I check that the value of <code>$VERS_INPUT</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload