shell-嵌套循环之九九乘法表

kehanxin 2020-05-26

for循环嵌套
#!/bin/bash
  for i in `seq 1 9`;
  do
    for j in `seq 1 $i`;
    do
            let "k=$i*$j"
            echo -ne "$i*$j=$k\t"
    done
    echo
  done

输出结果
shell-嵌套循环之九九乘法表

while循环嵌套
#!/bin/bash
  a=1
  while [ $a -le 9 ]
  do
    b=1
    while [ $b -le $a ]
    do
            let "c=$a*$b"
            echo -ne "$a*$b=$c\t"
            let "b+=1"
    done
    echo
    let "a+=1"
  done

输出结果
shell-嵌套循环之九九乘法表

until循环嵌套
#!/bin/bash
  a=1
  until [ $a -gt 9 ]
  do
    b=1
    until [ $b -gt $a ]
    do
            let "c=$a*$b"
            echo -ne "$a*$b=$c\t"
            let "b+=1"
    done
    echo
    let "a+=1"
  done

输出结果:
shell-嵌套循环之九九乘法表

相关推荐