gpt4 book ai didi

arrays - bash:如何删除函数内的列表条目?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:58 25 4
gpt4 key购买 nike

  1. 如何从函数列表中删除条目?
  2. 为什么 [见鬼] 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com