Not able to call an element from an array in ksh

Hi,
I have:

    # Initialize variables
    #!/usr/bin/ksh

    FILENM=$1
    INDEX=0
 
    # read filename
    echo "You are working with the Config file: $FILENM"

    while read line
    do
      echo $line
      data[$INDEX]=$line
      ((INDEX=INDEX+1))
    done <"$FILENM"

  # Test data array
    echo "What line would you like to look at?"
    read INDEX
    echo "Output: $data[$INDEX]"

But when I run it, I get:

ncm@cwncm:~/work> ./ThisConf.sh ios.cfg
You are working with the Config file: ios.cfg




!
version 12.4
service timestamps debug datetime msec
  .
  .
  .
  .
end

What line would you like to look at?
5
Output: [5]

Can someone tell me where I went into left field?

Thanks

Is ksh your default shell?

If not, then you're not using ksh in your script:

# Initialize variables
#!/usr/bin/ksh

The "shebang" needs to be on the first line.

And the syntax is:

    echo "Output: ${data[$INDEX]}"

And arrays are zero-based, meaning the first element is at index 0.

1 Like

If you'll be referring to line numbers starting with 1,

echo "Output: ${data[$((INDEX-1))]}"

Hi Scott.

THANK YOU!
To be honest, I cut and paste the code so " #!/usr/bin/ksh" was actually the first line of the script

But your correction of my echo command solved the problem :smiley:

Thank you very much!

Marc Grossman