Shell多層迴圈&&利用迴圈語句輸出簡單圖形
阿新 • • 發佈:2021-07-20
二層迴圈
for 語句
#!/bin/bash for ((i=1; i<=10; i++)) do echo "outer loop:$i" for ((j=1; j<=5; j++)) do echo "--inter loop:$j" done done
while 語句
#!/bin/bash a=1 while [ $a -le 5 ] do echo "outer loop:$a" b=1 while [ $b -le 10 ] do echo "--inter loop:$b" let b++ done let a++ done
三層迴圈
#!/bin/bash for ((i=1; i<=10; i++)) do echo "outer loop:$i" for ((j=1; j<=5; j++)) do echo "--1st inter loop:$j" for ((k=1; k<=3; k++)) do echo "---2nd inter loop:$k" done done done
break 跳出單個迴圈
for 語句
#!/bin/bash for ((a=1; a<=5; a++)) do echo "outer loop:$a" for ((b=1; b<=50; b++)) do if [ $b -eq 5 ];then break fi echo "--inter loop:$b" done done
while 語句
#!/bin/bash a=1 while [ $a -le 5 ] do echo "outer loop:$a" b=0 while [ $b -le 10 ] do let b++ if [ $b -ge 4 -a $b -le 8 ];then break fi echo "--inter loop:$b" done let a++ done
continue 中止某次迴圈中的命令,但不會完全中止整個命令
for 語句
#!/bin/bash for ((a=1; a<=15; a++)) do if [ $a -gt 5 ] && [ $a -lt 10 ];then continue fi echo "current value:$a" done
while 語句
#!/bin/bash a=1 while [ $a -le 5 ] do echo "outer loop:$a" b=0 while [ $b -lt 10 ] do let b++ if [ $b -ge 4 -a $b -le 8 ];then continue fi echo "--inter loop:$b" done let a++ done
多層迴圈中的break命令
#!/bin/bash for ((i=1; i<=10; i++)) do echo "outer loop:$i" for ((j=1; j<=5; j++)) do echo "--1st inter loop:$j" for ((k=1; k<=3; k++)) do echo "---2nd inter loop:$k" if [ $k -eq 2 ];then break fi done done done
break 後跟數字?表示跳出?層迴圈
#!/bin/bash for ((i=1; i<=10; i++)) do echo "outer loop:$i" for ((j=1; j<=5; j++)) do echo "--1st inter loop:$j" for ((k=1; k<=3; k++)) do echo "---2nd inter loop:$k" if [ $k -eq 2 ];then break 2 fi done done done
輸出一行字串
#!/bin/bash for ((i=1; i<=20; i++)) do echo -n "*" done echo ""
輸出一個由字串組成的矩形
#!/bin/bash for ((i=1; i<=10; i++)) do for ((j=1; j<=20; j++)) do echo -n "*" done echo "" done
輸出一個空心矩形
#!/bin/bash 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
輸出一個三角形
#!/bin/bash for ((i=1; i<=9; i++)) do for ((j=1; j<=i; j++)) do echo -n "*" done echo "" done
輸出一個倒三角形
#!/bin/bash for ((i=1; i<=9; i++)) do for ((j=9; j>=i; j--)) do echo -n "*" done echo "" done
輸出一個反三角形
#!/bin/bash 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
輸出一個等腰三角形
方法一
#!/bin/bash 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 for ((g=2; g<=i; g++)) do echo -n "*" done echo "" done
方法二
#!/bin/bash for ((i=1; i<=9; i++)) do for ((j=9; j>=i; j--)) do echo -n " " done for ((k=1; k<=$[$i*2-1]; k++)) do echo -n "*" done echo "" done
輸出一個倒等腰三角形
#!/bin/bash for ((i=9; i>=1; i--)) do for ((j=9; j>=i; j--)) do echo -n " " done for ((k=1; k<=$[$i*2-1]; k++)) do echo -n "*" done echo "" done
輸出一個菱形
#!/bin/bash for ((i=1; i<=9; i++)) do for ((j=9; j>=i; j--)) do echo -n " " done for ((k=1; k<=$[$i*2-1]; k++)) do echo -n "*" done echo "" done for ((i=10; i<=17; i++)) do for ((j=0; j<=$[$i-9]; j++)) do echo -n " " done for ((k=8; k>=$[$i-9]; k--)) do echo -n "*" done for ((g=7; g>=$[$i-9]; g--)) do echo -n "*" done echo "" done
輸出一個可變大小的菱形
#!/bin/bash read -p "enter a variate:" N for ((i=1; i<=$N; i++)) do for ((j=$N; j>=i; j--)) do echo -n " " done for ((k=1; k<=$[$i*2-1]; k++)) do echo -n "*" done echo "" done for ((i=$[$N+1]; i<=$[$N*2-1]; i++)) do for ((j=0; j<=$[$i-$N]; j++)) do echo -n " " done for ((k=$[$N-1]; k>=$[$i-$N]; k--)) do echo -n "*" done for ((g=$[$N-2]; g>=$[$i-$N]; g--)) do echo -n "*" done echo "" done
輸出一個平行四邊形
#!/bin/bash for ((i=1; i<=6; i++)) do for ((j=1; j<=i; j++)) do echo -n " " done for ((k=1; k<=6; k++)) do echo -n "*" done echo "" done
輸出一個九九乘法表
#!/bin/bash for i in `seq 9` do for j in `seq 9` do [ $j -le $i ] && echo -n -e "$i*$j=`echo $(($i*$j))`\t" done echo "" done