How to use variable inside array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use variable inside array?
# 1  
Old 01-12-2013
Linux How to use variable inside array?

I tried to use variable inside an array variable, but its not working as expected.Smilie
Code:
 
ENV1[0]=123
ENV1[1]=789
ENV1[2]=120
ENV2[0]=567
if [ $1 -eq 1 ]
then
name=ENV1
echo "${name[0]}"
echo "${name[1]}"
echo "${name[2]}"
else
name=ENV1
echo "${name[1]}"
fi

Output: ./val.sh 1
123
<blank line>

123 is for "${name[0]}" and blank line is for "${name[1]}". I want 789 for "${name[1]}" and 120 for "${name[2]}" as output.

Please help to get the correct output and i am a beginner. Thanks

Last edited by Jayavinoth; 01-12-2013 at 09:54 AM..
# 2  
Old 01-12-2013
What shell are you using? I'd rather think name=ENV1 assigns the string constant "ENV1" to the scalar variable name, so the 123 output is a bit surprising to me... possible that the array name has been assigned to earlier?
# 3  
Old 01-13-2013
Lightbulb Solved

I got the solution by usingSmilie

Code:
Set -A name ${ENV1[@]}

Code:
ENV1[0]=123 ENV1[1]=789 ENV1[2]=120 ENV2[0]=567 if [ $1 -eq 1 ] then Set -A name ${ENV1[@]} echo "${name[0]}" echo "${name[1]}" echo "${name[2]}" else name=ENV1 echo "${name[1]}" fi

Refer: eTechTricks.com

Last edited by Jayavinoth; 01-13-2013 at 09:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

2. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

3. Shell Programming and Scripting

unique inside array

I have a file root@server # cat /root/list12 11.22.33.44 22.33.44.55 33.44.55.66 33.44.55.66 33.44.55.66 I try to pass to array and display unique. root@server# cat /root/test12.sh #!/bin/bash #delcare array badips and accumulate values to array elemenrs badips=( $( cat... (4 Replies)
Discussion started by: anil510
4 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

6. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. Shell Programming and Scripting

using array inside awk

Hi All, I have the following code sequence for reading some bulk file and moving the content to two different arrays. while read data do THREEG_PATTERN=`echo $data | egrep "3G"` if then NEW_THREEG_PATTERN=`echo $THREEG_PATTERN | cut -d " " -f2` ... (12 Replies)
Discussion started by: subin_bala
12 Replies

8. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question