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

标签 arrays linux bash function

  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/

相关文章:

javascript - 如果电子邮件在 Gmail 的主题或正文中包含特定字词,则使用脚本对其进行标记

c - sprintf 将十六进制数组转换为十进制字符数组只读取第一个字节

java - 错误 : Could not find or load main class

linux - 检查套接字是否在 bash 中关闭?

linux - 在终端/Linux 上查找包含特定日期范围文件名的所有文件

java - 如何将 byte[] 转换为 Byte[] 和其他方式?

c# - 从字符串中的特定字符创建数组

c++ - Centos 7 带有自定义构建的 GLFW binary/usr/bin/ld :/usr/local/lib/libglfw3. a(init.c.o): `.text' 部分中无法识别的重定位 (0x2a)

linux - 关于 docker 守护进程在自定义 CIDR block 中生成容器

linux - 想要在使用shell脚本时动态增加变量