Objective
Practice the shell to improve your logical abilities.
Script
Triangles and diamonds are implemented through several for loops, but can also be used while.
Here is the script content:
Triangle
#!/bin/bashread -p "Please input the longs:" longfor ((i=1;i<= $long; i++)) do for ((j= $long; j>i;j--) #控制空格输出 do echo -n " " done for m in ' seq 1 $i ' #输出所在行数相同的数字个数, as in the third row, outputs 3 3 do echo -n "$i" done #for ((n=1;n<i;n++)) for n in ' seq 2 $i ' #输出所在行数减一的数字个数, as in the third row, outputs 2 three do ## This can be combined with a for loop to achieve output per line (number of rows-1) digits echo -n "$i "&NBSP;&NBSP;&NBSP;&NBSP;DONE&NBSP;&Nbsp; echo "" Done
After the execution:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/78/61/wKiom1Z7OtPyCdplAAD7V8yWXAc165.jpg "title=" 1111111.jpg "alt=" Wkiom1z7otpycdplaad7v8ywxac165.jpg "/>
If you change the $i of the two for loop inside the above script to Echo-n $i "" to *, the script effect is as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/78/61/wKiom1Z7PIGQigzCAAC2u-DQK04106.jpg "title=" 222222.jpg "alt=" Wkiom1z7pigqigzcaac2u-dqk04106.jpg "/>
Diamond
#!/bin/bashread -p "Please input the longs:" longfor ((i=1;i<= $long; i++)) #图形上半部分do for ((j= $long; j>i;j--)) do echo -n " " done for m in ' seq 1 $i ' do echo -n "*" done #for ((n=1;n<i;n++)) for n in ' seq 2 $i ' do echo -n "*" done echo "" Donefor (i=2;i<= $long, i++) #图形下半部分do for ((j=1;j<i;j++)) do echo -n " " done let k= $long +1-$i for m in ' seq 1 $k ' do echo -n "*" done for ((n=1;n<k;n++)) #for n in ' seq 2 $k ' do echo -n "*" done echo "" "Done
After improvement:
#!/bin/bashread -p "Please input the longs:" longfor ((i=1;i<= $long; i++)) #控制行数do for (j= $long; j>i;j--) #控制空格输出 do echo -n " " done let "G=2*i-1" for m in ' seq 1 $g ' #控制 * Quantity do echo -n "*" done echo "" Donefor ((i=2;i<= $long; i++) #控制行数do for ((j=1;j<i;j++)) #控制空格输出 do echo - n " " done let "k=2* (long-i) +1" for m in " seq 1 $k ' #控制 * Quantity do echo -n "*" done echo "" Done
After the execution:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/78/61/wKiom1Z7RTLiRun4AADRWz-0eH4083.jpg "title=" 1111111.jpg "alt=" Wkiom1z7rtlirun4aadrwz-0eh4083.jpg "/>
Shell script output diamond and equilateral triangle