Bash数组没有设置值

时间:2017-06-12 21:04:47

标签: arrays bash loops syntax

我有一个执行以下操作的Bash脚本: 它将读入一个文件,并解析csv文件的第3列以存储到数组中,该数组稍后将用于外部循环中的计算。

    dummy_Test_File=~/dummyTest/*.csv
    array=()
    for entry in ${dummy_Test_File}
    do
      lineCount=0
      echo "File to be checked: ${entry}"
      echo "---------------------------------------------------------------" 

      awk -F '[,]' '{
          printf( "deg: %f |\n", $3 ); 
      }' ${entry} >> ${Pass_Fail_Manifest_Temp}
      sed -i "/\b\(0.000000\|[0123456789][0123456789][0123456789][0123456789].000000\)\b/d" ${Pass_Fail_Manifest_Temp}
      cat ${Pass_Fail_Manifest_Temp} | while read line
      do
        MYVAR=$line
        DEGVAL=${MYVAR% |*}  # retain the part before the |
        DEGVAL=${DEGVAL##*:}  # retain the part after the last :
        #DEGVAL=$(echo "scale=6; ( ${DEGVAL} )" | bc)
        array+=($DEGVAL)
        echo "$lineCount : (${array[$lineCount]})"
        ((lineCount++))
      done

      for (( i=0; i<=5; i++ ))
      do
        echo "i: $i , array[$i]: ${array[$i]}"
        #DO SOME CALCULATION/MANIPULATION HERE
      done
    done
  echo "---------------------------------------------------------------"

结果如下:

File to be checked: /home/user/dummyTest/dummy.csv
---------------------------------------------------------------
0 : (-115.250140)
1 : (-79.464851)
2 : (-118.490720)
3 : (-134.699540)
4 : (17.391972)
5 : (60.691368)
i: 0 , array[0]: 
i: 1 , array[1]: 
i: 2 , array[2]: 
i: 3 , array[3]: 
i: 4 , array[4]: 
i: 5 , array[5]: 
---------------------------------------------------------------

然而,我曾预料到:

0 : (-115.250140)
1 : (-79.464851)
2 : (-118.490720)
3 : (-134.699540)
4 : (17.391972)
5 : (60.691368)
i: 0 , array[0]: -115.250140
i: 1 , array[1]: -79.464851
i: 2 , array[2]: -118.490720
i: 3 , array[3]: -134.699540
i: 4 , array[4]: 17.391972
i: 5 , array[5]: 60.691368

0 个答案:

没有答案