linux/bash:map作为参数传递给function

本文介绍在Linux Bash脚本中如何巧妙地传递Map(关联数组)作为函数参数的方法。通过实例演示了如何获取Map变量的声明字符串,重新创建临时Map变量并输出所有key和value,解决直接传递Map数据结构的难题。

在linux bash中map是作为数组处理的,不能作为参数直接传递函数,如果一定要传递给函数,要做一些变通处理,示例如下:

#!/bin/bash
function test_map()
{
	# 获取map变量的声明字符串
	# 在本例中为:declare -A user='([name]="tom" [age]="15" [sex]="male" )'
    local var=$(declare -p "$1")
    # 截取=号后的部分: '([name]="tom" [age]="15" [sex]="male" )'
    # 重新创建一个临时map变量ref
    eval "declare -A local ref"=${var#*=}
	
	# 输出所有的key
    echo keys:${!ref[@]} 
    # 输出所有的value
    echo values:${ref[@]} 
}

#  定义一个map变量
declare -A user=(['name']='tom' ['age']='15')

# 再添加一个映射
user[sex]=male

# 将参数名传递给函数
test_map user

参考资料:

https://stackoverflow.com/questions/49901305/how-to-pass-a-map-data-structure-as-an-argument-to-a-method-in-a-bash-script

https://www.linuxquestions.org/questions/programming-9/bash-passing-associative-arrays-as-arguments-4175474655/

[ 39%] Building CXX object modules/photo/CMakeFiles/opencv_photo.dir/src/tonemap.cpp.o [ 39%] Linking CXX shared library ../../lib/libopencv_imgcodecs.so /home/mao/gcc-linaro-1400-202306-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/14.0.0/../../../../aarch64-linux-gnu/bin/ld: ../../3rdparty/lib/liblibpng.a(pngrtran.c.o): in function `png_do_read_transformations': pngrtran.c:(.text.png_do_read_transformations+0x1178): undefined reference to `png_riffle_palette_neon' /home/mao/gcc-linaro-1400-202306-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/14.0.0/../../../../aarch64-linux-gnu/bin/ld: pngrtran.c:(.text.png_do_read_transformations+0x21d0): undefined reference to `png_do_expand_palette_rgba8_neon' /home/mao/gcc-linaro-1400-202306-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/14.0.0/../../../../aarch64-linux-gnu/bin/ld: pngrtran.c:(.text.png_do_read_transformations+0x325c): undefined reference to `png_do_expand_palette_rgb8_neon' /home/mao/gcc-linaro-1400-202306-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/14.0.0/../../../../aarch64-linux-gnu/bin/ld: ../../3rdparty/lib/liblibpng.a(pngrutil.c.o): in function `png_read_filter_row': pngrutil.c:(.text.png_read_filter_row+0xac): undefined reference to `png_init_filter_functions_neon' collect2: 错误: ld 返回 1 make[2]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/build.make:457:lib/libopencv_imgcodecs.so.4.10.0] 错误 1 make[1]: *** [CMakeFiles/Makefile2:2515:modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] 错误 2
03-09
glad_get_dlopen_handle': gl.c:(.text+0x24174): undefined reference to `dlopen' /usr/bin/ld: CMakeFiles/glad.dir/src/gl.c.o: in function `glad_close_dlopen_handle': gl.c:(.text+0x241b7): undefined reference to `dlclose' /usr/bin/ld: CMakeFiles/glad.dir/src/gl.c.o: in function `glad_dlsym_handle': Dependencies file "ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/src/ex_get_loadbal_param.c.o.d" is newer than depends file "/tmp/tmp/build-vtk952a_rc/ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/compiler_depend.internal". gl.c:(.text+0x241eb): undefined reference to `dlsym' Dependencies file "ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/src/ex_get_map.c.o.d" is newer than depends file "/tmp/tmp/build-vtk952a_rc/ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/compiler_depend.internal". Dependencies file "ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/src/ex_get_map_param.c.o.d" is newer than depends file "/tmp/tmp/build-vtk952a_rc/ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/compiler_depend.internal". /usr/bin/ld: Dependencies file "ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/src/ex_get_name.c.o.d" is newer than depends file "/tmp/tmp/build-vtk952a_rc/ThirdParty/exodusII/vtkexodusII/CMakeFiles/exodusII.dir/compiler_depend.internal". CMakeFiles/glad.dir/src/egl.c.o: in function `glad_get_dlopen_handle': egl.c:(.text+0x3745): undefined reference to `dlopen' /usr/bin/ld: CMakeFiles/glad.dir/src/egl.c.o: in function `glad_close_dlopen_handle': egl.c:(.text+0x3788): undefined reference to `dlclose' /usr/bin/ld: CMakeFiles/glad.dir/src/egl.c.o: in function `glad_dlsym_handle':
最新发布
11-11
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值