2012年1月18日 星期三

[Linux 文章收集] Bash For Loop Examples

轉載自 這裡 
Preface : 
A ‘for loop’ is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop. A for loop can be used at a shell prompt or within a shell script itself. 

for loop syntax : 
Numeric ranges for syntax is as follows : 
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN

done

This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop : 
- test1.sh :
  1. #!/bin/sh  
  2. for i in 1 2 3 4 5  
  3. do  
  4.         echo "Welcome $i times"  
  5. done  

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). It can be done easily with seq command. A representative example in bash as follows : 
- test2.sh :
  1. #!/bin/sh  
  2. for i in $(seq 1 2 20)  
  3. do  
  4.         echo "Welcome $i times"  
  5. done  

Latest bash version 3.0+ has inbuilt support for setting up ranges : 
- test3.sh :
  1. #!/bin/sh  
  2. for i in {1..5}  
  3. do  
  4.         echo "Welcome $i times"  
  5. done  

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax : 
- test4.sh :
  1. #!/bin/sh  
  2. echo "Bash version ${BASH_VERSION}..."  
  3. for i in {0..10..2}  
  4. do  
  5.         echo "Welcome $i times"  
  6. done  

上面執行結果 : 
$ ./test4.sh
Bash version 4.2.8(1)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

Three-expression bash for loops syntax : 
This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3). 
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3

done

A representative three-expression example in bash as follows : 
- test5.sh :
  1. #!/bin/sh  
  2. for (( c=1; c<=5; c++ ))  
  3. do  
  4.         echo "Welcome $c times..."  
  5. done  

How do I use for as infinite loops ? 
Infinite for loop can be created with empty expressions, such as : 
  1. #!/bin/bash  
  2. for (( ; ; ))  
  3. do  
  4. echo “infinite loops [ hit CTRL+C to stop]“  
  5. done  
Conditional exit with break : 
You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break : 
- test6.sh :
  1. #!/bin/sh  
  2. for i in 1 2 3 4 5  
  3. do  
  4.         echo "Test $i..."  
  5.         if [ "$i" -eq "3" ]; then  
  6.                 echo "Break!"  
  7.                 break  
  8.         fi  
  9. done  

執行結果 : 
$ ./test6.sh
Test 1...
Test 2...
Test 3...
Break!

Early continuation with continue statement : 
To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement. 
- test7.sh :
  1. #!/bin/sh  
  2. for i in {1..10}  
  3. do  
  4.         if [ "$i" -eq "5" ]; then  
  5.                 echo "Continue...(Skip 5)"  
  6.                 continue  
  7.         fi  
  8.         echo "Test $i..."  
  9. done  

執行結果 : 
$ ./test7.sh
Test 1...
Test 2...
Test 3...
Test 4...
Continue...(Skip 5)
Test 6...
Test 7...
Test 8...
Test 9...
Test 10...

Supplement 
nixCraft - Bash For Loop Examples

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...