简体   繁体   English

数组初始化中的ksh93“下标超出范围”错误

[英]ksh93 “subscript out of range” error in array initialization

In a ksh93 script, I'm trying to use a two dimensions array. 在ksh93脚本中,我尝试使用二维数组。 I need to initialize each cell with a string "B1". 我需要使用字符串“ B1”初始化每个单元格。 Here is part of my code : 这是我的代码的一部分:

#!/bin/ksh93
num_cols=192
echo Number of cols : $num_cols
#init matrix to blank
echo initialize the matrix
i=1
while [ $i -le $num_rows ]; do
    j=1
    while [ $j -le $num_cols ]; do
        matrix[$i][$j]="B1"
        echo $matrix[$i][$j]
        j=$(($j+1))
    done
    i=$(($i+1))
done

when I execute this, I get that error and I can't figure out why : 当我执行此操作时,我得到了那个错误,但我不知道为什么:

+ num_cols=192
+ echo echo Number of cols : 192 echo Number of cols : 192
+ echo initialize the matrix initialize the matrix
+ i=1
+ [ 1 -le 15 ]
+ j=1
+ [ 1 -le 192 ]
+ matrix2html.sh[38]: matrix: subscript out of range

I also tried this basic code as a test and it's working fine : 我还尝试了此基本代码作为测试,并且工作正常:

#!/bin/ksh93
for i in 1 2 3
do
   for j in 4 5 6
   do
       for k in 7 8 9
       do
           array[$i][$j][$k]=$(( i + j + k ))
#          echo ${array[$i][$j][$k]}
       done
   done
done

for i in 1 2 3
do
   echo ${array[$i][4][7]}
done

Thx for your help. 谢谢您的帮助。

Finally, i managed to figure out what was wrong. 最后,我设法弄清楚出了什么问题。 For logging purpose, I was starting my script using sh +x scriptname.sh but that script is using ksh93. 出于记录目的,我使用sh + x scriptname.sh启动脚本,但是该脚本使用ksh93。 so the correct command line to start it is ksh93 +x scriptname.sh 因此启动它的正确命令行是ksh93 + x scriptname.sh

Sorry for that stupid mistake. 对不起那个愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM