Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146451
  • 博文数量: 25
  • 博客积分: 165
  • 博客等级: 入伍新兵
  • 技术积分: 460
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-15 19:24
文章分类

全部博文(25)

文章存档

2013年(21)

2012年(4)

我的朋友

分类: LINUX

2013-09-24 10:53:21

Abstract:
1) for 循环与列表分隔符;
2)while 循环;
3)until 循环;

1. for 循环与列表分隔符:
Linux shell 中 for 循环用于处理列表的循环遍历;
1.1 for循环的用法:

  1. for var in list#list 可以是一个字符串或者一个字符序列
  2. do
  3. 执行代码; #使用$var变量对list进行遍历
  4. done
example:

  1. echo -e "\e[42;31m --- Iterate the contents of out.txt!\e[0m";
  2. echo 1 > out.txt;
  3. echo 2 >> out.txt;
  4. echo 3 >> out.txt;
  5. echo 4 >> out.txt;
  6. echo 5 >> out.txt;
  7. counter=0;
  8. for item in $(cat out.txt);
  9. do
  10. echo "Item: $item";
  11. let counter++;
  12. done
  13. echo "counter:$counter";


1.2 for 循环的列表分隔符:
Linux shell 里面有个十分重要的环境变量 IFS (Internal Field Separator),这个变量用作处理字符串时的默认分隔字符;
先上代码,看看这个 $IFS 到底是何方神圣!

example 1:
  1. echo -e "\e[42;31m --- Use default \$IFS to iterate the contents of out.txt! ---\e[0m";
  2. echo -e "\e[1;33mDefault \$IFS=\"$IFS\"\e[0m";
  3. counter=0;
  4. for item in $(cat out.txt);
  5. do
  6. echo "Item: $item";
  7. let counter++;
  8. done
  9. echo "counter:$counter";

由上面黄色的字体的输出可见,$IFS 的默认值为一个换行字符;
因为 out.txt 使用 echo 进行初始化的,共有 5 行,所以我们看到当我们使用默认的 $IFS 分隔符对 cat out.txt 的标准输出 stdout 进行遍历时,一共循环了 5 次;

example 2:
  1. echo -e "\e[42;31m --- Change the internal field seperator \$IFS to comma!\e[0m";
  2. echo -e "\e[1;32m --- Use comma as \$IFS to iterate the contents of out.txt! ---\e[0m";
  3. counter=0;
  4. oldIFS=$IFS;
  5. IFS=,;
  6. for item in $(cat out.txt);
  7. do
  8. echo "Item: $item";
  9. let counter++;
  10. done
  11. IFS=$oldIFS;
  12. echo "counter:$counter";

由本次的运行结果看出,循环运行了一次,“Item”输出了一次;
因为本次代码里面我们把 $IFS 的值改成了 “," ,而 cat out.txt 的输出并不带逗号,所以就把所有输出作为一个整体输出;
因此我们只看到循环进行了一次;

example 3:
  1. echo -e "\e[1;32m --- Use comma as \$IFS to iterate a custom list! ---\e[0m";
  2. counter=0;
  3. oldIFS=$IFS
  4. IFS=,
  5. data="name,sex,rollno,location"
  6. for item in $data;
  7. do
  8. echo "Item: $item";
  9. let counter++;
  10. done
  11. IFS=$oldIFS
  12. echo "counter:$counter";

这次我们使用"," 作为 $IFS 的值去遍历一个以","作为分隔符的列表 data;
由运行结果看出,循环进行了 5 次;

1.3 为 for 构建循环条件列表:
for 循环里面的 list 可以自行构建,构建方法如下:

  1. {1..50} or {a..z} or {A..Z}
example:
for 循环遍历数字与字母构成的列表:

  1. echo -e "\e[42;31m--- for iteration in one number sequence! ---\e[0m";
  2. counter=0;
  3. for i in {1..5};
  4. do
  5. echo $i;
  6. let counter++;
  7. done;
  8. echo "counter=$counter";

  9. echo -e "\e[42;31m--- for iteration in one characters sequence! ---\e[0m";
  10. counter=0;
  11. for i in {A..z};
  12. do
  13. printf "$i";
  14. let counter++;
  15. done;
  16. echo;
  17. echo "counter:$counter";


1.4 for 同样支持 C 语言风格的循环:
用法:

  1. for((i=0;i<10;i++))
  2. {
  3. 执行代码;
  4. }
这里值得注意的是 for 的循环条件使用双括号 (()) ;
example:

  1. echo -e "\e[42;31m--- for iteration in \"C\" style! ---\e[0m";
  2. for(( i = 0 ,counter = 0;i<=10; i++,counter++))
  3. {
  4.     printf "%d " $i;
  5. }
  6. echo;
  7. echo "counter:$counter";


2. while 循环:
Linux shell 支持 while 风格的循环,只要循环条件为真,就不断循环执行,用法如下:

  1. while 循环条件
  2. do
  3. 执行代码;
  4. done
example:

  1. echo -e "\e[42;31m--- while iteration for counter! ---\e[0m";
  2. counter=0;
  3. while [ $counter -lt 5 ];
  4. do
  5. let counter++;
  6. done
  7. echo "counter:$counter";


3. until 循环:
until 循环是我在 linux shell 中第一次见到的一种特殊循环,循环代码会一直执行,直到循环条件为真才停止;
用法如下:

  1. until 循环停止条件;
  2. 执行代码;
  3. done
example:

  1. echo -e "\e[42;31m--- until iteration for counter! ---\e[0m";
  2. counter=0;
  3. until [ $counter -eq 7 ];
  4. do
  5. let counter++;
  6. printf "%d " $counter;
  7. done
  8. echo -e "\ncounter:$counter";
阅读(2639) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~