shell脚本------多重循环(例题源码解析)
Posted 下雨天的放羊娃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
二.直线
#!/bin/bash
for ((i=1;i<=50;i++))
do
echo -n "*"
done
echo " "
三.矩形
#!/bin/bash
for ((i=1;i<=5;i++))
do
for ((a=1;a<=50;a++))
do
echo -n "*"
done
echo " "
done
四.直角三角形
1.正直角三角形
#!/bin/bash
for ((a=1;a<=15;a++))
do
for ((b=1;b<=a;b++))
do
echo -n "*"
done
echo ""
done
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
五.等腰三角形
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
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
六.平行四边形
#!/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
七.直角梯形
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
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
八.等腰梯形
#!/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
九.菱形(可调节大小)
#!/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脚本------多重循环(例题源码解析)的主要内容,如果未能解决你的问题,请参考以下文章
不会做 还不来看?! for/while常用循环例题 shell脚本