双重循环需勋在两个及以上的自变量,一般用于多层循环判断中,比如将行和列设为两个自变量按一定规则顺序显示数据,可通过Shell脚本做出各种排列图形以及九九乘法表。
循环必须是有规律的递增或递减,脚本从上至下执行,在没有break、exit、sleep、continue等结束语句时,循环语句块会逐条执行。
内层循环是外层循坏的一条循环语句,当变量满足外层循环的条件时,就会进入外层循环执行外层循环语句,当变量满足内层循环的条件时,转而执行内层循环,只有当内层循环结束才会跳出内层循环,然后继续外层循环语句的执行。
停止循环,直接退出执行脚本
跳出当前循环,默认为break 1,后面数字为跳出层数,例如break 2为跳出2层循环。
return表示退出函数并返回一个退出值,脚本中可以用$?变量显示该值
使用原则:
(1)函数一结束就取返回值,因为$?变量只返回执行的最后一条命令的退出状态码;
(2)退出状态码必须是0~255,超出时返回值将为除以256后的取余数。
终止某次循环中的命令,但不会完全终止整个命令,而是直接跳过满足条件的循环,即满足条件不循环,转而循环不满足的条件
sleep休眠,单位为秒,将线程休眠规定时间后继续执行,sleep(10)为休眠10秒。
增加延时时间,单位为0.4毫秒,即延时一秒为delay(250)。
#!/bin/bash for ((a=1; a<=20; a++)) #循环20次 do echo -n "*" #不换行连续输出* done echo "" #末尾换行
[root@localhost ~]# ./zhixian.sh ********************
#!/bin/bash for ((a=1; a<=5; a++)) #循环5次,设置行数 do for ((b=1; b<=8; a++)) #循环8次,设置列数 do echo -n "*" done echo “” done
[root@localhost ~]# ./4.sh ******** ******** ******** ******** ********
#!/bin/bash for ((a=1; a<=5; a++)) do if [ $a -eq 1 -o $a -eq 5 ];then for ((b=1; b<=8; b++)) do echo -n "*" done echo "" else echo -n "*" for ((c=1; c<=6; c++)) do echo -n " " done echo "*" fi done
[root@localhost ~]# ./kong4.sh ******** * * * * * * ********
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=1; b<=a; b++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./3.sh * ** *** **** ***** ******
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=6; b>=a; b--)) do echo -n "*" done echo "" done
[root@localhost ~]# ./dao3.sh ****** ***** **** *** ** *
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=6; b>=a; b--)) do echo -n " " done for ((c=1; c<=a; c++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./fan3.sh * ** *** **** ***** ******
#!/bin/bash for ((a=1; a<=9; a++)) do for ((b=9; b>=a; b--)) do echo -n " " done m=$[($a*2) - 1] for ((c=1; c<=$m; c++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./dengyao3.sh * *** ***** ******* ********* *********** ************* *************** *****************
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=6; b>=a; b--)) do echo -n " " done for ((c=1; c<=6; c++)) do echo -n "*" done echo "" done:::
[root@localhost ~]# ./pingxing4.sh ****** ****** ****** ****** ****** ******
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=6; b>=a; b--)) do echo -n " " done for ((c=1; c<=6+$a; c++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./tixing.sh ******* ******** ********* ********** *********** ************
#!/bin/bash for ((a=1; a<=6; a++)) do for ((b=6; b>=a; b--)) do echo -n " " done for ((c=1; c<=6+$a; c++)) do echo -n "*" done for ((d=1; d<=a; d++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./dengyaotixing.sh ******** ********** ************ ************** **************** ******************
#!/bin/bash for ((a=1; a<=9; a++)) do for ((b=8; b>=a; b--)) do echo -n " " done m=$[($a*2) - 1] for ((c=1; c<=$m; c++)) do echo -n "*" done echo "" done for ((e=8; e>=1; e--)) do for ((f=8; f>=e ; f--)) do echo -n " " done n=$[($e*2) - 1] for ((g=1 ; g<=$n ; g++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./lingxing.sh * *** ***** ******* ********* *********** ************* *************** ***************** *************** ************* *********** ********* ******* ***** *** *
#!/bin/bash read -p "请输入数字:" num for ((a=1; a<=num; a++)) do for ((b=$num-1; b>=a; b--)) do echo -n " " done m=$[($a*2) - 1] for ((c=1; c<=$m; c++)) do echo -n "*" done echo "" done for ((e=$num-1; e>=1; e--)) do for ((f=$num-1; f>=e ; f--)) do echo -n " " done n=$[($e*2) - 1] for ((g=1 ; g<=$n ; g++)) do echo -n "*" done echo "" done
[root@localhost ~]# ./lingxing.sh 请输入数字:5 * *** ***** ******* ********* ******* ***** *** * [root@localhost ~]# ./lingxing.sh 请输入数字:8 * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** *
#!/bin/bash for((i=0; i<10; i++)) { for ((j=1; j<20; j++)) { echo -n " " } for ((j=10; j>i; j--)) { echo -n " " } for ((j=0; j<i*2+1; j++)) { echo -n "*" } echo "" } for ((i=0; i<6; i++)) { for ((j=0; j<i*3; j++)) { echo -n " " } for ((j=60; j>i*6; j--)) { echo -n "*" } echo "" } for ((i=0; i<5; i++)) { for ((j=17; j>i; j--)) { echo -n " " } for ((j=0; j<25+i*2; j++)) { echo -n "*" } echo "" } for ((i=1; i<=5; i++)) { for ((j=12; j>=i; j--)) { echo -n " " } for ((j=15; j>=i*3; j--)) { echo -n "*" } for ((j=0; j<=i*8; j++)) { echo -n " " } for ((j=15; j>=i*3; j--)) { echo -n "*" } echo "" }
[root@localhost ~]# ./5jiao.sh * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ************************************************************ ****************************************************** ************************************************ ****************************************** ************************************ ****************************** ************************* *************************** ***************************** ******************************* ********************************* ************* ************* ********** ********** ******* ******* **** **** * *
#!/bin/bash for ((a=1; a<=9; a++)) do for ((b=1; b<=a; b++)) do sum=$[$a * $b] echo -n "$a*$b=$sum " done echo "" done
[root@localhost ~]# ./99.sh 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81