如何在bash中用一个循环处理多级结果?
How to process multiple levels of results with a single loop in bash?
我需要创建一个特定的循环,该循环将在一定程度上描述具有所有节点的网络。这个循环将是与主题无关的其他内容的一部分。如您所知,在网络中,一个节点连接到另一个节点,依此类推。我将所有这些节点放在单独的文件中,我需要用一个循环来处理它们。每个循环都会产生更多的结果,我需要在其中执行更多的循环,并且数量呈指数增长,直到达到断点。现在我知道如何用 50 个嵌套循环来解决这个问题了:
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
.....
在大约 50 个之后,我想我应该没问题,但也许有一种方法可以通过一个或两个循环正确地做到这一点。
您可以使用递归函数而不是复制粘贴相同的循环:
// is first list parameter.
function do_things {
for a in ${[@]}
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
}
类似的东西。
另外,为了更正您的函数,将数组作为参数传递将无法直接工作(我得到了错误的替换)。它是这样工作的:
function do_things {
array=( "$@" )
for a in ${array[@]}
do
declare check1=($(grep ${a[@]} results.file))
if [[ "$a" == "breaking point" ]];
then echo "Match found"; break
elif [[ ! -z $check1 ]];
then continue
fi
echo ${a[@]}
x=$(( $x + 1 ))
declare loopvarnode1=($(some stuff with $a))
do_things ${loopvarnode1[@]}
done }
我需要创建一个特定的循环,该循环将在一定程度上描述具有所有节点的网络。这个循环将是与主题无关的其他内容的一部分。如您所知,在网络中,一个节点连接到另一个节点,依此类推。我将所有这些节点放在单独的文件中,我需要用一个循环来处理它们。每个循环都会产生更多的结果,我需要在其中执行更多的循环,并且数量呈指数增长,直到达到断点。现在我知道如何用 50 个嵌套循环来解决这个问题了:
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
.....
在大约 50 个之后,我想我应该没问题,但也许有一种方法可以通过一个或两个循环正确地做到这一点。
您可以使用递归函数而不是复制粘贴相同的循环:
// is first list parameter.
function do_things {
for a in ${[@]}
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
}
类似的东西。
另外,为了更正您的函数,将数组作为参数传递将无法直接工作(我得到了错误的替换)。它是这样工作的:
function do_things {
array=( "$@" )
for a in ${array[@]}
do
declare check1=($(grep ${a[@]} results.file))
if [[ "$a" == "breaking point" ]];
then echo "Match found"; break
elif [[ ! -z $check1 ]];
then continue
fi
echo ${a[@]}
x=$(( $x + 1 ))
declare loopvarnode1=($(some stuff with $a))
do_things ${loopvarnode1[@]}
done }