Visit Our UNIX and Linux User Community


Not able to call an element from an array in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to call an element from an array in ksh
# 1  
Old 10-26-2012
Not able to call an element from an array in ksh

Hi,
I have:
Code:
    # 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:
Code:
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
# 2  
Old 10-26-2012
Is ksh your default shell?

If not, then you're not using ksh in your script:
Code:
# Initialize variables
#!/usr/bin/ksh

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

And the syntax is:
Code:
    echo "Output: ${data[$INDEX]}"

And arrays are zero-based, meaning the first element is at index 0.
This User Gave Thanks to Scott For This Post:
# 3  
Old 10-26-2012
If you'll be referring to line numbers starting with 1,
Code:
echo "Output: ${data[$((INDEX-1))]}"


Last edited by elixir_sinari; 10-26-2012 at 11:24 AM.. Reason: Thanks to Scott!
# 4  
Old 10-26-2012
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 Smilie

Thank you very much!

Marc Grossman

Quote:
Originally Posted by Scott
Is ksh your default shell?

If not, then you're not using ksh in your script:
Code:
# Initialize variables
#!/usr/bin/ksh

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

And the syntax is:
Code:
    echo "Output: ${data[$INDEX]}"

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

Previous Thread | Next Thread
Test Your Knowledge in Computers #904
Difficulty: Medium
The benchmark used in the LINPACK Benchmark is to solve a dense system of nonlinear equations.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

2. Shell Programming and Scripting

File names as array element in ksh

Hi, I have an ksh array(ARR). the elements to the array are file names. i need to go to each file in the array and manipulate the records. for name in ${files}; do ---this loop is for all the file names in the array for i in $(wc -l < $name); do --this loop is for all the records in... (20 Replies)
Discussion started by: usrrenny
20 Replies

3. Shell Programming and Scripting

ksh insert element in array

Hi all, I need help with the following scenario in ksh. If the number of elements contained by arrayA is 11 I need to insert a zero as the element arrayA then print all arrayA elements separated by comma. Appreciate your help. (9 Replies)
Discussion started by: ejianu
9 Replies

4. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

5. Emergency UNIX and Linux Support

Assigning zero to element of ksh array.

set -A matched #find referenced files. for i in ${file_names_html} do counter_j=0 for j in ${file_names_minus_index} do match=`cat $i | grep... (1 Reply)
Discussion started by: robin_simple
1 Replies

6. Shell Programming and Scripting

Problem to initialize ksh array when first element includes hyphen

Hi I'm trying to create an array with variable including hyphen but ksh refuses the first element set -A allArgs set +A allArgs ${allArgs} -all set +A allArgs ${allArgs} -date set +A allArgs ${allArgs} test ./test.ksh: -all: bad option(s) It happens only when first element is like... (4 Replies)
Discussion started by: gdan2000
4 Replies

7. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

8. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

Adding array element in KSH

All, I would like to add the first 10 elements of an array. Here is how I am doing it now (only included first few add ops): #!/usr/bin/ksh ###Grab the array values out of a file### TOTAL=`awk '/time/' /tmp/file.out | awk '{print $4}'` set -A times $TOTAL SUM=$((${times} + times... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies

10. Shell Programming and Scripting

accessing my first element of array

Hello everyonel, I have an array set like so num=4 read name arr=name I go through while loop to assign different values to different array element from 1 to 4. when I try to access the FIRST element of the array I get the last one first. Like if I say ${arr} it will show the last element... (4 Replies)
Discussion started by: afadaghi
4 Replies

Featured Tech Videos