Add 'bash/associative_array.bash'
Associative Arrays and how to iterate over them
This commit is contained in:
19
bash/associative_array.bash
Normal file
19
bash/associative_array.bash
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# associative_array.bash. Derived from:
|
||||||
|
# https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash
|
||||||
|
|
||||||
|
# Make an array and stuff it full of values
|
||||||
|
declare -A array
|
||||||
|
array[repo_one]=true
|
||||||
|
array[repo_two]=true
|
||||||
|
array[repo_three]=false
|
||||||
|
|
||||||
|
# The key is accessed using the exclamation point
|
||||||
|
for i in "${!array[@]}"
|
||||||
|
do
|
||||||
|
echo "key : $i"
|
||||||
|
if ${array[$i]}; then
|
||||||
|
echo "$i is true"
|
||||||
|
fi
|
||||||
|
done
|
||||||
Reference in New Issue
Block a user