在bash shell 函数传递数组的问题2

在bash shell 函数传递数组的问题2

引入 nameref 特性,bash shell 版本 较新的版本 ,可以使用 nameref 来解决 数组传递的问题。

在函数中如何 传递 传递数组变量

bash 复制代码
# !/bin/bash
# array variable to function test
# 
function testit {
   # 定义变量 
   local newarray
   # 重新构建一个新的数组 
   # newarray=(`echo "$@"`)
   newarray=("$@")
   echo "The new array value is: ${newarray[*]}"
}

myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
# 注意这里传递方式 是把整个数组进行传递
testit ${myarray[*]}
testit "${myarray[@]}"

这种方式 就是把整个数组全部进入传入, 在函数内部重新构建 这个数组。 缺点也很明显,如果我需要传递两个数组呢? 如果想传递两个变量,有数组和其他的变量呢?

如果你的shell 版本 大于等于5.0 ,可以支持 nameref 变量引用

bash 复制代码
# 查看 bash  version
bash --version  |head -n1 
GNU bash, version 5.2.37(1)-release (x86_64-apple-darwin22.6.0)

nameref 特性是在 Bash 4.3 版本中引入的。这个特性允许你创建一个变量作为另一个变量的引用,从而可以通过该引用变量来间接访问和修改目标变量的值。

bash 复制代码
#!/bin/bash

function testit {
   # 接受 数组名 需要使用 -n 这个选项 
   local -n thisarray="${1}"  # 传递给函数的是数组名,所以使用 -n 参数,nameref 变量引用 
   echo "The received array is ${thisarray[*]}"
}

myarray=(1 2 3 4 5)
echo "The original array is: ${myarray[*]}"
# 传递给函数的是数组名,所以使用 -n 参数,nameref 变量引用 
# $myarray 只会取数组第一个元素的值,也就是 1  是不对的....
testit myarray

真实的样例

我希望删除 不同命名空间的 相同的secret , 这个命名空间和secrets 就是两个不同的数组,如果每一个都重复写,脚本就需要 写 M* N 个命令, 所以我想通过数组的形式 传入的function 中 来执行删除操作。

bash 复制代码
#!/bin/bash
# description: 删除指定命名空间下的指定 Secret, 需要Bash 4.3 及以上版本中才引入的 nameref 特性
# bash --version  |head -n1 
# GNU bash, version 5.2.37(1)-release (x86_64-apple-darwin22.6.0)
# author: frank
# date: 2025-07-03
# usage:  bash delete_secret_senior.sh


# 函数定义:批量删除多个命名空间中的 Secrets
delete_secrets_in_namespaces() {
    local -n secrets_array=$1     # 使用 nameref 引用传入的 Secret 数组
    local -n namespaces_array=$2  # 使用 nameref 引用传入的 Namespace 数组

    for ns in "${namespaces_array[@]}"; do
        echo "Processing namespace: ${ns}"

        for secret in "${secrets_array[@]}"; do
            if kubectl get secret "${secret}" -n "${ns}" &> /dev/null; then
                echo "Deleting secret '${secret}' in namespace '${ns}'..."
                # debug,not execute true command
                # kubectl delete secret "${secret}" -n "${ns}"
                if [ $? -eq 0 ]; then
                    echo "✅ Secret '${secret}' deleted successfully from namespace '${ns}'."
                else
                    echo "❌ Failed to delete secret '${secret}' from namespace '${ns}'."
                fi
            else
                echo "⚠️ Secret '${secret}' not found in namespace '${ns}', skipping deletion."
            fi
        done
    done
}


# 定义要删除的 Secret 名称数组
SECRETS=("acr-zhihe-secret-public" "acr-zhihe-secret-internal")

# 定义命名空间数组(支持多个)
NAMESPACES=("dev" "test" "prod" )

# 调用函数
delete_secrets_in_namespaces SECRETS NAMESPACES

echo "Secret deletion process completed."

总结:

shell 脚本中数组作为函数参数, 如果bash shell 版本 在5.0 以上 ,可以使用nameref 特性 会比较方便一点。 不需要重新创建数组 来解决数组在函数中作为参数的问题。
分享快乐,留住感动. '2025-07-07 23:00:52' --frank

相关推荐
vortex59 小时前
Windows 下 Git Bash 终端高效配置指南
windows·git·bash
vortex516 小时前
Bash One-Liners 学习精要指南
开发语言·chrome·bash
蒋士峰DBA修行之路17 小时前
红帽练习环境介绍
linux·开发语言·bash
张志翔的博客7 天前
如何在 macOS 上切换 Shell:从 Bash 到 Zsh 或其他 Shell
开发语言·macos·bash
zhengqiqiqinqin7 天前
reboot提示 -bash: fork: retry: 没有子进程
开发语言·bash
顾安r9 天前
11.22 脚本 手机termux项目分析(bash)
前端·python·stm32·flask·bash
Solyn_HAN11 天前
非编码 RNA(ceRNA/lncRNA/circRNA)分析完整流程:从数据下载到功能验证(含代码模板)
python·bash·生物信息学·r
叶羽西12 天前
Bash基础知识-[[和]]用于条件测试
开发语言·bash
不想画图12 天前
Linux——小白初识shell脚本(二)
linux·运维·bash
裤裤兔13 天前
linux提取指定前缀文件到其他文件夹
linux·运维·服务器·bash·终端