A. For command
Two. While command
Three. Until command
1.for Command Basic format
1 for inch List 2 Do 3 Commands4 done
1[Email protected]:~/testshell>CatFortest.SH 2#!/bin/Bash3#test forCommand4 5 forCityinchBeijing Shanghai Shenzhen Dalian6 Do7 EchoThe city is $city8 Done 9[Email protected]:~/testshell>./fortest.SH Ten The city is Beijing One The city is Shanghai A The city is Shenzhen -The city is Dalian
A C language-style for command
1 for ((variable assignment; condition; iterationprocess)) 2 Do 3 Commands4 done
1[Email protected]:~/testshell>CatFortest.SH 2#!/bin/Bash3#test forCommand4 5 sum=06 for((i=1; i<= -; i++ ))7 Do8((sum=sum+i))9 Ten Done One Echo sum= $sum A - for((a=1, b=1;a<5,b<3; a++,b++ )) - Do the((c = A +b)) - Echoc =$c - Done -[Email protected]:~/testshell>./fortest.SH + sum=5050 -c =2 +c =4
2.while Command Basic format
1 while Test Command 2 Do 3 Other commands4 done
1[Email protected]:~/testshell>CatWhiletest.SH 2#!/bin/Bash3#test whileCommand4 5Var=36 7 while[$var-GT0 ]8 Do9(var = var-1 ))Ten Echovar =$var One Done A[Email protected]:~/testshell>./whiletest.SH -var =2 -var =1 thevar =0
3.until Command Basic format
1 until Test Commands 2 Do 3 Other commands4 done
1[Email protected]:~/testshell>CatUntiltest.SH 2#!/bin/Bash3#testuntilCommand4 5Var=56 7 until[$var-GT8 ]8 Do9((var++ ))Ten Echovar =$var One Done A[Email protected]:~/testshell>./untiltest.SH -var =6 -var =7 thevar =8 -var =9
Another thing is that the loop output can be output to the screen or output to a file, which is to add a processing command after the done command
1[Email protected]:~/testshell>CatUntiltest.SH 2#!/bin/Bash3#testuntilCommand4 5Var=56 7 until[$var-GT8 ]8 Do9((var++ ))Ten Echovar =$var One Done>Result.txt A[Email protected]:~/testshell>./untiltest.SH -[Email protected]:~/testshell>ls -Untiltest.SHResult.txt the[Email protected]:~/testshell>CatResult.txt -var =6 -var =7 -var =8 +var =9
Linux shell scripts use structured commands (2)