for command
for Var in list
Do
Commands
Done
Code 2-1
[Email protected]:/data# cat demo1 #!/bin/bashfor test in Alabama Alaska Arizona Arkansas ' California Colorado ' do Echo "The next state is $test" Doneecho "The last state we visited be $test" test= "Connecticut" echo "Wait, now we ' re visiting $ Test "[Email protected]:/data#/demo1 The next state was alabamathe next state was alaskathe next state was arizonathe next s Tate is arkansasthe next state are California coloradothe last state we visited were California coloradowait, now we ' re Visi Ting Connecticut
As shown in code 2-1, if the string you want to print contains spaces, you can wrap it in quotation marks
The For loop sometimes encounters special symbols:
- Use escape characters to escape single quotes
- Use double quotation marks to define the value of single quotation marks
Code 2-2
[Email protected]:/data# cat Demo2 #!/bin/bashecho ' print I don ' t know if this ' ll work ' for test in I don't know if this ' ll Workdo echo "Word: $test" Doneecho "——————————————————————————————" for test in I don\ ' t know if This\ ' ll workdo EC Ho "Word: $test" Doneecho "——————————————————————————————" for Test in I Don "'" t know if this "" ll Workdo echo "Word: $te St "Done[email protected]:/data#/demo2 print I don ' t know if this ' ll workword:Iword:dont know if Thisllword:work ————————— ————————————————————— Word:Iword:don ' tword:knowword:ifword:this ' llword:work —————————————————————————————— word:i Word:don ' Tword:knowword:ifword:this ' ll
The For loop is distinguished by a space, and can be enclosed in double quotation marks if a character containing a space is encountered
Code 2-3
[Email protected]:/data# cat Demo3 #!/bin/bashfor test in Nevada "New Hampshire" "New Mexico" "New York" does echo "now g oing to $test "Done[email protected]:/data#./demo3 now going to nevadanow going to new Hampshirenow going to new Mexiconow Going to New York
Reading a list from a variable
Code 2-4
[Email protected]:/data# cat Demo4
#!/bin/bash
list= "Alabama Alaska Arizona Arkansas California Colorado"
list= $list "Connecticut"
For the $list
Do
echo "Do you ever visited $state?"
Done
[Email protected]:/data#./demo4
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited California?
Have you ever visited Colorado?
Have you ever visited Connecticut?
Reading a value from a command
Code 2-5
[Email protected]:/data# cat State Alabama Alaska Arizona Arkansas California Colorado connecticut[email protected]:/data # cat Demo5 #!/bin/bashfile=statefor State in ' Cat $file ' do echo ' visit beautiful $state ' Done[email protected]:/data# ./demo5 visit Beautiful alabamavisit beautiful alaskavisit beautiful arizonavisit beautiful ARKANSASVISIT beautiful Calif Orniavisit Beautiful Coloradovisit Beautiful Connecticut
Modify the field delimiter, using ifs=$ '; '
Code 2-6
[Email protected]:/data# cat states Alabama; Alaska; Arizona; Arkansas; California; Colorado; Connecticut[email protected]:/data# cat Demo6 #!/bin/bashfile= "states" ifs=$ '; ' For the ' Cat $file ' do echo ' visit beautiful $state ' Done[email protected]:/data#./demo6 visit Beautiful Alabamavi Sit beautiful alaskavisit beautiful arizonavisit beautiful arkansasvisit beautiful californiavisit Beautiful Coloradovisit beautiful Connecticut
By default, the following characters are treated as field separators by the bash shell:
To read a directory with wildcard characters
Using the For loop command to automatically traverse a directory full of files, you must use a wildcard character in the file name or path name when you do this. It forces the shell to use file extension matching. File extension matching is the process of generating a file name or path name that matches a specified wildcard character
Code 2-7
[email protected]:/data# ls-l/home/software/apache-tomcat-1/total 120drwxr-xr-x 2 root root 4096 Nov bind Rwxr-xr-x 3 root root 4096 Nov confdrwxr-xr-x 2 root root 4096 Nov lib-rw-r--r--1 root root 56977 Nov 3 licensedrwxr-xr-x 2 root root 12288 Dec 3 01:21 logs-rw-r--r--1 root root 1397 Nov 3 notice-rw-r--r-- 1 root root 6779 3 release-notes-rw-r--r--1 root root 16204 Nov 3 running.txtdrwxr-xr-x 2 root root 4 096 tempdrwxr-xr-x 2 root root 4096 Nov webappsdrwxr-xr-x 3 root root 4096 Nov work[email protected]:/data# ls-l/home/software/apache-tomcat-1/conf/total 212drwxr-xr-x 3 root root 4096 Nov Cata LINA-RW-------1 root root 12374 Nov 3 catalina.policy-rw-------1 root root 6427 Nov 3 Catalina.properti ES-RW-------1 root root 1577 Nov 3 context.xml-rw-------1 root root 3387 Nov 3 logging.properties-rw-- -----1 root root 6491 SERVER.XML-RW-------1 root root 1744 Nov 3 tomcat-users.xml-rw-------1 root root 1846 Nov 3 2 014 TOMCAT-USERS.XSD-RW-------1 root root 163468 Nov 3 web.xml[email protected]:/data# cat Demo1 #!/bin/bashf or file in/home/software/apache-tomcat-1/*/home/software/apache-tomcat-1/conf/*do if [-D $file] Then echo "$file is a directory" elif [-F $file] then echoes "$file is a file" else echo "$file doesn ' t exists" fidone[email protected]:/data#./demo1/home/software/apache-t Omcat-1/bin is a directory/home/software/apache-tomcat-1/conf are a directory/home/software/apache-tomcat-1/lib is a Directory/home/software/apache-tomcat-1/license is a file/home/software/apache-tomcat-1/logs is a directory/home/ Software/apache-tomcat-1/notice is a file/home/software/apache-tomcat-1/release-notes is a file/home/software/ Apache-tomcat-1/running.txt is a file/home/software/apache-tomcat-1/temp is a directory/home/software/apache-tomcat-1/webapps is a directory/home/software/ Apache-tomcat-1/work is a directory/home/software/apache-tomcat-1/conf/catalina is a directory/home/software/ Apache-tomcat-1/conf/catalina.policy is a file/home/software/apache-tomcat-1/conf/catalina.properties is a file/home /software/apache-tomcat-1/conf/context.xml is a file/home/software/apache-tomcat-1/conf/logging.properties is a file /home/software/apache-tomcat-1/conf/server.xml is a file/home/software/apache-tomcat-1/conf/tomcat-users.xml is a File/home/software/apache-tomcat-1/conf/tomcat-users.xsd is a file/home/software/apache-tomcat-1/conf/web.xml is a File
Note: If the file name or directory name contains spaces, you will need to use double quotation marks [-D "$file] to determine whether the files or directories
C Language for loop style
Code 2-9
[Email protected]:/data# cat Demo2 #!/bin/bashfor (i=1;i<10;i++) do echo "The next number is $i" Doneecho "——————— ——————————————————— "for (a=1,b=10;a<10;a++,b--) do echo" $a-$b = ' expr $a-$b ' "Done[email protected]:/data#. Demo2 the next number is 1The next number are 2The Next number is 3The next number are 4The Next number is 5The next number Is 6The next number is 7The next number was 8The next number is 9 —————————————————————————— 1-10 =-92-9 = 73-8 = 54 -7 = 35-6 =-16-5 = 17-4 = 38-3 = 59-2 = 7
While command
The while command allows you to define a command to test and then loop through a set of commands as long as the defined test command returns exit status code 0. It tests the test command at the beginning of each iteration. When the test command returns a non-0 exit status code, the while command stops executing the set of commands
While Test command
Do
Other commands
Done
Code 2-10
[Email protected]:/data# cat Demo3 #!/bin/bashval=10while [$val-gt 0]do Echo $val ((val--)) Done[email protecte d]:/data#./demo3 10987654321
Using multiple test commands
While the command line allows you to define multiple test commands, only the exit status code of the last Test command is used to exit the loop (note: Multi-line commands must be separated by a carriage return)
Code 2-11
[Email protected]:/data# cat Demo4 #!/bin/bashval=10while echo $val [$val-gt 0]do echo "This is inside the Loop " ((val--)) Done[email protected]:/data#./demo4 10This is inside the loop9this are inside the loop8this is inside T He loop7this is inside the loop6this are inside the loop5this is inside the loop4this are inside the loop3this is inside the Loop2this is inside the loop1this is inside the loop0
Until command
The until command and the while command work exactly the opposite way. The until command requires specifying a test instruction that normally outputs a non-0 exit status code. Only the exit status code of the test command is not 0,bash the shell will specify those commands listed in the loop, and once the test command returns exit status code 0, the loop ends
Until test commands
Do
Other commands
Done
Code 2-12
[Email protected]:/data# cat Demo5 #!/bin/bashval=100until [$val-eq 0]do echo $val val=$[$val -25]done[email protected]:/data#./demo5 100755025
Nested loops
Code 2-13
[Email protected]:/data# cat Demo6 #!/bin/bashfor (a=1;a<=3;a++) do echo ' starting loop $a: ' For ((b=1;b <=3;b++) do echo "Inside loop: $b" donedone[email protected]:/data#./demo6 starting loop 1:inside loop : 1Inside loop:2inside loop:3starting loop 2:inside loop:1inside loop:2inside loop:3starting loop 3:inside loop:1Inside Lo Op:2inside Loop:3
To cycle through file data:
- Using Nested Loops
- Modifying IFS environment variables
Process file data in a loop
Code 2-14
[Email protected]:/data# cat passwd root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/ Nologinbin:x:2:2:bin:/bin:/usr/sbin/nologin[email protected]:/data# cat demo1 #!/bin/bashifs=$ ' \ n ' for entry in ' Cat passwd ' Do echo ' Values in $entry ' ifs=$ ': ' for value in $entry do echo ' $value ' donedone [Email protected]:/data#./demo1 Values in Root:x:0:0:root:/root:/bin/bash root x 0 0 Root /root /bin/bashvalues in daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin daemon x 1 1 daemon /usr/sbin /usr/sbin/nologinvalues in Bin:x:2:2:bin:/bin:/usr/sbin/nologin bin x 2 2 bin /bin /usr/sbin/nologin
Control loops:
- Break command
- Continue command
Code 2-15
[Email protected]:/data# cat Demo2 #!/bin/bashfor val in 1 2 3 4 5 6 7 8 9do if [$val-eq 6] then break Fi echo "Iteration number: $val" Doneecho "The For Loop is completed" [email protected]:/data#./demo2 Iteration Number: 1Iteration number:2iteration number:3iteration number:4iteration number:5the for loop is completed
Break N,n describes the loop level to jump out. By default, n is 1, which indicates that it jumps out of the current loop. If set to 2,break command stops the next level of external loops
Code 2-16
[Email protected]:/data# cat Demo3 #!/bin/bashfor ((a = 1;a<5;i++)) do echo ' Outer loop $a: ' For ((b=1;b<100 ; b++)) do if [$b-gt 6] then 2 fi echo "Inner loop: $b" donedone[email protected]:/ data#./demo3 Outer Loop 1:inner loop:1inner loop:2inner loop:3inner loop:4inner loop:5inner loop:6
Continue command
Code 2-17
[Email protected]:/data# cat Demo4 #!/bin/bashfor ((val=1;val<16;val++)) do if [$val-gt 5] && [$val-lt Then continue fi echo "Iteration number: $val" Done[email protected]:/data#./demo4 Iteration Number:1iteration number:2iteration number:3iteration number:4iteration number:5iteration number:11Iteration number : 12Iteration number:13iteration number:14iteration number:15
Like the break command, continue n also allows you to specify which level of loop to continue with the command parameter
Code 2-18
[Email protected]:/data# cat Demo5 #!/bin/bashfor (a=1;a<6;a++) do echo ' iteration $a: ' For ((b=1;b<3;b + +)) do if [$a-gt 2] && [$a-lt 5] then continue 2 fi val=$[$a * $b] echo "the Result of $a * $b = $val " donedone[email protected]:/data#./demo5 iteration 1:the Result of 1 * 1 = 1 The res Ult of 1 * 2 = 2 Iteration 2:the result of 2 * 1 = 2 The result of 2 * 2 = 4 Iteration 3:iteration 4:iteration 5:th e Result of 5 * 1 = 5
Processing the output of a loop
After the done command, you can add the redirected output to output the command to a file instead of the screen
Code 2-19
[Email protected]:/data# cat passwd root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/ Nologinbin:x:2:2:bin:/bin:/usr/sbin/nologin[email protected]:/data# Cat Demo6 #!/bin/bashfor file in ' Cat passwd ' do echo "The file content: $file" done > Text[email protected]:/data#/demo6 [email protected]:/data# cat text the FIL e content:root:x:0:0:root:/root:/bin/bashthe file Content:daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinthe file Content:bin:x:2:2:bin:/bin:/usr/sbin/nologin
The done command is useful for looping the results of a loop to another command, redirecting to a file or screen, and using other commands to display to the screen
[Email protected]:/data# cat Demo7 #!/bin/bashfor State in ' North Dakota ' Connecticut Illnois Alabama Tennesseedo Echo "$state is the next place to go" done | Sort > Textecho "This completes we travels" [email protected]:/data#./demo7 This completes our travels[email protected ]:/data# Cat Text Alabama is the next place to Goconnecticut are the next place to Goillnois are the next place to GoNorth D Akota is the next place to Gotennessee are the next place to go
Linux shell Scripting--Using structured commands (iii)