12个例子写的很全,转自
基本格式1,用for in:
for varname in list do command1 command2 .. done
基本格式2,c语言形式:
for (( expr1; expr2; expr3 )) do command1 command2 .. done
1. Static values for the list after “in” keyword直接在”in”后面接值
In the following example, the list of values (Mon, Tue, Wed, Thu and Fri) are directly given after the keyword “in” in the bash for loop.在下面的例子中,变量为day,在Mon,Tue,Wed,Thu和Fri中取值。
$ cat for1.sh i=1 for day in Mon Tue Wed Thu Fri do echo "Weekday $((i++)) : $day" done $ ./for1.sh Weekday 1 : Mon Weekday 2 : Tue Weekday 3 : Wed Weekday 4 : Thu Weekday 5 : Fri
Caution: The list of values should not be separated by comma (Mon, Tue, Wed, Thu, Fri). The comma will be treated as part of the value. i.e Instead of “Mon”, it will use “Mon,” as value as shown in the example below.注意点:五个不同的值不能用逗号分隔,例如Mon,
因为会自动把逗号算入到Mon
之内。测试如下:
$ cat for1-wrong1.sh i=1 for day in Mon, Tue, Wed, Thu, Fri do echo "Weekday $((i++)) : $day" done $ ./for1-wrong1.sh Weekday 1 : Mon, Weekday 2 : Tue, Weekday 3 : Wed, Weekday 4 : Thu, Weekday 5 : Fri
Caution: The list of values should not be enclosed in a double quote. (“Mon Tue Wed Thu Fri”). If you enclose in double quote, it will be treated as a single value (instead of 5 different values), as shown in the example below.注意:五个值不可以用双引号括起来,这会被认为只有一个值
$ cat for1-wrong2.sh i=1 for day in "Mon Tue Wed Thu Fri" do echo "Weekday $((i++)) : $day" done $ ./for1-wrong2.sh Weekday 1 : Mon Tue Wed Thu Fri
2. Variable for the list after “in” keyword在”in”后面接变量
Instead of providing the values directly in the for loop, you can store the values in a variable, and use the variable in the for loop after the “in” keyword, as shown in the following example.在使用前把值赋给变量,在”in”后面接变量。
$ cat for2.sh i=1 weekdays="Mon Tue Wed Thu Fri" for day in $weekdays do echo "Weekday $((i++)) : $day" done $ ./for2.sh Weekday 1 : Mon Weekday 2 : Tue Weekday 3 : Wed Weekday 4 : Thu Weekday 5 : Fri
Caution: As a best practice, you should always quote the bash variables when you are referring it. There are few exceptions to this best practice rule. This is one of them. If you double quote the variable in this for loop, the list of values will be treated as single value. Lot of people fall into this trap. Be careful and do not double quote your variable in the for loop.注意:作为一个良好的习惯,请每次都将值用变量来代替。这里的变量不能用双引号括起来,它会将其视为一个值。测试如下:
$ cat for2-wrong.sh i=1 weekdays="Mon Tue Wed Thu Fri" for day in "$weekdays" do echo "Weekday $((i++)) : $day" done $ ./for2-wrong.sh Weekday 1 : Mon Tue Wed Thu Fri
3. Don’t specify the list; get it from the positional parameters不指定列表
If you don’t specify the keyword “in” followed by any list of values in the bash for loop, it will use the positional parameters (i.e the arguments that are passed to the shell script).如果in中不加变量,则在执行时加上即可,例如:
$ cat for3.sh i=1 for day do echo "Weekday $((i++)) : $day" done $ ./for3.sh Mon Tue Wed Thu Fri Weekday 1 : Mon Weekday 2 : Tue Weekday 3 : Wed Weekday 4 : Thu Weekday 5 : Fri
Caution: Please be careful if you use this method. You should not include the keyword “in” in the for loop. If you leave the keyword “in” without any values, it will not use the positional parameter as shown below. It will not go inside the loop. i.e for loop will never get executed as shown in the example below.使用这种方法一定要小心,保证 in 这个字符都不出现,下面的例子就是没有输出任何信息。
$ cat for3-wrong.sh i=1 for day in do echo "Weekday $((i++)) : $day" done $ ./for3-wrong.sh Mon Tue Wed Thu Fri
Note: Refer to our earlier article to understand more about bash positional parameters.可以查看bash位置参数。
4. Unix command output as list values after “in” keyword以命令的输出做为列表
You can use the output of any UNIX / Linux command as list of values to the for loop by enclosing the command in back-ticks ` ` as shown below.使用反引号将要执行的命令引起来,可使结果作为列表。
$ cat for4.sh i=1 for username in `awk -F: '{print $1}' /etc/passwd` do echo "Username $((i++)) : $username" done $ ./for4.sh Username 1 : ramesh Username 2 : john Username 3 : preeti Username 4 : jason ..
5. Loop through files and directories in a for loop使用当前目录或文件做列表
To loop through files and directories under a specific directory, just cd to that directory, and give * in the for loop as shown below.定位到指定目录下,用*代替列表。
The following example will loop through all the files and directories under your home directory.下面的例子使用了/home目录。
$ cat for5.sh i=1 cd ~ for item in * do echo "Item $((i++)) : $item" done $ ./for5.sh Item 1 : positional-parameters.sh Item 2 : backup.sh Item 3 : emp-report.awk Item 4 : item-list.sed Item 5 : employee.db Item 8 : storage Item 9 : downloads
Usage of * in the bash for loop is similar to the file globbing that we use in the linux command line when we use ls command (and other commands).上面的例子显示类似于ls的执行结果。
For example, the following will display all the files and directories under your home directory. This is the concept that is used in the above for5.sh example.
cd ~ ls *
The following will display all the *.conf file that begins with either a, b, or, c or d under /etc directory.下面显示以a,b,c或d开头,以.conf结尾的文件
$ ls -1 /etc/[abcd]*.conf /etc/asound.conf /etc/autofs_ldap_auth.conf /etc/cas.conf /etc/cgconfig.conf /etc/cgrules.conf /etc/dracut.conf
The same argument that is used in the ls command above, can be used in a bash for loop, as shown in the example below.上面的可直接作为列表
$ cat for5-1.sh i=1 for file in /etc/[abcd]*.conf do echo "File $((i++)) : $file" done $ ./for5-1.sh File 1 : /etc/asound.conf File 2 : /etc/autofs_ldap_auth.conf File 3 : /etc/cas.conf File 4 : /etc/cgconfig.conf File 5 : /etc/cgrules.conf File 6 : /etc/dracut.conf
6. Break out of the for loop(for循环中的break)
You can break out of a for loop using ‘break’ command as shown below.可以使用如下的方式,中断for循环。
$ cat for6.sh i=1 for day in Mon Tue Wed Thu Fri do echo "Weekday $((i++)) : $day" if [ $i -eq 3 ]; then break; fi done $ ./for6.sh Weekday 1 : Mon Weekday 2 : Tue
7. Continue from the top of the for loop(for循环中的continue)
Under certain conditions, you can ignore the rest of the commands in the for loop, and continue the loop from the top again (for the next value in the list), using the continue command as shown below.在有些情况下,可以忽略某些之后再回到循环中,使用continue即可做到。
The following example adds “(WEEKEND)” to Sat and Sun, and “(weekday)” to rest of the days.当循环遇到i=7 或i=8时,显示WEEKEND,然后继续。
$ cat for7.sh i=1 for day in Mon Tue Wed Thu Fri Sat Sun do echo -n "Day $((i++)) : $day" if [ $i -eq 7 -o $i -eq 8 ]; then echo " (WEEKEND)" continue; fi echo " (weekday)" done $ ./for7.sh Day 1 : Mon (weekday) Day 2 : Tue (weekday) Day 3 : Wed (weekday) Day 4 : Thu (weekday) Day 5 : Fri (weekday) Day 6 : Sat (WEEKEND) Day 7 : Sun (WEEKEND)
8. Bash for loop using C program syntax使用C语言for循环语法
This example uses the 2nd method of bash for loop, which is similar to the C for loop syntax. The following example generates 5 random number using the bash C-style for loop.在文章开头有使用c语言的for循环,下面的例子生成出1-5个数字
$ cat for8.sh for (( i=1; i <= 5; i++ )) do echo "Random number $i: $RANDOM" done $ ./for8.sh Random number 1: 23320 Random number 2: 5070 Random number 3: 15202 Random number 4: 23861 Random number 5: 23435
9. Infinite Bash for loop无限循环
When you don’t provide the start, condition, and increment in the bash C-style for loop, it will become infinite loop. You need to press Ctrl-C to stop the loop.当不设定起始,条件和步长的时候,会变为无限循环,只能通过Ctrl-C来停止。
$ cat for9.sh i=1; for (( ; ; )) do sleep $i echo "Number: $((i++))" done
Like we said above, press Ctrl-C to break out of this bash infinite for loop example.
$ ./for9.sh Number: 1 Number: 2 Number: 3
10. Using comma in the bash C-style for loop在c语言类似的循环中使用逗号
In the bash c-style loop, apart from increment the value that is used in the condition, you can also increment some other value as shown below.在c语言类似的循环中,可以将增长和变量分开。
In the initialize section, and the increment section of the bash C-style for loop, you can have multiple value by separating with comma as shown below.
The following for loop is executed a total of 5 times, using the variable i. However the variable j start with 5, and getting increment by 5 every time the loop gets executed.下面的例子将执行5次,但是目标是改变j的值,每一次加5
$ cat for10.sh for ((i=1, j=10; i <= 5 ; i++, j=j+5)) do echo "Number $i: $j" done $ ./for10.sh Number 1: 10 Number 2: 15 Number 3: 20 Number 4: 25 Number 5: 30
11. Range of numbers after “in” keyword在in列表中使用数字区间
You can loop through using range of numbers in the for loop “in” using brace expansion.
The following example loops through 10 times using the values 1 through 10.下面的例子取值1到10,用大括号括起来。
$ cat for11.sh for num in {1..10} do echo "Number: $num" done $ ./for11.sh Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 ...
12. Range of numbers with increments after “in” keyword在in列表中使用数字区间,并以2做为步长。
The following example loops through 5 times using the values 1 through 10, with an increment of 2. i.e It starts with 1, and keeps incrementing by 2, until it reaches 10.
$ cat for12.sh for num in {1..10..2} do echo "Number: $num" done $ ./for12.sh Number: 1 Number: 3 Number: 5 Number: 7 Number: 9
12个for循环的例子很基础很规范,多看优质代码才能更快成长。
Leave a Reply