Shell Programming for loop Summary
In shell programming, the execution of a loop is repeated several times for a piece of code, a common loop has a for, while, and a until loop, where the for loop is often used for finite cycles, and the syntax structure for the FOR Loop has the following two:
The first type:Shell legacy for loop syntax structure
For variable name in variable value list; do Instructions... Done The list of values is usually 5 different ways: (1) Give the list directly, separated by a space in the middle of the list [[Email protected] ~] #for i in 1 3 5;do echo $i;d One 1 3 5 (2) List of integers (a) {start: End [[Email protected] ~] #for i in {1..5..2};d o echo $i;d one 1 3 5 (b) $ (SEQ start step End) [[Email protected] ~] #for i in $ (SEQ 1 2 5);d o echo $i;d one 1 3 5 (3) Return to command list [[Email protected] ~] #mkdir Test [[Email protected] ~] #cd test/ [[email protected] ~/test] #touch 1.sh [[email protected] ~/test] #touch 3.sh [[email protected] ~/test] #touch 5.sh [[email protected] ~/test] #for i in $ (LS);d o echo $i;d one 1.sh 3.sh 5.sh (4) using glob, such as: *.sh [[email protected] ~/test] #for i in *\.sh; Do echo $i;d one #选择当前目录下满足 *.sh 1.sh 3.sh 5.sh (5) Use variable references such as [email protected],$* [[email protected] ~/test] #vim 1 [[email protected] ~/test] #chmod +x 1 1 #!/bin/bash 2 for i in [email protected];d o 3 RM-RF $i 4 Done [[email protected] ~/TEST]#./1 1.sh 3.sh 5.sh [[email protected] ~/test] #ls 1 #验证1. sh 3.sh 5.sh deleted |
The second structure: C-language structural body
for (EXP1; exp2; exp3) Do Instructions Done [[email protected] ~/test] #for ((i=1; i<=5; i=i+2));d o echo $i;d one 1 #注意: The difference between a=a++ and a=a+2, a=a+2 is an expression, the = number has a lower priority, so the first + post-assignment 3 5 |
Basic Case Analysis:
1. Print the 99-exponent formula,
[[email protected] ~/scripts] #vim sufakoujue.sh #!/bin/bash For I in $ (seq 9);d o For J in $ (seq $i);d o Let sum= $i * $j echo-n "$j * $i = $sum" Done Echo #Print line breaks, one line at a time for Each loop Done [[email protected] ~/scripts] #chmod +x sufakoujue.sh [Email protected] ~/scripts]#./suanfakoujue.sh 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 |
2. determine the type of all files in the current directory
[[email protected]~/test] #vim panduan.sh #!/bin/bash for filename in *;d o # Use the wildcard character Glob if[-F $filename];then echo $filename is file else with the fourth way in the For statement list [-D $filename] Echo$filename is directory &NBSP;FI done [[email protected] ~/test]# Chmod+x panduan.sh [[email protected] ~/test]#./panduan.sh 1 is file 1.sh are file 2 is a file 3 is dir Ectory 3.sh is file 5 are directory 5.sh is file panduan.sh is file |
3. calculates the sum of 1+2+3+4+...+n, where n is entered by the user
[roo[ Email protected]~/test] #vim sum.sh #!/bin/bash read-p "please inputyour Inter:" Inter #++ +++++++++++makesure The parameter isn't empty++++++++ [-z $inter] && echo "You must input a inter!" &A mp;& exit1 #+++++++++++++makesure The parameter is integer++++++++++ if[[$inter =~ ^[0-9]+$ &nbs p;]]; Then for me in $ (seq $inter);d o Let sum= $sum +i done echo "1+2+. + $i = $sum " else echo" You must input a inter! " fi [[email protected] ~/test] #chmod +x sum.sh [[email protected] ~/test]#./sum.sh Please input your inter:4 1+2+. +4=10
|
4. calculates the sum of integers within 100 that can be divisible by 3
[Email protected]~/test] #vim 3sum.sh #!/bin/bash For I in $ (Seq 3 3);d o #利用整数列表步进的方式 Let sum= $sum +i Done echo "3+6+ ... 99= $sum " [[email protected] ~/test] #chmod +x 3sum.sh
3+6+ ... 99=1683 |
5. determine the network segment host survival status of LAN 192.168.1.0
[[email protected] ~/test] #vim hostping.sh net=192.168.1 for i in {1..255};d o { If Ping-c1-w1 $net. $i &>/dev/null;then echo "$net. $i is Exsit" fi &nb Sp }& #通过放入后台并行执行提高执行效率 to avoid sequential execution done wait &NBS P #避免敲回车退出脚本, you can view Help by helping wait [[email protected] ~/test] #chmod +x hostping.sh [[email& Nbsp;protected] ~/test]#./hostping.sh 192.168.1.48 is exsit |
6. Print Isosceles three angle Shape
[Email protected] ~/test]vim dengyao.sh
Read-p "Please input a inter:" A For I in $ (seq$a);d o Let j= $a-$i +1 Let k=2* $i-1 #++++++++ Prints white space characters for each line of isosceles triangles +++++ For L in $ (seq $j);d o Echo-n "" Done #++++++++ Printing isosceles triangle composition Graphics +++++++ For n in $ (seq $k);d o Echo-n "" Done #++++++++ the line ends after each line +++++++++++ Echo Done [Email protected] ~/test]./dengyao.sh
|
For details, please contact: QQ767743577 e-mail address: [email protected], have asked to answer, there is Bing, everyone for me, I for everyone ******* |
This article is from the "11831715" blog, please be sure to keep this source http://11841715.blog.51cto.com/11831715/1958918
Shell Programming for loop Summary