shell脚本—图形(双循环)
目录
一、直角三角形
1.1 正直角三角形
1.1.1 for循环
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n "*"
done
echo ""
done
1.1.2 while循环
i=1
while [ $i -le 9 ]
do
j=1
while [ $j -le $i ]
do
echo -n "*"
let j++
done
let i++
echo ""
done
1.2 倒直角三角形
1.2.1 for循环
for ((i=1;i<=9;i++))
do
for((j=9;j>=i;j--))
do
echo -n "*"
done
echo ""
done
1.2.2 while循环
i=1
while [ $i -le 9 ]
do
j=9
while [ $j -ge $i ]
do
echo -n "*"
let j--
done
let i++
echo ""
done
1.3 右侧正直角三角形
1.3.1 for循环
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
1.3.2 while循环
i=1
while [ $i -le 9 ]
do
k=9
while [ $k -ge $i ]
do
echo -n " "
let k--
done
j=1
while [ $j -le $i ]
do
echo -n "*"
let j++
done
echo ""
let i++
done
1.4 右侧倒直角三角形
1.4.1 for循环
for ((i=1;i<=9;i++))
do
for ((k=1;k<=i;k++))
do
echo -n " "
done
for ((j=9;j>=i;j--))
do
echo -n "*"
done
echo ""
done
1.4.1 while循环
i=1
while [ $i -le 9 ]
do
j=1
while [ $j -le $i ]
do
echo -n " "
let j++
done
k=9
while [ $k -ge $i ]
do
echo -n "*"
let k--
done
echo ""
let i++
done
二、等腰三角形
2.1 正等腰三角形
for ((i=1;i<=9;i++))
do
for ((k=9;k>=i;k--))
do
echo -n " "
done
a=$[$i*2-1]
for ((j=1;j<=$a;j++))
do
echo -n "*"
done
echo ""
done
2.2 倒等腰三角形
for ((i=9;i>=1;i--))
do
for ((k=9;k>=i;k--))
do
echo -n " "
done
a=$[$i*2-1]
for ((j=1;j<=$a;j++))
do
echo -n "*"
done
echo ""
done
三、菱形
for ((i=1;i<=9;i++))
do
for ((k=9;k>=i;k--))
do
echo -n " "
done
a=$[$i*2-1]
for ((j=1;j<=$a;j++))
do
echo -n "*"
done
echo ""
done
for ((i=8;i>=1;i--))
do
for ((k=9;k>=i;k--))
do
echo -n " "
done
a=$[$i*2-1]
for ((j=1;j<=$a;j++))
do
echo -n "*"
done
echo ""
done
四、矩形
4.1 全矩形
for ((i=1;i<=10;i++))
do
for ((j=1;j<=20;j++))
do
echo -n "*"
done
echo ""
done
4.2 镂空矩形
for ((i=1;i<=10;i++))
do
if [ $i -eq 1 -o $i -eq 10 ];then
for ((j=1;j<=20;j++))
do
echo -n "*"
done
echo ""
else
echo -n "*"
for ((k=1;k<=18;k++))
do
echo -n " "
done
echo "*"
fi
done
五、平行四边形
5.1 正平行四边形
for ((i=1;i<=9;i++))
do
for ((j=9;j>=i;j--))
do
echo -n " "
done
for ((k=1;k<=9;k++))
do
echo -n "*"
done
echo ""
done
5.2 倒平行四边形
for ((i=1;i<=9;i++))
do
for ((j=1;j<=i;j++))
do
echo -n " "
done
for ((k=1;k<=9;k++))
do
echo -n "*"
done
echo ""
done
六、梯形
6.1 直角梯形
for ((i=1;i<=9;i++))
do
for ((j=9;j>=i;j--))
do
echo -n " "
done
for ((k=1;k<=9+$i;k++))
do
echo -n "*"
done
echo ""
done
注:倒直角梯形同理,这边就不再阐述了
6.2 等腰梯形
for ((i=1;i<=9;i++))
do
for ((j=9;j>=i;j--))
do
echo -n " "
done
for ((k=1;k<=9+$i;k++))
do
echo -n "*"
done
for ((g=1;g<=i;g++))
do
echo -n "*"
done
echo ""
done