简体   繁体   English

Shell脚本数组

[英]Shell script array

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 我在bash脚本中有两个数组,每个数组具有相同数量的元素,我需要为for循环中第一个数组中的每个元素编写第二个数组的每个元素

first array name: ARR_MPOINT 第一个数组名称:ARR_MPOINT

second array name: ARR_LVNAME 第二个数组名称: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

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.) (我怀疑您不需要在行之后创建/etc/fstab的短期副本;在循环开始之前将其备份一次,然后将整个循环的输出附加到文件中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM