- 如何从函数列表中删除条目?
- 为什么 [见鬼]
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 #
最佳答案
您正在 cleanup()
中生成一个进程:
$(pop_list)
我认为它是从子列表的副本中弹出的。
关于arrays - bash:如何删除函数内的列表条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46665234/