shell-----更多结构化命令
for命令
bash shell提供了for命令,允许你创建一个遍历一系列的循环。
for var in list do commands done
1、读取列表中的值
for命令最基本的用法就是遍历for命令自身所定义的一系列值。
[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
2、读取列表中的复杂值
如果列表中数值比较麻烦,for命令可能会识别异常。
[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循环假定每个值都是空格分割,如果要包含空格的数值,可以用””双引号。
3、从变量读取列表
可以将一些列的值储存在变量中,然后遍历变量中的整个列表。
[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
4、从命令读取值
[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
5、更改字段分隔符
IFS(内部字段分隔符)环境变量定义了bash shell用做字段分隔符的一些列字符,默认情况下,bash shell将以下字符当做字段分隔符:
1、空格
2、制表符
3、换行符
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=:
6、通配符读取目录
可以用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
控制循环
1、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!
2、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!