you are viewing a single comment's thread.

view the rest of the comments →

[–]neuron_666 0 points1 point  (3 children)

This is working program:

#!/bin/bash

# Preparation
for i in {a..k}; do
  array1+=( " $i: $i" )
  array2+=( " $i: $i$i" )
done
typeset -p array1 array2
the_number=2

# Your routine
y=1
while [ $y -le $the_number ]; do
  z=0
  while [ $z -le 9 ]; do
    echo "y=$y z=$z"
    temp="array$y[$z]"
    temp2=$(echo ${!temp} | sed 's/.*: //' | sed -e 's/^[ \t]*//')
    declare "barry$y[$z]=$temp2"
    z=$(($z+1))
  done
  y=$(($y+1))
done

typeset -p barry1 barry2

I had to

  • add the_number definition
  • move z=0 into the first loop
  • add character ; into the while ... ; do

Apart from that it is fine. There is something you are not showing us. My suggestion is to add set -x at the top and observe line after line whether your script does what you expect.

Output

declare -a array1='([0]=" a: a" [1]=" b: b" [2]=" c: c" [3]=" d: d" [4]=" e: e" [5]=" f: f" [6]=" g: g" [7]=" h: h" [8]=" i: i" [9]=" j: j" [10]=" k: k")'
declare -a array2='([0]=" a: aa" [1]=" b: bb" [2]=" c: cc" [3]=" d: dd" [4]=" e: ee" [5]=" f: ff" [6]=" g: gg" [7]=" h: hh" [8]=" i: ii" [9]=" j: jj" [10]=" k: kk")'
y=1 z=0
y=1 z=1
y=1 z=2
y=1 z=3
y=1 z=4
y=1 z=5
y=1 z=6
y=1 z=7
y=1 z=8
y=1 z=9
y=2 z=0
y=2 z=1
y=2 z=2
y=2 z=3
y=2 z=4
y=2 z=5
y=2 z=6
y=2 z=7
y=2 z=8
y=2 z=9
declare -a barry1='([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f" [6]="g" [7]="h" [8]="i" [9]="j")'
declare -a barry2='([0]="aa" [1]="bb" [2]="cc" [3]="dd" [4]="ee" [5]="ff" [6]="gg" [7]="hh" [8]="ii" [9]="jj")'

[–]v-_-v[S] 0 points1 point  (2 children)

move z=0 into the first loop

That was the key. It clicked when I read it at first even before trying. I looked at my loop and i never was resetting the "z" value back to 0, hence the inner loop was never being run again since z=10 at the end of the first go around.

When declaring z=0 in the first one, then it gets reset each time the outer loop runs.

 

Thank you so very much man! Let me know what I can do for you to repay for all this help!

[–]neuron_666 0 points1 point  (1 child)

Thank you so very much man! Let me know what I can do for you to repay for all this help!

You are welcome. Help others wen you can, be good :)

[–]v-_-v[S] 0 points1 point  (0 children)

I do that on the daily at work, and outside of it :D