eval, array, number of elements - couldn't get it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting eval, array, number of elements - couldn't get it
# 1  
Old 07-01-2008
eval, array, number of elements - couldn't get it

I try to get a number of elements in an array, using dynamic array name.
I need the array name to be dynamic.

The array name is constructed as 'inf_ln_$nmb', where $nmb is a file line number
So, say I have the arr 'inf_ln_4':
Code:
> for (( el=0; el<${#inf_ln_4[*]}; el++ )); do 
>    echo "$el: >"${inf_ln_4[$el]}"<"; 
> done
0: >Address 2<
1: >Memphis, TN 38134<
2: ><
3: >31<
4: >11/1/2003<
5: >6/30/2004<
6: >00018906465 MAC 01<
7: >TRUE<
8: ><
9: ><
> # or :
>  echo ${inf_ln_4[@]}
Address 2 Memphis, TN 38134 31 11/1/2003 6/30/2004 00018906465 MAC 01 TRUE

I do next:
Code:
>
>a=4;  # this is to be used for dynamic array's name 
> eval echo ${inf_ln_$a[@]}     # expect 'eval' will replace $a for  4
bash: ${inf_ln_$a[@]}: bad substitution
>
> # the same I have by trying to access an array element:
> eval ec ${inf_ln_$a[0]}
bash: ${inf_ln_$a[0]}: bad substitution

Is there a way to prevent the bash-shell from treat the '$a[0]' as a substitution?

My main attempt is to copy the current dynamicaly numbered array to some staticly-named array to comfortably work with it's element

Appreciate your advise or help!
Alex
# 2  
Old 07-01-2008
I have found it by myself:
Code:
 eval echo $\{#inf_ln_$a[@]\}
> 10

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Issue with the incorrect number of array elements

Hello , I have a file : RestartSession.txt with the below contents : Backup p203pcrw01_OS_Weekly Failed full 10/11/2015 10:00:07 PM 1444572007 10/11/2015 10:26:23 PM 1444573583 0:00 0:26 18.76 1 08 0 0 0 2 2 180668 ... (4 Replies)
Discussion started by: rahul2662
4 Replies

4. Programming

Array Elements Check

Hi everyone, :) I'm trying to make a simple C program that scans an array of chars to see if its elements are similar. I can't understand what's wrong. Could you help me to fix this? Here is the code. Thanks! #include<stdio.h> int main() { int arr; int i, len; int flag =... (10 Replies)
Discussion started by: IgorGest
10 Replies

5. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

6. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

7. Shell Programming and Scripting

Using eval to populate an array in the background

Hi. I am trying to populate an array using eval in the background within a function. So this function will create a list of 3 character directories from SVN and store in an array. I want to call this function when the script starts in the background unknown to the user. The problem is that... (2 Replies)
Discussion started by: jmiaebrown
2 Replies

8. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

9. Shell Programming and Scripting

how to get status array for the commands in eval

I want to get a status code array for the commands in eval. For example, eval "ls abc; ls def; ls $HOME" I want to get the status code for each ls command in eval. Is there any way to make it? Thanks. (2 Replies)
Discussion started by: pankai
2 Replies

10. UNIX for Dummies Questions & Answers

Confuse on how to use array and eval..

Hi, Can anyone advise me on how to use array on Bash? I am really confuse in how eval is link to array... What i wan to do is let say i have an index of an array of 50. Store either 'N' or 'Y' or "NA" in each index. how to i go about doing it.. then lastly how to i print all the values out in... (2 Replies)
Discussion started by: Kinki
2 Replies
Login or Register to Ask a Question