Loops in a loop shell are mainly for,while,untile,select for 1, list for loop for VARIABLE in (list) do Command Done example: Loop print John's favorite fruit for fruit in apple orange banana pear Do echo "$fruit is John's favorite" Done echo "No more fruits." Note: In inside can be the standard output of any command such as: fruits= "apple orange Banana pear" Seq 1 2 #产生1到100的奇数数列 ls 1..5 #表示1 2 3 4 5 #!/ Bin/bash echo ' variable as an in parameter ' fruits= ' apple orange pear ' for fruit in ${fruits} Do echo $fruit-done echo "sequence as parameter of in ' for Var In 1 2 3 4 5 does echo $var done echo "seq-generated sequence as parameter of in" #declare-I. TOtal=0 Total=0 for Var in $ (Seq 1 2) do #total + = $var # (Total + = var) #注意算数运算的写法, a total of three formulations can be assigned let "Total+=var" done echo "1+3+5+...+99 is $total" echo "LS results as an in parameter" for VAR in $ (LS) Do ls-l $var done 2, without list for loop for variable Do command-Done note: Using a for without a list requires that the value of the variable be passed as a parameter when the script is run #fo R.sh #!/bin/bash for Var does echo $var done # . ./for.sh 1 2 3 Poor readability, you can use [email protected] to achieve the above functional improvements: #!/bin/bash for Var in [email protected] do echo $var DonE 3, Class Cfor loop for ((expression; expression2; expression3)) do Comman D-done Example: #!/bin/bash for ((i=0; i<10; i++)) # There is no space for the requirements, in order to be beautiful, use the space do Echo-n "$i" did 4, for the Infinite loop for ((; 1;)) does echo done while while expression Do command done example: 1, #输出1到10的数字序列 #!/bin/bash Dec Lare-i count=0 while [[$count-lt], do echo "$count" Count+=1 done 2, #求1-100 and #求1-100 odd sum #!/bin/bash Declare-i sum01=0 declare-i sum02=0 declare-i i=1 declare-i j=1 while [[I-le 1XX]] do sum01+=i; j=i%2; #if [[$ (i%2)]-ne 0]];then if [[J-ne 0]];then Sum02+=i; Fi ((i++)); #这里不可一写 $ ((i++)) done Echo $sum echo $sum 0 2 3, #猜数字 #!/bin/bash num=8 echo "Input a number in 1 to 10:" #体会read的用法 while the read guass do if [[$guass-eq $num]];then echo "right." Break else Echo ' wrong.try Again ' fi done 4. read files by line cat student_info.txt John boy Sue Girl Wang's boy Xu Girl solution one: #!/bin/bash While read Name= ' echo $line | Cut-d '-f1 ' #echo输出line之后Tab会变成空格, so you need to specify the separator (-D ") age= ' echo $line when using Cut | Cut-d '-f2 ' #也可以使用awk awk ' {print $n} ', the delimiter for awk is a space (') or tab sex= ' echo $line | Cut-d '-f3 ' echo ' My name is $name, I ' m $age years old,i ' m a $sex "done < Student_info.txt Note: The whole idea is clear, the focus is how to read the file, choose what command processing line. In comparison, scripts are much more concise than editing a high-level language program. Two: #!/bin/bash cat Student_info.txt | While read Name= ' echo $line | Cut-d '-f1 ' age= ' echo $line | Cut-d '-f2 ' sex= ' echo $line | Cut-d '-f3 ' echo ' My name is $name, I ' m $age years old,i ' m a $sex "done The two solutions function the same, but with subtle differences. Using a redirected while only produces a shell, while a script that uses a pipeline produces 3 shells at run time, the firstThe shell is cat (running quickly, causing it to be caught without the PS command), the second shell is the pipeline, the third shell is the while 5, the infinite Loop #方法一 while ((1)) Do command did #方法二 while true Do command done #方法三 while:do Command done Example: Detection system process, #!/bin/bash WHI Le true do atd_status= ' service ATD STATUS | grep running ' If [[-Z ' $ATD _status "]];then echo" ATD is stopped, try To restart. " Service ATD Restart #在脚本中, if the return value of the command is not required, the command can be written directly without replacing the else echo "A" with the $ () command TD is running,wait 5 sec until next check "fi sleep 5 #延迟函数, Unit is seconds (s) Done UNtil structure: Until expression do command done Description: Pre-run Test, the test result is false when continuing execution, is true when the exit example: #使用until计算1到100的和以及1-100 odd and #!/bin/bash declare-i Sum01=0 declare-i sum02=0 declare-i i=0 until [[I-GT]] Do Sum01+=i if [[$[i%2]-ne 0]];then sum02+=i fi ((i++ )); Done echo "sum01= $sum" echo "sum02= $sum" until Infinite Loop until ((0)) do Command done until false does command done Sele CT Loop structure: Select Menu in (list) does command done description: Menu extension loops, syntax and a for loop with lists very similar examples: 1, judging the user's Choice #!/bin/bash echo "which car do yo U prefer?"Select car in Benz Audi VolksWagen does break #注意这个break, no this will always be selected Do echo "You chose $car" NOTE: Select has the ability to determine user input, so select is often combined with the case statement, 2. Joint use of SELECT and Case #!/bin/bash select Var in Mon Tue Wed Thu Fri Sat Sun D o Case $var in #case是不需要break语句的 Mon) echo "Today I S Monday "break;; Tue) echo "Today is Tuesday" break;; Wed) echo "Today is Wednesday" break;; Thu) echo "Today is Thursday" break;; Fri) echo "Today is Friday" break;; Sat| Sun) echo "You can has a rest day" break;; *) echo "Unknown input. Try again ";; Esac done Nested Loop example: Print 99 multiplication table #!/bin/bash Declare-i i=1; Declare-i j=1; Declare-i k=0 for ((i = 1; I <= 9; i++)) Does for ((j = 1; J <= I; j + +)) Do k= $i + $j #let "k=i+j" Echo-ne $j "*" $i "=" $k "\ T" #echo-ne $j "*" $i "=" $ (($i + $j)) "\ T" #echo-ne $j "*" $i "=" $[$j * $i] "\ t" #这里看到了多种表达式求值 done echo Do note: 1, for the Class C notation, do not need the $ symbol, nor need To use the 2, ECHO output tabs, such as-le, you need to use the-e parameter, which indicates that an escape character is escaped. No-e will output as-is without interpreting the escape character 3, expression evaluation 4, self-adapting to the other loop version of the 99 multiplication table extensionExhibition: Using SED and awk output 99 multiplication table #!/bin/bash seq 9 |sed "h;g" |awk-v rs= "" ' {for (i=1;i<=nf;i++) {J=I*NR; printf ("%d*%d=%d\t", I,NR,I*NR); } printf ("\ n"); } ' Loop control break Continue is not described in detail, as with high-level languages, the only difference is that break,continue can be followed by numbers, which means that the number of nested layers jumps out, similar to the labels in Java. Example: Print prime solution one: #!/bin/bash declare-i i=1 declare-i j=2 for ((i = 1; I < 100; i++) (j = 2; J < I; J + +)) do if! ($i% $j); then #注意! and (()) extended operations have a space break; Fi done if [[$j-eq $i]];then echo-ne "$i \ T" fi Done Echo Solution II: #!/bin/bash declare-i i=1 declare-i j=2 for ((i = 1; i <; i++)) Do for ((j = 2; J < I; J + +)) do if! ($i% $j); then #注意! and (()) extension operations have a space between continue 2; #continue后面的数字表示跳出的嵌套数 fi Done if [[$j-eq $i]];then Echo-ne "$i \ T" Fi done Echo
Shell Learning note seven (looping)