bash: combine arrays with weird substitution/references


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash: combine arrays with weird substitution/references
# 1  
Old 10-13-2009
bash: combine arrays with weird substitution/references

Hi all.

I'm trying to finish a bash script with the following elements:
Code:
ARRAY[0]="blah $ITEM blah blah"
ARRAY[1]="blah blah $ITEM blah bluh"
#ARRAY[n]="...."
# ...the ARRAY elements represent a variable but defined
#    syntax and they're all hard-coded in the script.

#(...)

ITEMS='1.0 2.3 -4.0'
#ITEMS=' .... '
# ...the ITEMS 'vector' comes from the command line
(...)

ARRAY[@] and ITEMS should have to get finally combined into NEWARRAY as detailed:

Code:
NEWARRAY[0]=blah 1.0 blah blah"
#(as ARRAY[0], but substituted with 1st item)
NEWARRAY[1]="blah 2.3 blah blah"
#(as ARRAY[0], but substituted with 2nd item)
NEWARRAY[2]="blah -4.0 blah blah"
#(as ARRAY[0], but substituted with 3rd item)
#-------------------
NEWARRAY[3]="blah  blah 1.0 blah bluh"
NEWARRAY[4]="blah blah 2.3 blah bluh"
NEWARRAY[5]="blah blah -4.0 blah bluh"
# (as ARRAY[1], but substituted)
# (...)

I think that the combination issue is not a mayor problem as it could be (naively?) resolved with 2 loops:

Code:
 c0=0; while [ $c0 -lt ${#ARRAY[@]} ]; do
    for elem in $ITEM; do
         #....???....???....
    done
    let "c0+=1"
done

Nevertheless the main problem to me is to get the $ITEMS values correctly referenced (if possible) within NEWARRAY during execution time. Anyway, I'm not sure if the whole stuff is well set...

Any help will be (desperately) welcome...

Y.

Last edited by yomaya; 10-13-2009 at 07:42 PM..
# 2  
Old 10-13-2009
Code:
me@tinybird:~$ ARRAY[0]="blah $ITEM blah blah"
me@tinybird:~$ echo ${ARRAY[0]}
blah blah blah
me@tinybird:~$ ARRAY[0]='blah $ITEM blah blah'
me@tinybird:~$ echo ${ARRAY[0]}
blah $ITEM blah blah
me@tinybird:~$ ITEM=1.0
me@tinybird:~$ echo ${ARRAY[0]}
blah $ITEM blah blah
me@tinybird:~$ eval echo ${ARRAY[0]}
blah 1.0 blah blah
me@tinybird:~$ eval 'NEWARRAY[5]=${ARRAY[0]}'
me@tinybird:~$ eval echo ${NEWARRAY[5]}
blah 1.0 blah blah


Last edited by Scrutinizer; 10-14-2009 at 03:33 AM..
# 3  
Old 10-14-2009
Perfect!

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combine two arrays. for in for ?..

Hello all, I have 2 very long list of elements. f.e.: List 1 1 2 3 4 5 List 2 a b c How can I combine the two with other. Like this: 1 a 1 b 1 c 2 a 2 b 2 c 3 a etc. (21 Replies)
Discussion started by: mrAibo
21 Replies

2. Shell Programming and Scripting

Variable substitution with arrays

Hi all, I have a script with the following gist: declare -a index=(0 1 2 3 4); declare -a animals=(dog cat horse penguin cow); declare -a fruits=(orange apple grapes peach mango); declare -a drinks=(juice milk coffee tea coke); declare -a cities=(toronto paris london glasgow... (18 Replies)
Discussion started by: Kingzy
18 Replies

3. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

4. Shell Programming and Scripting

BASH weird acting: unquoted parameter accepted as quoted one !

In one session I have strange behavior of the bash-shell: SDX-Q> echo ">$ss<" #this is just to present the $ss var: > lll kkk < SDX-Q> # more obviose: SDX-Q> od -cb <<<"$ss" 0000000 l l l k k k \n 040 040 040 154 154 154 040... (3 Replies)
Discussion started by: alex_5161
3 Replies

5. Shell Programming and Scripting

In bash getting weird output from function ?

My script- result="" times() { echo "inside the times function" result=8 echo "Inside function $result" return $result } result=$(times) echo "the value is "$? echo "the value of result $result" when I run I get this, why the value still remain 0. $ ./func the value is 0 the value... (5 Replies)
Discussion started by: boy18nj
5 Replies

6. Shell Programming and Scripting

Yet another bash arrays question

Hi all, I have a file that contains many lines, but only a few are of my interest, so I'm cutting it with grep + awk, and the result I get is for example line 0 line 1 line 2 line 3 line n Now I want to store each line in an array "cell" so I can use it later calling to ${array},... (2 Replies)
Discussion started by: TuxSax
2 Replies

7. Shell Programming and Scripting

subtraction in bash arrays

hi i am using bash shell to perform some subraction. here is what i have: i have a while loop and am using i as a counter. diff= `expr ${ARRAY1} - ${ARRAY2}` for example array1 has -0.7145 and array2 has -0.7041. when i try the above command, i get expr: non-numeric argument. any... (6 Replies)
Discussion started by: npatwardhan
6 Replies

8. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Arrays in bash.need help

:confused: Is it possible to delete array elements dynamically.For instance,consider an array( a b c d ) ,now can i delete array (the third element 'c').So that the array becomes array(a b d).. Thanks in advance!! (1 Reply)
Discussion started by: tj23
1 Replies

10. UNIX for Dummies Questions & Answers

How to get rid of all the weird characters and color on bash shell

Does anyone of you know how to turn off color and weird characters on bash shell when using the command "script"? Everytime users on my server used that command to record their script, they either couldn't print it because lp kept giving the "unknown format character" messages or the print paper... (1 Reply)
Discussion started by: Micz
1 Replies
Login or Register to Ask a Question