push_list
为什么[地狱] pop_list
按预期工作
不是吗?#!/bin/bash
declare -a the_list=()
push_list() {
the_list[${#the_list[@]}]="`echo $@`"
}
pop_list() {
local -i n=${#the_list[@]}
(( n > 0 )) || return
let n-=1
echo ${the_list[$n]}
unset the_list[$n]
}
cleanup() {
echo Cleanup...
local x=$(pop_list)
while [ -n "$x" ]; do
echo "/$x/"
x=$(pop_list)
done
echo ...cleaned.
}
trap cleanup EXIT
echo Start.
push_list aaa bbb ccc
push_list qqq www eee
push_list mmm nnn bbb
declare -p the_list
echo End.
# EOF #
答案 0 :(得分:2)
您正在cleanup()
中产生一个流程:
$(pop_list)
我认为它会从孩子名单的副本中弹出。
答案 1 :(得分:0)
正如经常发生的那样,我找到了一个问题的答案:
#!/bin/bash
declare -a the_list=()
push_list() {
the_list[${#the_list[@]}]="`echo $@`"
}
pop_list() {
local -i n=${#the_list[@]}
(( n > 0 )) || return
let n-=1
echo ${the_list[$n]}
unset the_list[$n]
}
cleanup() {
echo Cleanup...
# local x=$(pop_list)
# while [ -n "$x" ]; do
# echo "/$x/"
# x=$(pop_list)
# done
while (( ${#the_list[@]} > 0 )); do
local -i i=${#the_list[@]}
let i-=1
local x=${the_list[$i]}
echo "/$x/"
unset the_list[$i]
done
echo ...cleaned.
}
trap cleanup EXIT
echo Start.
push_list aaa bbb ccc
push_list qqq www eee
push_list mmm nnn bbb
declare -p the_list
echo End.
# EOF #
这意味着嵌套调用和/或陷阱中的变量作用域发生了奇怪的事情......