How to use string in array name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use string in array name?
# 1  
Old 01-09-2013
How to use string in array name?

Hi,
I'm the beginner in bash scripting and I just want to create loop, which will create a few tables with string in name, and each time i try to do this in the following way, I receive:
Code:
a=1; 
while [ $a -ne 5 ] 
    do 
    echo "Plik a=$a"
    for m in {1..4} 
        do echo "Plik m=$m"
        table_$a=(`grep pattern-filename-$m*`)
    done
    ((a+=1))
done

Code:
bash: syntax error near unexpected token ``grep pattern-filename-$m*`'

Please help me Smilie
# 2  
Old 01-09-2013
Code:
table_$a=$(grep pattern-filename-$m*)

Try to avoid using backtics `code`, its hard to read, and nesting needs escaping. Do use $(code) instead.
# 3  
Old 01-09-2013
Now I did not receive this error message, but for following code:
Code:
a=1; 
while [ $a -ne 3 ] 
    do 
    echo "Plik a=$a"
    for m in {1..2} 
        do echo "Plik m=$m"
        table_$a=$(ps -ef | grep bash | awk '{print $2}' | head -5)
    done
    ((a+=1))
done

where the output for command ps -ef | grep bash | awk '{print $2}' | head -5 is
Code:
533
1192
8195
16404
16437

I get output:
Code:
Plik a=1
Plik m=1
bash: table_1=533: command not found
Plik m=2
bash: table_1=533: command not found
Plik a=2
Plik m=1
bash: table_2=533: command not found
Plik m=2
bash: table_2=533: command not found

So sth is wrong...
For now I'm only interested in getting all numbers into each table_$a.

Last edited by masterqu; 01-09-2013 at 04:31 AM.. Reason: removed back quotes
# 4  
Old 01-09-2013
You don't. Shell doesn't do that kind of doublethink, as a security feature. Neither do most languages. You can brute force it with eval but that will be ugly, and a security hole.

Some like ksh, awk, or perl, can do something similar through associative arrays like table["asdf"]="qwerty". But what are you actually trying to do? Why do you need randomly named variables?
# 5  
Old 01-09-2013
Hi,
I've got two files randomly named with common begin and different number (eg. filename1, filename2).
Each filename* contains lines with random number. Now I want to create table (with name the same as file) per file in one loop.

Last edited by masterqu; 01-09-2013 at 04:49 AM..
# 6  
Old 01-09-2013
That's just repeating that you want variable names picked at runtime. I'm asking why you need variables named at runtime. I'm not sure how you'd even use those dynamically named arrays. You've just found out that shell doesn't do that kind of doublethink easily, you'd probably have to resort to ugly and unsafe eval tricks.

What are you going to be doing with these tables of data? If you need sequential access, that's easy to do without dumping the whole file into memory.
# 7  
Old 01-10-2013
The whole idea of program I want to use is to do:

1) For a randomly named files (eg, fileName1, fileName223) grep lines that include pattern, then cut the third column of each line.

So now, output will be:
table1= (1034 2345)
table223=(2312 4567)
....
2) Each element of table* I want to use to grep all lines with them {( (1034 2345) and (2312 4567)} from source files (eg, fileName1, fileName223)
note: I want to grep them separately (1034 2345 from fileName1; 2312 4567 from fileName223)

This is the final result for now, out will be:
1034 file with all lines includes this code(1034) in file fileName1
2345 file with all lines includes this code(2345) in file fileName1
2312 file with all lines includes this code(2312) in file fileName223
4567 file with all lines includes this code(4567) in file fileName223

Hope this is clear for you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to split the string value to an array?

Test1.txt Tom is hot Test.sh filename="/directory/Test1.txt" set - A store while IFS= read value do awk '{split($value,store," ")}' done < "$filename" echo ${#sore} From the code in the executing file, I would like each... (8 Replies)
Discussion started by: TestKing
8 Replies

2. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

3. UNIX for Beginners Questions & Answers

How to split a string into array?

value=malayalam # i need to store the value in an array by splitting the character #the output i need is m a l a y a l a m Please use CODE tags for output data as well as required by forum rules! (5 Replies)
Discussion started by: Meeran Rizvi
5 Replies

4. Shell Programming and Scripting

Shell string array

Hi all, I want to create an array variable in shell, for example: #!/bin/sh name="foo" name="bar" name="baz" But above code didn't work, I also tried with: name=(foo bar baz) and set -A name foo bar baz but none of these worked. Another question is how to know the shell... (6 Replies)
Discussion started by: Roy987
6 Replies

5. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

6. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Programming

String Array in java

Hi , I wonder how I can set up an String array in java. I need the array so I can store different items per line so each item will be like one record. I tried something like : String x = new String; but when it comes to storing the data and retrieve the is when I struggle. The... (2 Replies)
Discussion started by: arizah
2 Replies

8. Shell Programming and Scripting

how to convert array into the string

Hi I have a line/string as follows: A=" 3498 NORDEA - INDX LINKED NORY" which was converted into an array of characters: p321$ echo "${ARR}" 3 4 9 8 N O R D E A - I N D X L I N K E D N O R Y When I am trying print this array there are blank... (4 Replies)
Discussion started by: aoussenko
4 Replies

9. UNIX for Dummies Questions & Answers

String Array

I am having a string with the value: str="name1, name2, name3, " I want to traverse the list of elements. How do I loop them? (6 Replies)
Discussion started by: ravikirankethe
6 Replies

10. UNIX for Dummies Questions & Answers

string to array

Hi Experts, I'm new to Shell Scripting world and need a little help from you "Scripting Gurus". I want to convert a string (entered by a user) to array in csh. I want something like this: echo "Enter the Numbers: " set num = $< Suppose, user enters: 1 2 3 4 5 6 7 This... (6 Replies)
Discussion started by: sumitgarg
6 Replies
Login or Register to Ask a Question