$1 和 $2 变量 shell 脚本递归是接收相同的函数
$1 and $2 variable shell script recursive is function is received as same
我写了一个递归函数来遍历目录,当达到指定的深度时应该退出迭代。入口目录和最大深度值都作为参数传递给函数。
perform_fragmented_copy() {
echo -e "perform_fragmented_copy: at depth "
while [ "" ] && [ "" -le $n_max_depth ];
do
declare -i n_entry_size=$(du -sb | awk '{print }')
if [ $sz_remainig_destination -gt $n_entry_size ]
then
echo -e "perform_fragmented_copy: can do the copy";
else
echo -e "perform_fragmented_copy: cannot, iterating again";
declare -i n_curr_depth=$((+1))
perform_fragmented_copy ""/* $n_curr_depth
fi
shift
done
echo -e "perform_fragmented_copy: exiting: at depth "
}
调用为
sz_remainig_destination=10000
n_max_depth=5
perform_fragmented_copy 0
输出为:
perform_fragmented_copy: /home/ROSS-82/ at depth 0
perform_fragmented_copy: cannot, iterating again
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: /home/ROSS-82//ross_reader-82: integer expression expected
perform_fragmented_copy: exiting: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: : integer expression expected
perform_fragmented_copy: exiting: 0 at depth
在第一个递归的输出中 </code> 被接收为整数 0</p>
<pre><code>perform_fragmented_copy: /home/ROSS-82/ at depth 0
在第二次递归的输出中 </code> 被接收为 <code>
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
我已经尝试了很多方法来解决这个问题,并搜索了解决方案,但还没有结果。
您应该将深度移至第一个参数。然后你可以将它从循环之前的参数列表中移出,循环用 </code> 和 <code>shift
.
处理剩余的参数
perform_fragmented_copy() {
depth=
shift
echo -e "perform_fragmented_copy: $@ at depth $depth"
if [ "$depth" -ge "$n_max_depth" ]
then return
fi
declare -i n_curr_depth=$(($depth+1))
while [ $# -gt 0 ];
do
declare -i n_entry_size=$(du -sb "" | awk '{print }')
if [ $sz_remainig_destination -gt $n_entry_size ]
then
echo -e "perform_fragmented_copy: can do the copy";
else
echo -e "perform_fragmented_copy: cannot, iterating again";
perform_fragmented_copy $n_curr_depth ""/*
fi
shift
done
echo -e "perform_fragmented_copy: exiting: at depth $depth"
}
你不能在最后的exiting
消息中引用</code>,因为当所有参数都被移走时循环结束。当 <code>
为空时循环结束。
我写了一个递归函数来遍历目录,当达到指定的深度时应该退出迭代。入口目录和最大深度值都作为参数传递给函数。
perform_fragmented_copy() {
echo -e "perform_fragmented_copy: at depth "
while [ "" ] && [ "" -le $n_max_depth ];
do
declare -i n_entry_size=$(du -sb | awk '{print }')
if [ $sz_remainig_destination -gt $n_entry_size ]
then
echo -e "perform_fragmented_copy: can do the copy";
else
echo -e "perform_fragmented_copy: cannot, iterating again";
declare -i n_curr_depth=$((+1))
perform_fragmented_copy ""/* $n_curr_depth
fi
shift
done
echo -e "perform_fragmented_copy: exiting: at depth "
}
调用为
sz_remainig_destination=10000
n_max_depth=5
perform_fragmented_copy 0
输出为:
perform_fragmented_copy: /home/ROSS-82/ at depth 0
perform_fragmented_copy: cannot, iterating again
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: /home/ROSS-82//ross_reader-82: integer expression expected
perform_fragmented_copy: exiting: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
./ftest.sh: line 69: [: : integer expression expected
perform_fragmented_copy: exiting: 0 at depth
在第一个递归的输出中 </code> 被接收为整数 0</p>
<pre><code>perform_fragmented_copy: /home/ROSS-82/ at depth 0
在第二次递归的输出中 </code> 被接收为 <code>
perform_fragmented_copy: /home/ROSS-82//82 at depth /home/ROSS-82//ross_reader-82
我已经尝试了很多方法来解决这个问题,并搜索了解决方案,但还没有结果。
您应该将深度移至第一个参数。然后你可以将它从循环之前的参数列表中移出,循环用 </code> 和 <code>shift
.
perform_fragmented_copy() {
depth=
shift
echo -e "perform_fragmented_copy: $@ at depth $depth"
if [ "$depth" -ge "$n_max_depth" ]
then return
fi
declare -i n_curr_depth=$(($depth+1))
while [ $# -gt 0 ];
do
declare -i n_entry_size=$(du -sb "" | awk '{print }')
if [ $sz_remainig_destination -gt $n_entry_size ]
then
echo -e "perform_fragmented_copy: can do the copy";
else
echo -e "perform_fragmented_copy: cannot, iterating again";
perform_fragmented_copy $n_curr_depth ""/*
fi
shift
done
echo -e "perform_fragmented_copy: exiting: at depth $depth"
}
你不能在最后的exiting
消息中引用</code>,因为当所有参数都被移走时循环结束。当 <code>
为空时循环结束。