本文介绍了巴什二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:)嗯,我想读线和file.o保存第一和第二列,我想将它保存到在bash二维数组。过滤是确定其做正确的想法。但我不知道如何检查,如果数组是空的(我的意思是,它没有保存到数组,以便其所有从文件)。其次即时得到的功能及RELIANCE该命令的错误没有被发现,再次回声不工作,尝试谷歌一切,但似乎没有人正在与两个索引二维数组。泰任何提示!

 #!/斌/庆典
声明-a名字
声明-a功能
声明-a RELIANCE
索引= 1
索引1 = 1
对于在file.o

  名称[$指数] = $一个
  直到[纳米file.o | grep的-o'[UTBGCD] *。| AWK'{打印$ 2}'| awk的NR == $索引1-eq 0]
  做
    FUNCTION [$指数] [$指数1] = $(NM file.o | grep的-o'[UTBGCD] *。'| awk的'{$打印2}| awk的NR == $指数1)
    RELIANCE [$指数] [$指数1] = $(NM filea.o | grep的-o'[UTBGCD] *。'| awk的'{$打印1}'| awk的NR == $指数1)
    回声$ {FUNCTION [$指数] [$指数1]}
    索引1 = $((索引1 + 1))
  DONE
  指数= $((索引+ 1))
DONE


解决方案

击不支持多维数组。但是,您可以伪造它以两种方式之一。首先,有关联数组:

 声明-A数组
X = 1 Y = 2
数组[$ {X} _ $ [Y}] =价值

二,固定大小的数组,你可以简单地做一些数学。

 声明-a阵列
x_max = 100
X = 1 Y = 2
数组[(Y * x_max)+ X] =价值

:) well i want to read line and save first and second column from file.o And i want to save it into two dimensional array in bash. Filtration is ok its doing the right think. BUT i dont know how to check if the array is empty ( i mean that it saved nothing to that array so its all from the file) . Secondly im getting error on FUNCTION and RELIANCE that command was not found and again echo is not working, tried to google everything but it seems that no one is working with two indexed dimensional arrays. Ty for any tip!

#!/bin/bash
declare -a NAMES
declare -a FUNCTION
declare -a RELIANCE
index=1
index1=1
for a in file.o
do
  NAMES[$index]=$a
  until [ nm file.o | grep -o '[UTBGCD].*' | awk '{print $2}' | awk "NR==$index1" -eq 0 ]
  do
    FUNCTION[$index][$index1]=$( nm file.o | grep -o '[UTBGCD].*' | awk '{print $2}' | awk "NR==$index1" )
    RELIANCE[$index][$index1]=$( nm filea.o | grep -o '[UTBGCD].*' | awk '{print $1}' | awk "NR==$index1" )
    echo ${FUNCTION[$index][$index1]}
    index1=$((index1+1))
  done
  index=$((index+1))
done
解决方案

Bash does not support multidimensional arrays. However, you can fake it in one of two ways. First, there's associative arrays:

declare -A array
x=1 y=2
array["${x}_$[y}"]=value

Second, with fixed-size arrays, you can simply do some math.

declare -a array
x_max=100
x=1 y=2
array[(y*x_max)+x]=value

这篇关于巴什二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:52