Before we get into the question, I know there are answers SIMILAR to this on stack overflow already. However this one is unique in it's use of the eval statement with associative arrays. ( Believe me I've read them all ).
Okay now into the question
I have X number of arrays defined via an eval function similar to this:
for (( i=1;i<=X;i++ ))
do
eval "declare -gA old$i"
eval "old$i[key]=value"
done
This code is in function : makeArrays
Now I have a second function that must loop through these different arrays
old1
old2
.
.
.
oldX
I'll call this function : useArrays
Now, I have a for loop for this useArrays function.
for (( i=0;i<$#;i++ ))
do
// ACCESS OLD1[KEY]
done
My question is, how do I access this array FOR COMPARISONS.
I.E.
if[ old1 -eq 0 ]
then
...
fi
Is there a way I could COPY these associate arrays into a variable I can use for comparisons using eval as little as possible?
Modern versions of bash support namerefs (originally a ksh feature), so you can point a constant name at any variable you choose; this makes eval unnecessary for the purposes to which you're presently placing it.
key="the key you want to test"
for (( i=0;i<$#;i++ )); do
declare -n "oldArray=old$i" # map the name oldArray to old0/old1/old2/...
printf 'The value of %q in old%q is: %q\n' "$key" "$i" "${oldArray[$key]}"
unset -n "oldArray" # remove that mapping
done
You could of course refer to "${!oldArray[#]}" to iterate over its keys; also map a newArray namevar to compare with; etc.
Related
What I have is an array with some variables. I can iterate to get the values of those vars but what I need is actually their names (values will be used elsewhere).
Going with var[i] won't work cause I will have different names. I guess I could workaround this by creating another array with the names - something similar to this:
Getting variable values from variable names listed in array in Bash
But I'm wondering if there is a better way to do this.
var1=$'1'
var2=$'2'
var3=$'3'
Array=( $var1 $var2 $var3)
for ((i=0; i<${#Array[#]}; i++))
do
echo ${Array[i]}
done
Is:
>1
>2
>3
Should be:
>var1
>var2
>var3
It sounds like you want an associative array.
# to set values over time
declare -A Array=( ) || { echo "ERROR: Need bash 4.0 or newer" >&2; exit 1; }
Array[var1]=1
Array[var2]=2
Array[var3]=3
This can also be assigned at once:
# or as just one assignment
declare -A Array=( [var1]=1 [var2]=2 [var3]=3 )
Either way, one can iterate over the keys with "${!Array[#]}", and retrieve the value for a key with ${Array[key]}:
for var in "${!Array[#]}"; do
val="${Array[$var]}"
echo "$var -> $val"
done
...will, after either of the assignments up top, properly emit:
var1 -> 1
var2 -> 2
var3 -> 3
What about this solution?
#!/bin/bash
var1=$'1'
var2=$'2'
var3=$'3'
Array=( var1 var2 var3 )
for var in "${Array[#]}"; do
echo "$var = ${!var}"
done
The idea just consists in putting your variable names in the array, then relying on the indirection feature of Bash.
But as pointed out by #CharlesDuffy, the use of associative arrays sounds better adapted to the OP's use case.
Also, this related article may be worth reading: How can I use variable variables… or associative arrays?
I have a bash associative array that looks like this
declare -A arraySalary=( [1]=1000 [8]=3000 [2]=2000)
I am learning bash scripting and am trying to implement a bubble sort on the array
with this piece of code
sortedDesc=false
while ! $sortedDesc ;
do
sortedDesc=true
for ((currentIndex=0; currentIndex<$((${#arraySalary[#]} -1)); currentIndex++))
do
if [[ ${arraySalary[$((currentIndex))]} -lt ${arraySalary[$((currentIndex + 1))]} ]]
then
sortedDesc=false
biggerNumber=${arraySalary[((currentIndex - 1))]}
arraySalary[$((currentIndex + 1))]=${arraySalary[$((currentIndex))]}
arraySalary[currentIndex]=${biggerNumber}
echo "swapped"
fi
done
done
echo "Printing new values"
# Print new values
for key in "${!arraySalary[#]}";
do
echo $key "->" ${arraySalary[$key]}
done
but the output I get is
swapped
swapped
Printing new values
0 -> 2000
1 -> 1000
2 ->
Can someone please explain why this is? Thanks
I don't know why you have chosen to use an associative array instead of an ordinary array, but you need to be aware that the index of an associative array is not a numeric context.
If arraySalary had been declared with -a instead of -A, then this would have been fine:
arraySalary[currentIndex]=${biggerNumber}
because the index of an ordinaey array is a numeric context, and in numeric contexts you can use variabke names without $. But since it is associative, what that does is set the element whose key is the string currentIndex. What you want is:
arraySalary[$currentIndex]=${biggerNumber}
It is not necessary to write $((currentIndex)); bash does not distinguish between integers and strings which contain the integer.
Sorting an array just by its value will be easy. In the case that you sort keys
by its value by using an assoc array, you need to introduce another array to hold the order of the elements because an assoc array is orderless.
Let's name the new array index which holds (1 8 2 ..) then the script will look like:
#!/bin/bash
declare -A arraySalary=([1]=1000 [8]=3000 [2]=2000)
declare -a index=("${!arraySalary[#]}")
sortedDesc=false
while ! $sortedDesc ;
do
sortedDesc=true
for ((i=0; i<$((${#index[#]} - 1)); i++))
do
if [[ ${arraySalary[${index[$i]}]} -lt ${arraySalary[${index[$(($i + 1))]}]} ]]
then
sortedDesc=false
biggerIndex=${index[$(($i + 1))]}
index[$(($i + 1))]=${index[$i]}
index[$i]=$biggerIndex
echo "swapped"
fi
done
done
echo "Printing new values"
# Print new values
for key in "${index[#]}";
do
echo $key "->" ${arraySalary[$key]}
done
which yields
swapped
swapped
swapped
Printing new values
8 -> 3000
2 -> 2000
1 -> 1000
function generateFileList {
for entry in "$ORIGINATION_PATH"/*
do
entry=${entry%.*} # retain the part before the dot
entry=${entry##*/} # retain the part after the last slash
if [ $(contains "${FILENAME[#]}" $entry) == "n" ]; then
FILENAME[$fn_counter]=$entry
fn_counter=(expr $fn_counter + 1)
echo $entry "added to filelist"
echo ${FILENAME[$fn_counter]}
fi
done
NUMBER_OF_FILES=$(expr ${#FILENAME[#]} + 1)}
I have this function .My $ORIGINATION_PATH has many files in it. However, when I call this function my $FILENAME array gets populated only with one entry.Why? Inside the function everything seems fine, and it seems that $FILENAME array gets all the values it needs to get, but when I check outside the function I only get one value in the $FILENAME aray
Problems with your code and suggestions for improvement:
You should initialize ${FILENAME[#]} to an empty array (either in the function itself if you always want the function to generate a new list of files from scratch, or before calling the function if you want to be able to build up a composite list of files by calling the function repeatedly on different base directories).
You should initialize $fn_counter to zero before starting the loop. Or, for the composite build-up idea, to the number of elements currently in ${FILENAME[#]}. Actually, another, perhaps preferable solution, would be to remove the $fn_counter variable entirely and replace it with ${#FILENAME[#]}, since it should always be equal to that value.
In the line fn_counter=(expr $fn_counter + 1), you're assigning $fn_counter to an array, rather than incrementing it. This is because you forgot the dollar before the open parenthesis. If you ran fn_counter=$(expr $fn_counter + 1) then it would work. But there's a better way to increment a numeric variable: let ++fn_counter.
You don't have to dollar-prefix variables in arithmetic expressions. So, for example, we can say ${FILENAME[fn_counter]} instead of ${FILENAME[$fn_counter]}.
You're trying to echo the element of ${FILENAME[#]} that was just added in the current iteration, but indexing it with $fn_counter after it was incremented, which is incorrect. You can solve this by subtracting 1 from it, i.e. echo "${FILENAME[fn_counter-1]}". Or, if removing $fn_counter, echo "${FILENAME[${#FILENAME[#]}-1]}".
When assigning $NUMBER_OF_FILES, I don't know why you're adding 1 to ${#FILENAME[#]}. The number of elements in the ${FILENAME[#]} array should be equal to the number of files, without requiring an increment, no? I recommend removing this variable entirely, since the value can be accessed directly as ${#FILENAME[#]}.
I recommend you pass inputs as arguments (e.g. pass $ORIGINATION_PATH as an argument) and use the local keyword to reduce the likelihood of variable clashes between functions. Globals are the default in bash, which creates dangerous possibilities for different functions to step on each others' toes. For example, imagine if the contains function (assuming it's a shell function) assigned a value to the global $entry variable.
I recommend always using the [[ command rather than [, as it's more powerful, and it's good to be consistent.
As written, your script won't work correctly on an empty directory. You could test in advance if the directory is empty (e.g. [[ -n "$(find "$ORIGINATION_PATH" -maxdepth 0 -empty)" ]]). Another solution is to set nullglob. Another solution is to skip glob words that don't actually exist (e.g. if [[ ! -e "$entry" ]]; then continue; fi;).
Always double-quote variable expansions to protect against word splitting, which takes place after variable expansion. For example, the contains call should be contains "${FILENAME[#]}" "$entry" (notice the double-quoting around $entry). The only exceptions are (1) when assigning a string variable to a string variable, i.e. new=$old, in which case you don't have to quote it, and (2) when expanding a numeric variable, which is guaranteed not to be corrupted by word splitting.
Here's a working solution, filling in the missing pieces:
function contains {
local target="${#:$#:1}";
local -a array=("${#:1:$#-1}");
local elem='';
for elem in "${array[#]}"; do
if [[ "$elem" == "$target" ]]; then
echo 'y';
return;
fi;
done;
echo 'n';
} ## end contains()
function generateFileList {
local path="$1";
local entry='';
for entry in "$path"/*; do
if [[ ! -e "$entry" ]]; then continue; fi;
entry=${entry%.*}; ## retain the part before the dot
entry=${entry##*/}; ## retain the part after the last slash
if [[ "$(contains "${FILENAME[#]}" "$entry")" == 'n' ]]; then
FILENAME[${#FILENAME[#]}]=$entry;
echo "$entry added to filelist";
echo "${FILENAME[${#FILENAME[#]}-1]}";
fi;
done;
} ## end generateFileList()
ORIGINATION_PATH='...';
FILENAME=(); ## build up result on global ${FILENAME[#]} var
generateFileList "$ORIGINATION_PATH";
echo "\${#FILENAME[#]} == ${#FILENAME[#]}";
echo "\${FILENAME[#]} == (${FILENAME[#]})";
I have an array, index is the hard drive size and value is number of hard drives having same size. So this is what i do.
DRIVE_SIZES[$DRIVE_SIZE]=`expr ${DRIVE_SIZES[$DRIVE_SIZE]} + 1`
I have not initialized the DRIVE_SIZES array to 0. So above line might not work.
I would like to initialize a sparse array in bash script.
Lets say all drives in the host are of the same size, except one. Some 10 drives are of size 468851544 and one drive is of size 268851544. So I cannot initialize all index from 0-468851544 because I dont know the maximum disk size beforehand.
So is there a way to initialize such an sparse array to 0. May be if there is way to declare an integer array in bash that might help. But after some initial research found out I can declare an integer, but not integer array(might be wrong on this). Can someone help me with this ?
I read this, but this might not be the solution to me
Use increment in an arithmetic expression:
(( ++DRIVE_SIZES[DRIVE_SIZE] ))
You can use parameter substitution to put a zero into the expression when the array key has not been defined yet:
DRIVE_SIZES[$DRIVE_SIZE]=`expr ${DRIVE_SIZES[$DRIVE_SIZE]:-0} + 1`
N.B. this is untested, but should be possible.
An array element when unset and used in arithmetic expressions inside (( )) and $(( )) has a default value of 0 so an expression like this would work:
(( DRIVE_SIZES[DRIVE_SIZE] = DRIVE_SIZES[DRIVE_SIZE] + 1 ))
Or
(( DRIVE_SIZES[DRIVE_SIZE] += 1 ))
Or
(( ++DRIVE_SIZES[DRIVE_SIZE] ))
However when used outside (( )) or $(( )), it would still expand to an empty message:
echo ${DRIVE_SIZES[RANDOM]} shows "" if RANDOM turns out to be an index of an element that is unset.
You can however use $(( )) always to get the proper presentation:
echo "$(( DRIVE_SIZES[RANDOM] ))" would return either 0 or the value of an existing element, but not an empty string.
Using -i to declare, typeset or local when declaring arrays or simple variables might also help since it would only allow those parameters to have integral values, and the assignment is always done as if it's being assigned inside (( )) or $(( )).
declare -i VAR
A='1 + 2'
is the same as (( A = 1 + 2 )).
And with (( B = 1 )), A='B + 1' sets A to 2.
For arrays, you can set them as integral types with -a:
declare -a -i ARRAYVAR
So a solution is to just use an empty array variable and that would be enough:
ARRAYVAR=()
Or
declare -a -i ARRAYVAR=()
Just make sure you always use it inside (( )) or $(( )).
I've got a variable list of associative arrays that I want to iterate through and retrieve their key/value pairs.
I iterate through a single associative array by listing all its keys and getting the values, ie.
for key in "${!queue1[#]}" do
echo "key : $key"
echo "value : ${queue1[$key]}"
done
The tricky part is that the names of the associative arrays are variable variables, e.g. given count = 5, the associative arrays would be named queue1, queue2, queue3, queue4, queue5.
I'm trying to replace the sequence above based on a count, but so far every combination of parentheses and eval has not yielded much more then bad substitution errors. e.g below:
for count in {1,2,3,4,5} do
for key in "${!queue${count}[#]}" do
echo "key : $key"
echo "value : ${queue${count}[$key]}"
done
done
Help would be very much appreciated!
The difficulty here stems from the fact that the syntax for indirect expansion (${!nameref}) clashes with the syntax for extracting keys from an associative arrays (${!array[#]}). We can have only one or the other, not both.
Wary as I am about using eval, I cannot see a way around using it to extract the keys of an indirectly referenced associative array:
keyref="queue${count}[#]"
for key in $(eval echo '${!'$keyref'}'); do ... ; done
You can however avoid eval and use indirect expansion when extracting a value from an array given the key. Do note that the [key] suffix must be part of the expansion:
valref="queue${count}[$key]"
echo ${!valref}
To put this in context:
for count in {1..5} ; do
keyref="queue${count}[#]"
for key in $(eval echo '${!'$keyref'}'); do
valref="queue${count}[$key]"
echo "key = $key"
echo "value = ${!valref}"
done
done
I was able to make it work with the following script:
for count in {1..5} ; do
for key in $(eval echo '${!q'$count'[#]}') ; do
eval echo '${q'$count"[$key]}"
done
done
Note it breaks if any key contained a space. If you want to deal with complex data structures, use a more powerful language like Perl.
I think this might work (but untested). The key is to treat the indexing
as the full name of a variable. (That is, the array queue5 can be
treated as a sequence of variables named queue5[this], queue5[that], etc.)
for count in {1,2,3,4,5} do
assoc="queue$count[#]"
for key in "${!assoc}" do
echo "key : $key"
val="queue$count[$key]"
echo "value : ${!val}"
done
done