Shell脚本画图形

【Shell脚本画图形】少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述Shell脚本画图形相关的知识,希望能为你提供帮助。
@toc
1、画直线
for循环画直线

#!/bin/bash #for循环画直线 for ((i=1; i< =30; i++)) do echo -n "-" done echo ""

while循环画直线
#!/bin/bash #while循环画直线 a=1 while [ $a -le 30 ] do echo -n "-" let a++ done echo ""

2、画矩形
for循环画矩形
#!/bin/bash #for循环画矩形 for ((i=1; i< =5; i++)) do for ((j=1; j< =15; j++)) do echo -n "*" done echo "" done

空心矩形
#!/bin/bash #for循环空心矩形 for ((i=1; i< =5; i++)) do if [ $i -eq 1 -o $i -eq 5 ] then for ((j=1; j< =15; j++)) do echo -n "*" done echo " " else echo -n "*" for ((k=1; k< =13; k++)) do echo -n " " done echo "*" fi done

3、画三角形
左下直角三角形(for循环)
#!/bin/bash #左下直角三角形 for ((i=1; i< =8; i++)) do for ((j=1; j< =i; j++)) do echo -n " *" done echo " " done

左下空心直角三角形(for循环)
#!/bin/bash #左下空心直角三角形 for ((i=1; i< =10; i++)) do if [ $i -eq 1 -o $i -eq 10 -o $i -eq 2 ] then for ((j=1; j< =i; j++)) do echo -n "*" done echo " " else echo -n "*" for ((k=1; k< =(i-2); k++)) do echo -n " " done echo"*" fi done

自定义左下直角三角形(for循环)
#!/bin/bash #自定义边长的三角形 read -p "输入自定义三角形的边长:" a for ((i=1; i< =$a; i++)) do for ((j=1; j< =i; j++)) do echo -n " *" done echo " " done

自定义左下直角三角形(while循环)
#!/bin/bash #while循环 自定义边长三角形 read -p "输入自定义三角形的边长:" a i=1 while [ $i -le $a ] do j=1 while [ $j -le $i ] do echo -n " *" let j++ done echo " " let i++ done

左上直角三角形(for循环)
#!/bin/bash #左上直角三角形 for ((i=1; i< =10; i++)) do for ((j=10; j> =i; j--)) do echo -n "*" done echo " " done

自定义左上直角三角形(for循环)
#!bin/bash #自定义左上直角三角形for循环 read -p "输入三角形边长:" a for ((i=1; i< =$a; i++)) do for ((j=$a; j> =$i; j--)) do echo -n "*" done echo " " done

自定义左上直角三角形(while循环)
#!/bin/bash read -p "输入三角形边长:" a i=1 while [ $i -le $a ] do j=$a while [ $j -ge $i ] do echo -n "*" let j-- done echo " " let i++ done

右下直角三角形(for循环)
#!/bin/bash #右下直角三角形for循环 for ((a=1; a< =10; a++)) do #空格倒三角 for ((b=10; b> =$a; b--)) do echo -n " " donefor ((c=1; c< =$a; c++)) do echo -n "*" done echo " " done

自定义右下直角三角形(for循环)
#!/bin/bash #自定义右下直角三角形for循环 read -p "输入自定义边长:" x for ((a=1; a< =$x; a++)) do for ((b=$x; b> =$a; b--)) do echo -n " " done for ((c=1; c< =a; c++)) do echo -n "*" done echo " " done

自定义右下直角三角形(while循环)
#!/bin/bash #自定义右下直角三角形while循环 read -p "输入自定义三角形边长:" x a=1 while [ $a -le $x ] do b=$x while [ $b -ge $a ] do echo -n "-" let b-- done c=1 while [ $c -le $a ] do echo -n "*" let c++ done echo " " let a++ done

右上直角三角形(for循环)
#!/bin/bash #右上直角三角形for for ((a=1; a< =10; a++)) do #空格倒三角 for ((b=1; b< =$a; b++)) do echo -n "#" done for ((c=10; c> =$a; c--)) do echo -n "*" done echo " " done

等腰三角形(for循环)
#!/bin/bash #等腰三角形 for ((a=1; a< =10; a++)) do for ((b=10; b> =$a; b--)) do echo -n " " done for ((c=1; c< =$[$a*2-1]; c++)) do echo -n "*" done echo" " done

自定义等腰三角形(for循环)
#!/bin/bash #自定义等腰三角形for循环 read -p "输入自定义边长:" x for ((a=1; a< =$x; a++)) do for ((b=$x; b> =$a; b--)) do echo -n " " done for ((c=1; c< =($a*2-1); c++)) do echo -n "*" done echo " " done

自定义等腰三角形(while循环)
#!/bin/bash #自定义等腰三角形while循环 read -p "输入自定义三角形边长:" x a=1 while [ $a -le $x ] do b=$x while [ $b -ge $a ] do echo -n " " let b-- done c=1 k=$[($a*2-1)] while [ $c -le $k ] do echo -n "*" let c++ done echo " " let a++ done

倒等腰三角形(for循环)
#!/bin/bash #倒等腰三角形 for ((a=10; a> =1; a--)) do for ((b=10; b> =$a; b--)) do echo -n " " done for ((c=1; c< =$[$a*2-1]; c++)) do echo -n "*" done echo " " done

自定义倒等腰倒三角形(for循环)
#!/bin/bash #自定义倒等腰三角形for循环 read -p "输入自定义边长:" x for ((i=1; i< =$x; i++)) do for ((j=1; j< =$i; j++)) do echo -n " " done a=$[($i*2)-1] b=$[($x*2)-1] for ((k=$b; k> =$a; k--)) do echo -n "*" done echo " " done

自定义倒等腰三角形(while循环)
#!/bin/bash #自定义等腰三角形while循环 read -p "输入自定义边长:" x i=1 while [ $i -le $x ] do j=1 while [ $j -le $i ] do echo -n " " let j++ done a=$[$i*2-1] b=$[$x*2-1] k=$b while [ $k -ge $a ] do echo -n "*" let k-- done echo " " let i++ done

4、菱形
菱形(for循环)
#!/bin/bash #菱形for循环 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

自定义菱形(for循环)
#!/bin/bash #自定义菱形for循环 read -p "请输入想要的菱形大小(数字):" n for((a=1; a< =$n; a++)) do for ((b=$n; b> =$a; b--)) do echo -n " " do for ((b=$n; b> =$a; b--)) do echo -n " " done for ((c=1; c< =$[$a*2-1]; c++)) do echo -n "*" done echo "" done for ((i=$n-1; i> =1; i--)) do for ((j=$n; j> =$i; j--)) do echo -n " " done for ((l=1; l< =$[$i*2-1]; l++)) do echo -n "*" done echo "" done

平行四边形1
#!/bin/bash #平行四边形1 for ((i=10; i> =1; i--)) do for((k=1; k< =i; k++)) do echo -n " " done for (( j=10; j> =1; j--)) do echo -n " *" done echo " " done

平行四边形2
#!/bin/bash #平行四边形2 for ((i=1; i< =10; i++)) do for((k=1; k< =i; k++)) do echo -n "#" done for (( j=1; j< =10; j++)) do echo -n " *" done echo " " done

梯形
#!/bin/bash #梯形 for ((a=1; a< =8; a++)) do for ((b=8; b> =$a; b--)) do echo -n " " done for ((c=1; c< =8; c++)) do echo -n "*" done for ((d=1; d< =$[$a*2-1]; d++)) do echo -n "#" done echo " " done


    推荐阅读