shell------多重循环(例题源码解析)

一.九九乘法表

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

shell------多重循环(例题源码解析)

二.直线

#!/bin/bash

for ((i=1;i<=50;i++))
do
        echo -n "*"
done
echo " "

shell------多重循环(例题源码解析)

三.矩形

#!/bin/bash

for ((i=1;i<=5;i++))
do
        for ((a=1;a<=50;a++))
        do
        echo -n "*"
        done
echo " "
done

shell------多重循环(例题源码解析)

四.直角三角形

1.正直角三角形

#!/bin/bash

for ((a=1;a<=15;a++))
do
        for ((b=1;b<=a;b++))
        do
                echo -n "*"
        done
echo ""
done

shell------多重循环(例题源码解析)

2.倒直角三角形

#!/bin/bash

for ((a=1;a<=15;a++))
do
        for ((b=1;b<=a;b++))
        do
                echo -n " "
        done
        for ((c=15;c>=a;c--))
        do
                echo -n "*"
        done
echo ""
done

shell------多重循环(例题源码解析)

五.等腰三角形

1.三角形拼接

#!/bin/bash

for ((a=1;a<=12;a++))
do
        for ((b=12;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

shell------多重循环(例题源码解析)

2.算法(2n-1)

#!/bin/bash

for ((a=1;a<=12;a++))
do
        for ((b=12;b>=a;b--))
        do
                echo -n " "
        done
        for ((c=1;c<=a*2-1;c++))
        do
                echo -n "*"
        done
echo ""
done

shell------多重循环(例题源码解析)

六.平行四边形

#!/bin/bash

for ((a=1;a<=12;a++))
do
        for ((b=12;b>=a;b--))
        do
                echo -n " "
        done
        for ((c=1;c<=a;c++))
        do
                echo -n "*"
        done
        for ((d=11;d>=a;d--))
        do
                echo -n "*"
        done
echo ""
done

shell------多重循环(例题源码解析)

七.直角梯形

1.左直角梯形

#!/bin/bash

for ((i=1;i<=10;i++))
do
        for ((a=1;a<=8;a++))
        do
                echo -n "*"
        done
        for ((b=1;b<=i;b++))
        do
                echo -n "*"
        done
echo " "
done

shell------多重循环(例题源码解析)

2.右直角梯形

#!/bin/bash

for ((i=1;i<=10;i++))
do
        for ((a=10;a>=i;a--))
        do
                echo -n " "
        done
        for ((b=1;b<=i;b++))
        do
                echo -n "*"
        done
        for ((c=1;c<=11;c++))
        do
                echo -n "*"
        done
echo " "
done

shell------多重循环(例题源码解析)

八.等腰梯形

#!/bin/bash

for ((i=1;i<=9;i++))
do
        for ((a=9;a>=i;a--))
        do
                echo -n " "
        done
        for ((b=1;b<+9;b++))
        do
                echo -n "*"
        done
        for ((c=1;c<=$[i*2-1];c++))
        do
                echo -n "*"
        done
echo " "
done


shell------多重循环(例题源码解析)

九.菱形(可调节大小)

#!/bin/bash
read -p "请输入大小:" s
for ((a=1;a<=s;a++))
do
        for ((b=s;b>=a;b--))
        do
                echo -n  " "
        done
        c=$[a*2-1]
        for d in $(seq 1 $c)  
        do 
        echo -n "*" 
        done  
        echo ""
done
for ((e=s-1;e>=1;e--))
do  
        for ((f=s;f>=e;f--)) 
        do
                echo  -n " "
        done
        for ((h=e;h>=1;h--))
        do
               echo -n "*"

        done
        for ((m=e-1;m>=1;m--))
        do
               echo -n "*"
        done
       echo ""
done

shell------多重循环(例题源码解析)
shell------多重循环(例题源码解析)

上一篇:linux下自动重启挂掉的python脚本


下一篇:git push的翻译