如何在循环中仅运行一个步骤?

时间:2019-03-17 13:15:55

标签: bash shell loops

我已经完成了下面的shell代码:

declare -i x=3;
declare -i y=8;
declare -i z=6;

for r1 in 0.80 0.90
do
  for r2 in 0.80 0.90
  do
    for (( i=0; i <= 48; i++))
    do
      origin=$((x + z*i))
      end=$((y + z*i))
      rm -r ${name}_${r1}_${r2}.gnu

      echo "beginrow=$origin" > ${name}_${r1}_${r2}.gnu
      echo "endrow=$end"  >> ${name}_${r1}_${r2}.gnu
      echo "r1=$r1" >> ${name}_${r1}_${r2}.gnu
      echo "r2=$r2" >> ${name}_${r1}_${r2}.gnu
    done
  done
done

我希望它仅对每对r1和r2变量执行第三循环的一个步骤。相反,当我运行代码时,它将为每对r1和r2变量执行一个完整的第三循环循环。我应该改变什么?

1 个答案:

答案 0 :(得分:0)

您应该指定循环的上限和下限。目前, 您正在从0迭代到48:

for (( i=0; i <= 48; i++))

您说:“我希望它对每对r1和r2变量只执行第三循环的一个步骤。” 。您要完成哪个步骤?看来您根本不需要第三个循环:

declare -i x=3;
declare -i y=8;
declare -i z=6;

for r1 in 0.80 0.90
do
  for r2 in 0.80 0.90
  do
    # for (( i=0; i <= 48; i++))
    # do
      i = 48
      origin=$((x + z*i))
      end=$((y + z*i))
      rm -r ${name}_${r1}_${r2}.gnu

      echo "beginrow=$origin" > ${name}_${r1}_${r2}.gnu
      echo "endrow=$end"  >> ${name}_${r1}_${r2}.gnu
      echo "r1=$r1" >> ${name}_${r1}_${r2}.gnu
      echo "r2=$r2" >> ${name}_${r1}_${r2}.gnu
    # done
  done
done

像这样的建议编码是否有意义?