简体   繁体   中英

ksh93 “subscript out of range” error in array initialization

In a ksh93 script, I'm trying to use a two dimensions array. I need to initialize each cell with a string "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. so the correct command line to start it is ksh93 +x scriptname.sh

Sorry for that stupid mistake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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