for命令
bash shell提供了for命令,容许你建立一个遍历一系列的循环。node
for var in list do commands done
一、读取列表中的值
for命令最基本的用法就是遍历for命令自身所定义的一系列值。shell
[root@node1 ljy]# more ceshi.sh #!/bin/bash for test in football basketball volleyball do echo "you like $test" done [root@node1 ljy]# sh ceshi.sh you like football you like basketball you like volleyball
二、读取列表中的复杂值
若是列表中数值比较麻烦,for命令可能会识别异常。bash
[root@node1 ljy]# more ceshi.sh #!/bin/bash for test in I don\'t know "new york" do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:I word:don't word:know word:new york
可使用转义字符(\)或者双引号来定义用到单引号的值。测试
for循环假定每一个值都是空格分割,若是要包含空格的数值,能够用""双引号。this
三、从变量读取列表
能够将一些列的值储存在变量中,而后遍历变量中的整个列表。spa
[root@node1 ljy]# more ceshi.sh #!/bin/bash list="baketball football volleyball" for test in $list do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:baketball word:football word:volleyball
四、从命令读取值
[root@node1 ljy]# more one.txt football basketball volleyball [root@node1 ljy]# more ceshi.sh #!/bin/bash file=/ljy/one.txt for test in $(cat $file) do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:football word:basketball word:volleyball
五、更改字段分隔符
IFS(内部字段分隔符)环境变量定义了bash shell用作字段分隔符的一些列字符,默认状况下,bash shell将如下字符当作字段分隔符:code
一、空格blog
二、制表符for循环
三、换行符class
IFS=$'\n'这个语句表示bash shell会在数据中忽视空格和制表符。
[root@node1 ljy]# more one.txt football basketball volleyball new york [root@node1 ljy]# more ceshi.sh #!/bin/bash IFS=$'\n' file=/ljy/one.txt for test in $(cat $file) do echo "word:$test" done [root@node1 ljy]# sh ceshi.sh word:football word:basketball word:volleyball word:new york
假如你遍历一个文件用冒号分隔的值,能够设置为IFS=:
六、通配符读取目录
能够用for命令来遍历目录中的文件,进行操做时必须在文件名或路径中加入通配符*。他会强制shell使用文件扩展匹配。
[root@node1 ljy]# more ceshi.sh #!/bin/bash file=/ljy/* for file in /home/* do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] then echo "$file is a file" fi done [root@node1 ljy]# sh ceshi.sh /home/123 is a file /home/lisi is a file /home/ljy is a directory /home/test.sh is a file /home/zhangsan is a directory
在Linux中容许目录或者文件名中包含空格,要适应这种状况,应该讲$file变量用双引号圈起来,若是不这么作可能包含空格的目录会有问题。
while命令
while明亮的基本格式:
while test command do other commands done
只要while测试条件成立,while命令就会不停的循环执行下去
实例:
[root@node1 ljy]# more ceshi.sh #!/bin/bash var=10 while [ $var -gt 2 ] do echo "$var" var=$[ $var - 1 ] done [root@node1 ljy]# sh ceshi.sh 10 9 8 7 6 5 4 3
until命令
一旦测试命令成立循环结束。
[root@node1 ljy]# more ceshi.sh #!/bin/bash var=10 until [ $var -eq 2 ] do echo "$var" var=$[ $var - 1 ] done [root@node1 ljy]# sh ceshi.sh 10 9 8 7 6 5 4 3
嵌套循环
循环语句能够在循环内使用任意类型的命令,包括其余循环命令,这种循环叫嵌套循环。
实例1:
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<5;a++)) do echo "this is $a" for (( b=1;b<4;b++ )) do echo " this is $b" done done [root@node1 ljy]# sh ceshi.sh this is 1 this is 1 this is 2 this is 3 this is 2 this is 1 this is 2 this is 3 this is 3 this is 1 this is 2 this is 3 this is 4 this is 1 this is 2 this is 3
循环处理文件数据
经过IFS环境变量,强制for循环将文件中的每行当成一个条目来处理。
[root@node1 ljy]# more ceshi.sh #!/bin/bash IFS=$'\n' for test in $(cat /etc/passwd) do echo "values is $test----" IFS=: for value in $test do echo "$value" done done [root@node1 ljy]# sh ceshi.sh values is root:x:0:0:root:/root:/bin/bash---- root x 0 0 root /root /bin/bash values is bin:x:1:1:bin:/bin:/sbin/nologin---- bin x 1 1 bin /bin /sbin/nologin
控制循环
一、break命令
能够用break命令来跳出任意循环,包括while和until循环
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<9;a++ )) do if [ $a -eq 5 ] then break fi echo "number is $a!" done echo "completed!" [root@node1 ljy]# sh ceshi.sh number is 1! number is 2! number is 3! number is 4! completed!
二、continue
continue命令能够提早停止某次循环中的命令,但并不会彻底停止整个循环。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for (( a=1;a<19;a++ )) do if [ $a -gt 5 ]&&[ $a -lt 10 ] then continue fi echo "number is $a!" done echo "completed!" [root@node1 ljy]# sh ceshi.sh number is 1! number is 2! number is 3! number is 4! number is 5! number is 10! number is 11! number is 12! number is 13! number is 14! number is 15! number is 16! number is 17! number is 18! completed!
处理循环的输出
shell能够将for命令的结果重定向到一个新的文件中,而不是显示在屏幕上。
[root@node1 ljy]# more ceshi.sh #!/bin/bash for file in /root/* do if [ -d $file ] then echo "$file is a directory!" elif [ -f $file ] then echo "$file is a file!" fi done > out.txt [root@node1 ljy]# sh ceshi.sh [root@node1 ljy]# more out.txt /root/anaconda-ks.cfg is a file! /root/ceshi is a file! /root/one is a file! /root/test.txt is a file!