Array indexing in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array indexing in shell
# 1  
Old 02-04-2010
Array indexing in shell

Hi ,
I have 4 array as below

Input:
Code:
servernames=(10.144.0.129 10.144.0.130 10.144.0.131)
subfolder_129=(PSTN_SigtranCamel_03 PSTN_SigtranCamel_04 PSTN_SigtranCamel_05)
subfolder_130=(SigtranCamel_11 SigtranCamel_12 SigtranCamel_13 SigtranCamel_14 SigtranCamel_15)
subfolder_131=(SigtranCamel_21 SigtranCamel_22 SigtranCamel_23 SigtranCamel_24 SigtranCamel_25)

Output:
For each elements of servernames array I need to select all elements of respective subfolder elements. i.e for server name 10.144.0.129 need to select all elemets of subfolder_129 array. and server 10.144.0.131 need to select all elements of subfolder_131.

I have writen below scripts but not working. Could please help me on this?
Code:
foldername=(subfolder_129 subfolder_130 subfolder_131, )
for server in $servernames
do
                j=0
                subfolder=${foldername[$j]}
                index=0
                echo $subfolder
                for x in ${"$subfolder"[$index]}; do
                echo $x

                done
done


Last edited by pludi; 02-04-2010 at 02:03 AM.. Reason: code tags, please...
# 2  
Old 02-04-2010
Code:
j=0
subfolder=${foldername[$j]}
# index=0 no need of this
echo $subfolder
# for x in ${"$subfolder"[$index]}; do
for x in ${subfolder[@]}; do
echo $x
done

# 3  
Old 02-11-2010
Thanks for you reply Frans.

But echo $X prints 'subfolder_130'. I want value SigtranCamel_11 in $x. how to do the same?

Regards,
SUshma
# 4  
Old 02-11-2010
I replaced the subfolders arrays by blank separated strings (the arrays don't support the ${!VARIABLE} syntax.
Code:
#!/bin/bash
servernames=( 10.144.0.129 10.144.0.130 10.144.0.131 )
subfolder_129="PSTN_SigtranCamel_03 PSTN_SigtranCamel_04 PSTN_SigtranCamel_05"
subfolder_130="SigtranCamel_11 SigtranCamel_12 SigtranCamel_13 SigtranCamel_14 SigtranCamel_15"
subfolder_131="SigtranCamel_21 SigtranCamel_22 SigtranCamel_23 SigtranCamel_24 SigtranCamel_25"
for server in ${servernames[@]}
do
    subfolders="subfolder_${server##*.}"
    echo "server : $server"
    for x in ${!subfolders}
    do
        echo "x='$x'"
    done
done

# 5  
Old 02-16-2010
Its working. This what I exactly wanted. Thank you so much Frans Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Dummies Questions & Answers

Single Liner for indexing

Hello, This is pretty simple, I`m looking for a faster and better method than brute force that I`m doing. I have a 20GB file looks like Name1,Var1,Val1 Name1,Var2,Val2 Name2,Var1,Val3 Name2,Var2,Val4 I want 3 files. Nameindex 1 Name1 2 Name2 ... (2 Replies)
Discussion started by: senhia83
2 Replies

3. Shell Programming and Scripting

Indexing Variable Names

Hi All I think I might have bitten off more than I can chew here and I'm hoping some of you guys with advanced pattern matching skills can help me. What I want to do is index the occurrence of variable names within a library of scripts that I have. Don't ask why, I'm just sad like that... ... (3 Replies)
Discussion started by: bbq
3 Replies

4. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

5. Shell Programming and Scripting

Split a file using 2-D indexing system

I have a file and want to split it using a 2-D index system for example if the file is p.dat with 6 data sets separated by ">". I want to set nx=3, ny=2. I need to create files p.dat.1.1 p.dat.1.2 p.dat.1.3 p.dat.2.1 p.dat.2.2 p.dat.2.3 I have tried using a single index and want... (3 Replies)
Discussion started by: kristinu
3 Replies

6. UNIX for Dummies Questions & Answers

awk, array indexing

cat filename|nawk ' { FS="="; if (!a++ == 0) print $0 } ' can anyone plz explain how does array inexing works,how it is evaluating if (!a++ == 0)?? (2 Replies)
Discussion started by: dreamzalive
2 Replies

7. Shell Programming and Scripting

indexing a file

hello guys, I have a file like this: input.dat Push-to-talk No Coonection IP support Support for IP telephony Yes Built-in SIP stack Yes Support via software Yes Microsoft Support for Microsoft Exchange Yes UMA (5 Replies)
Discussion started by: Johanni
5 Replies

8. Shell Programming and Scripting

indexing list of words in a file

Hey all, I'm doing a project currently and want to index words in a webpage. So there would be a file with webpage content and a file with list of words, I want an output file with true and false that would show which word exists in the webpage. example: Webpage content data.html ... (2 Replies)
Discussion started by: Johanni
2 Replies

9. Shell Programming and Scripting

[ask]filtering file to indexing...

dear all, i have file with format like this file_master.txt 20110212|231213|rio|apri|23112|222222 20110212|312311|jaka|dino|31223|543234 20110301|343322|alfan|budi|32131|333311 ... i want filter with output like this index_nm.txt rio|apri jaka|dino ... index_years.txt 20110212... (7 Replies)
Discussion started by: zvtral
7 Replies
Login or Register to Ask a Question