shell脚本多重循环小实验
一、99乘法表
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=1;b<=$a;b++)) do c=$[$a*$b] echo -n -e "$a*$b=$c\t" done echo "" done
二、矩形
#!/bin/bash for ((a=1;a<=5;a++)) do for ((b=1;b<=10;b++)) do echo -n "*" done echo "" done
三、直线
#!/bin/bash for ((a=1;a<=15;a++)) do echo -n "*" done echo ""
四、正直角三角形
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=1;b<=$a;b++)) do echo -n "*" done echo "" done
五、反直角三角形
1、 空格矩形加三角
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=1;b<=10;b++)) do echo -n " " done for ((c=1;c<=$a;c++)) do echo -n "*" done echo "" done
2、空格倒三角加三角
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n " " done for ((c=1;c<=$a;c++)) do echo -n "*" done echo "" done
六、倒直角三角形
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n "*" done echo "" done
七、等腰直角三角形
1、算法计算 $a*2-1
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n " " done for ((c=1;c<=$[$a*2-1];c++)) do echo -n "*" done echo "" done
2、三角加小三角
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n " " done for ((c=1;c<=$a;c++)) do echo -n "*" done for ((d=2;d<=$a;d++)) do echo -n "*" done echo "" done
八、等腰梯形
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n " " done for ((c=1;c<=9;c++)) do echo -n "*" done for ((d=1;d<=$[$a*2-1];d++)) do echo -n "*" done echo "" done
九、菱形
#!/bin/bash for ((a=1;a<=9;a++)) do for ((b=9;b>=$a;b--)) do echo -n " " done for ((c=1;c<=$[$a*2-1];c++)) do echo -n "*" done echo "" done for ((d=1;d<=10;d++)) do for ((e=2;e<=$d;e++)) do echo -n " " done for ((f=10;f>=$d;f--)) do echo -n "*" done for ((g=9;g>=$d;g--)) do echo -n "*" done echo "" done
十、可调节大小菱形
#!/bin/bash read -p "输入大小" i for ((a=1;a<=$i;a++)) do for ((b=$i;b>=$a;b--)) do echo -n " " done for ((c=1;c<=$[$a*2-1];c++)) do echo -n "*" done echo "" done for ((d=1;d<=$i+1;d++)) do for ((e=1;e<=$d-1;e++)) do echo -n " " done for ((f=2*$i+1;f>=$[$d*2-1];f--)) do echo -n "*" done echo "" done