problem in access in array variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in access in array variables
# 1  
Old 09-13-2009
problem in access in array variables

hi all, its me again!!! i've requirement like this:
i want to create a file & an array with its name having the filename as its substring. here is the test script!!
Code:
#!/bin/bash
touch $1
declare -a $1_rec;
echo -n "$1_rec[1]: "
read $1_rec[1];
echo $[$1_rec[1]];

now see output:
this is when i enter only numeric value:
Code:
$./ex3 hell
hell_rec[1]: 45
45

when entered string or char.:
Code:
$ ./ex3 hell
hell_rec[1]: boy
0

entered alphanumeric string with number first:
Code:
$ ./ex3 hell
hell_rec[1]: 45ex
./ex3: line 6: 45ex: value too great for base (error token is "45ex")

entered alphanumeric string with char. first:
Code:
$ ./ex3 hell
hell_rec[1]: ex45
0

i actually wanted the value entered to redirect into the file.

thanks in advance!
regards
prayush
# 2  
Old 09-13-2009
Code:
echo $[$1_rec[1]];

should be
Code:
echo ${$1_rec[1]};

# 3  
Old 09-15-2009
Data

thanks binlib, but its not working!!Smilie
the code:
Code:
echo ${$1_rec[1]};

is giving this error:
Code:
./ex2: line 11: ${$1_rec[1]}: bad substitution

please help me ..its urgent!!!
# 4  
Old 09-15-2009
Quote:
Originally Posted by tprayush
Code:
echo ${$1_rec[1]};

How about ...

Code:
echo ${1_rec[1]};

# 5  
Old 09-15-2009
thanks dr.house!! but again output is same!!
Code:
./ex2: line 16: ${1_rec[1]}: bad substitution

i've tried all possible combination..but helpless!!!!!!!Smilie
# 6  
Old 09-15-2009
Code:
#!/bin/bash
touch $1
declare -a ${1}_rec;
echo -n "${1}_rec[1]: "
read ${1}_rec[1];
eval echo \${${1}_rec[1]};

# 7  
Old 09-15-2009
try
Code:
declare -a ${1}_rec
echo -n "${1}_rec[1] : "
read ${1}_rec[1]
echo $(eval echo \${${1}_rec[1]})

seems to work
P.S. sorry I didn't see that vgersh has already posted a reply

Last edited by sidorenko; 09-15-2009 at 12:24 PM.. Reason: post scriptum added
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Access a value in 2D array in C program

Hi All, I am new to c programming. I am getting compilation error in the below program. Can somebody help me? #include<stdio.h> #include<string.h> void main() { int i=j=0; char a={'f1',4,'f2','4'}; char count; for(i=0;i<2;i++) { for(j=1;j<=2;j++) { ... (2 Replies)
Discussion started by: sam_14189
2 Replies

2. Shell Programming and Scripting

How to access the bash variables in csh

Hi, I am having a primary script which is Bash based. I am calling a csh script from it. Now, their are some variables defined in my bash script which i need in csh. I am unable to do so. Is it possible ? (2 Replies)
Discussion started by: vdhingra123
2 Replies

3. Programming

Regarding the protected variables access

Hello forum, I am siva working as programmer .I was blocked with the below issue so please help any of the forum memebers. testve.h class cv { protected : struct state; state& m_state; }; testVe.cpp struct state { m_size; } the above are 2 files which have the... (3 Replies)
Discussion started by: workforsiva
3 Replies

4. Shell Programming and Scripting

problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle... (4 Replies)
Discussion started by: adlmostwanted
4 Replies

5. Shell Programming and Scripting

array variables

Hi, There are total 100 files in the backup directory with the name format as below: prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt How to use array variables (in perl) to get list of names of the above files ? (13 Replies)
Discussion started by: milink
13 Replies

6. Solaris

Unable to access 3500 FC array. Where is the problem?..

Hi, I have two Sun Fire V490 with Solaris 10 5/08, FC switch and two Sun StorageTek 3500 FC arrays. Each array is connected to switch and to one server at a time. In the last week I installed Solaris 10 5/08 on both servers and set up Sun Cluster, version 3.2. At wednesday all was fine - all... (7 Replies)
Discussion started by: Sapfeer
7 Replies

7. Shell Programming and Scripting

Getting variables into a array.

Hi , Im trying to monitor 2 instancesof a process on our solaris server and trying to do a notification if the returned process size is greater than 500M. Since there are two variables returned I want to make use of arrays to check each and every variable that is stored. the issue that im facing... (2 Replies)
Discussion started by: vivsiv
2 Replies

8. Shell Programming and Scripting

How to access variables across scripts

Hi All, I have declared a variable in script1 and assign a value for it. In script2 i'll call script1 and then I want the value of variables set in script1. I have tried with export, but in vain. How can I achive this? Below is the two scripts. --script1 #!/usr/bin/ksh echo $1... (1 Reply)
Discussion started by: javaDev
1 Replies

9. Shell Programming and Scripting

Array variables

hi, how to store array values in unix? and how to access array values? can somebody help? kavitha (2 Replies)
Discussion started by: kavitha
2 Replies

10. UNIX for Dummies Questions & Answers

Read variables from Access table

Hi! I've just started learning shell scripting, and have been somewhat 'thrown in at the deep-end and told to swim' so excuse my complete lack of knowledge and ignorance, but here goes... I've been given a unix script to 'tidy up'. Basically the script consists of the few lines of code being... (2 Replies)
Discussion started by: Sn33R
2 Replies
Login or Register to Ask a Question