I need to create a certain loop which will to some extend describe a network with all its nodes. This loop will be part of something else which is not relevant to the topic. As you know in a network one node is connected to another and so on. I have all these nodes in separate files and I need to process them with a loop. Each loop will produce more results within which i need to perform more loops and the number increases exponentially until breaking point is reach. Now i know how to solve this problem with like 50 nested loop's which look like this:
declare loopvarnode=($(some command to get list of nodes))
for a in ${loopvarnode[@]}
do
declare check1=($(grep ${a[@]} results.file))
if [[ "$a" == "breaking point" ]]
then echo "Match found"; break
elif [[ ! $check1 -z ]]
then continue
fi
echo ${a[@]} >> results.file
declare loopvarnode1=($(same thing but now we look for results in what was provided by variable $a))
for b in ${loopvarnode1[@]}
do
declare check2=($(grep ${b[@]} results.file))
if [[ "$b" == "breaking point" ]]
then echo "Match found"; break
elif [[ ! $check1 -z ]]
then continue
fi
echo ${b[@]} >> results.file
declare loopvarnode2=($(same thing but now we look for results in what was provided by variable $b))
for c in ${loopvarnode2[@]}
do
.....
After about 50 of these I suppose i should be fine but maybe there's a way to do this properly with one or two loops.
You can use recursive function instead of copy-paste the same loop:
//$1 is first list parameter.
function do_things {
for a in ${$1[@]}
do
declare check1=($(grep ${a[@]} results.file))
if [[ "$a" == "breaking point" ]]
then echo "Match found"; break
elif [[ ! $check1 -z ]]
then continue
fi
echo ${a[@]} >> results.file
declare loopvarnode1=($(same thing but now we look for results in what was provided by variable $a))
do_things loopvarnode1
done
}
Something like that.