一、for语句的双层嵌套
1.1、九九乘法表
#!/bin/bash # 九九乘法表 for ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do echo -ne " $i*$j=$[`expr $i*$j`] " done echo "" done
1.2:直角三角形
外层循环控制行,内存循环控制列
外层循环执行一次,内层循环执行一遍
打印直角三角形
echo -n ##不换行输出
echo -e #支持加反斜线转义的字符( 是水平制表符)
#!/bin/bash # 直角三角形 for ((i=1;i<=6;i++)) do for ((j=1;j<=i;j++)) do echo -n "*" done echo "" done
等腰三角形
#!/bin/bash # 等腰三角形 for ((i=1;i<=9;i++)) do for ((j=9;j>=i;j--)) do echo -n " " done for ((k=1;k<=i;k++)) do echo -n "* " done echo "" done ~
菱形
#!/bin/bash # 菱形 for ((i=1;i<=9;i++)) do for ((j=9;j>=i;j--)) do echo -n " " done for ((k=1;k<=i;k++)) do echo -n "* " done echo "" done for ((u=0;u<i;u++)) do for ((m=1;m<=u;m++)) do echo -n " " done for (( h=9;h>=u;h--)) do echo -n "* " done echo "" done
平行四边形
#!/bin/bash # 平行四边形 for ((i=1;i<=10;i++)) do for ((j=9;j>=i;j--)) do echo -n " " done for ((k=1;k<=i;k++)) do echo -n "*" done for ((l=1;l<=i;l++)) do echo -n "*" done for ((o=9;0>=i;o--)) do echo -n "*" done for ((y=9;y>=i;y--)) do echo -n "*" done for ((u=9;u>=i;u--)) do echo -n "*" done echo
2、幸运大抽奖
#!/bin/bash # 幸运大抽奖 a=0 b=0 c=0 for ((i=1;i<=10;i++)) do host=$(expr $[ RANDOM % 3+1 ]) namelist=$(cat /root/1.txt | grep $host | awk -F: '{print$2}') case $namelist in zhangsan) let a++ ;; lisi) let b++ ;; wanger) let c++ esac done echo "zhangsan$a票,lisi$b票,wanger$c票"