Reputation: 12596
Here is my situation. I have an array of Device id's. I know which device id's go with which ip addresses. I want to use the device id's to find the correct ip address and pass both of them in a command. I am using a shell script to do this.
Here is some psuedo code for what I want.
This is my dictionary type thing matching id's with ip's, no particular order
dictionary_thing = ( id:ip
id:ip
id:ip
id:ip )
This is my array of id that I am using in no particular order
array_of_used_ids = ( id
id
id )
Maybe make an array of objects with two properties (id and ip) for each id found
array_object
for(int i=0; i<count; i++)
{
array_object[i]= new object and set id to dictionary_thing id and ip
}
then I want to run all the commands in a for loop (or whatever you suggest)
for id in array_of_used_ids
./run_this_command with
for(int i=0; i<count; i++)
{
./command array_object[i].ip array_object[i].id
}
Upvotes: 1
Views: 498
Reputation: 28336
You already have your primary arrays built, so build a regex from the used IDs
regex="^${array_of_used_ids[0]}:"
i=1
while [ $i -lt ${#array_of_used_ids[*]} ]; do
regex="$regex|^${array_of_used_ids[$i]}:"
i=$(( $i + 1))
done
This should give you a regex like "^id:|^id:|^id:" Then iterate your dictionary checking each against the regex, when matched, replace the separating ':' with a space
array_object=()
i=0
j=0
while [ $i -lt ${#dictionary_thing[*]} ]; do
if [[ $dictionary_thing[$i] =~ $regex ]]; then
array_object[$j] = ${dictionary_thing[$i]/:/ } #if it is possible for an ID to contain a ':', you will need a different method for splitting here
j=$(( $j + 1 ))
fi
i=$(( $i + 1 ))
done
Finally, iterate your result object and execute the command
i=0
while [ $i -lt ${#array_object[*]} ]; do
command_to_execute ${array_object[$i]}
i=$(( $i + 1 ))
done
Upvotes: 2