【Shell】Shell编程之for循环命令

bash shell提供了for命令,用于创建通过一系列值重复的循环,for命令的格式为:
for var in list
do
   commands
done
在参数list中提供了一系列用于迭代的 值,变量$var包含当前迭代的列表项值。

1、读取列表中的值
for命令的最基本用法是通过在for命令中定义一列值来迭代:
[root@strong bash_stu]# cat test1.sh 
#!/bin/bash
#basic for command

for var in China Japan USA UK Canada 
do
   echo The next contry is $var
done

[root@strong bash_stu]# . test1.sh 
The next contry is China
The next contry is Japan
The next contry is USA
The next contry is UK
The next contry is Canada
[root@strong bash_stu]# 
最后一次迭代后,变量$var在shell脚本的其他地方仍然有效,除非改变它的值。
[root@strong bash_stu]# cat test1.sh 
#!/bin/bash

#basic for command

for var in China Japan USA UK Canada 
do
   echo The next contry is $var
done

echo The last value for contry is $var

var=Alen

echo The last value is $var
[root@strong bash_stu]# . test1.sh 
The next contry is China
The next contry is Japan
The next contry is USA
The next contry is UK
The next contry is Canada
The last value for contry is Canada
The last value is Alen
[root@strong bash_stu]# 
2、读取列表中的复杂值
for循环每个值用空格分割,当列表中有单引号或空格时,遍历时会出现错误的结果:
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

for var in Welcome to New York It's my baby's dog  -- New York为一个整体
do
   echo The word is $var
done
[root@strong bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is New
The word is York
The word is Its my babys
The word is dog
[root@strong bash_stu]# 
显示的结果不是我们想要的,有两种方法可以解决这个问题:
  • 使用转义字符(\);
  • 使用双引号定义使用单引号的值。
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

#basic for command

for var in Welcome to "New York" It\'s my "baby's" dog  --分别使用双引号和转义解决
do
   echo The word is $var
done
[root@strong bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is New York
The word is It's
The word is my
The word is baby's
The word is dog
[root@strong bash_stu]# 
3、从变量读取列表
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

#basic for command

list="Welcome to learn shell"
list=$list" Alen"  --将一个变量增加到一个已存在的变量中

for var in $list  
do
   echo The word is $var
done
[root@strong bash_stu]# . test2.sh 
The word is Welcome
The word is to
The word is learn
The word is shell
The word is Alen
[root@strong bash_stu]# 
4、读取命令中的值
使用反引号可以读取命令的结果,例如:
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

f="country.txt"

for var in `cat $f`  
do
   echo The word is $var
done
[root@strong bash_stu]# cat country.txt 
China
Japan
USA
UK
[root@strong bash_stu]# . test2.sh 
The word is China
The word is Japan
The word is USA
The word is UK
[root@strong bash_stu]# 
5、改变字段分隔符
环境变量IFS(内部字段分隔符)定义了shell用作字段分隔符的字符列表,默认情况下,shell将下面的字符看做字段分隔符:空格、制表符和换行符。
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

f="country.txt"

IFS=$'\n'  --重新定义字段分隔符,忽略空格和制表符
for var in `cat $f`  
do
   echo The word is $var
done
[root@strong bash_stu]# . test2.sh 
The word is China Beijing
The word is Japan 
The word is USA New York
The word is UK
[root@strong bash_stu]# 
为了能使IFS值可以返回默认值,可以改为如下方式:
IFS.OLD=$IFS
IFS=$'\n'
<使用新的IFS处理语句>
IFS=$IFS.OLD
6、使用通配符读取目录
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash
#basic for command

for var in /root/bash_stu/* 
do
   if [ -d $var ]
   then
      echo "$var is a directory"
   elif [ -f $var ]
   then
      echo "$var is a file"
   fi
done
[root@strong bash_stu]# . test2.sh 
/root/bash_stu/country.txt is a file
/root/bash_stu/test1.sh is a file
/root/bash_stu/test2.sh is a file
/root/bash_stu/tt is a directory
[root@strong bash_stu]# 
7、C 语言中的for命令
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

v=1;
for((i=1;i<5;i++))
do
  echo $v + $i = $(($v+$i))  
done
[root@strong bash_stu]# . test2.sh 
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
1 + 4 = 5
[root@strong bash_stu]# 
8、for循环使用多个变量
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1,j=5;i<5;i++,j--))
do
  echo $i + $j = $(($i+$j))  
done
[root@strong bash_stu]# . test2.sh 
1 + 5 = 6
2 + 4 = 6
3 + 3 = 6
4 + 2 = 6
[root@strong bash_stu]# 
9、练习,输出100之内能被3整除的数
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

for ((i=1;i<=100;i++))
do
  if [ $(($i%3)) -eq 0 ]
  then
    echo "$i"
  fi
done
[root@strong bash_stu]# . test2.sh 
3
6
9
12
15
18
21
24
27
--------------------省略---------------------
10、练习嵌套循环,打印9*9以内乘法表
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1;i<=9;i++))
do
  for((j=1;j<=i;j++))
  do
     echo -n "$i*$j=$(($i*$j))   "
  done
  echo ""
done
[root@strong bash_stu]# . test2.sh 
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
[root@strong bash_stu]# 
11、处理循环的输出
可以使用管道或者重定向循环输出结果,也可以在done后添加处理命令做到。
[root@strong bash_stu]# cat test2.sh 
#!/bin/bash

for((i=1;i<=9;i++))
do
  for((j=1;j<=i;j++))
  do
     echo -n "$i*$j=$(($i*$j))   "
  done
  echo ""
done > mu.txt
[root@strong bash_stu]# . test2.sh 
[root@strong bash_stu]# cat mu.txt 
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
[root@strong bash_stu]# 



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值