0

I have two arrays in a bash script, every array have same number of elements, I need to write 2nd array's every element for every element in the first array in a for loop

first array name: ARR_MPOINT

second array name: ARR_LVNAME

piece of the script:

  for MPOINT in "${ARR_MPOINT[@]}"

    do

        /sbin/mkfs -t $ftype /dev/mapper/VolGroup01-${ARR_LVNAME[$COUNT]}

        cp /etc/fstab /etc/fstab.org

        echo "/dev/mapper/VolGroup01-${ARR_LVNAME[***what should come hear***]}     $MPOINT         xfs      defaults        1 2" >> /etc/fstab

    done
Ramana
  • 3
  • 5
  • Not clear what you want. Do you want 1st element of each array on the 1st iteration, 2nd elements on the 2nd, etc? You aren't currently using `MPOINT` or `ARR_MPOINT` at all in the body of the loop. – chepner Jun 28 '17 at 12:11
  • I suspect you just want to iterate over the indices of either array, then use that index with both arrays in the loop: `for i in "${!ARR_MPOINT[@]}"; do`. – chepner Jun 28 '17 at 12:12
  • what is COUNT variable? – skr Jun 28 '17 at 12:16
  • ARR_MPOINT=(/tmp /var /log); ARR_LVNAME=(lv_tmp lv_var lv_log) I need to print below output /dev/mapper/VolGroup01-lv_tmp /tmp xfs defaults 1 2 /dev/mapper/VolGroup01-lv_var /var xfs defaults 1 2 /dev/mapper/VolGroup01-lv_log /log xfs defaults 1 2 – Ramana Jun 28 '17 at 12:19

1 Answers1

0

You want to iterate over the indices of the array, not the values, so that you can use it with both arrays.

cp /etc/fstab /etc/fstab.org
for i in "${!ARR_MPOINT[@]}"; do
  /sbin/mkfs -t "$ftype" "/dev/mapper/VolGroup-01-${ARR_LVNAME[i]}"
  echo "/dev/mapper/VolGroup01-${ARR_LVNAME[i]} ${ARR_MPOINT[i]} xfs defaults 1 2"
done >> /etc/fstab

(I suspect you don't need to create a short-lived copy of /etc/fstab after every line; back it up once before the loop starts, then append the output of the entire loop to the file.)

chepner
  • 497,756
  • 71
  • 530
  • 681