bash shell笔记3 结构化命令二

原创作品,允许转载,转载时请务必以超链接形式标明文章   原始出处  、作者信息和本声明。否则将追究法律责任。 http://twentyfour.blog.51cto.com/945260/513601

三、更多结构化命令
前面已经讲述了检查命令的输出和变量的值来操作shell脚本程序中的流。如下主要说明如何执行重复的过程和命令,使得一组命令循环下去,直到满足特定的条件。
知识内容:
# 使用for语句循环 
# 使用until语句迭代
# 使用while语句
# 结合循环
# 重定向循环输出
1、for命令
重复一系列的命令是常见的编程实践,对于shell如处理目录下的所有文件、系统中的所有用户、或者文本文件中的所有行。对于bash shell的for循环命令格式:
for xxx in xx
do
  command
done
在xx参数里头提供一系列用户迭代的值,如下例:
1.1、读取列表中的值
[root@wzp ~]# cat test3 
#!/bin/bash
for test in CCNA CCNP RHCE OCP OCM
do 
  echo the learning list is $test
done
[root@wzp ~]# ./test3 
the learning list is CCNA
the learning list is CCNP
the learning list is RHCE
the learning list is OCP
the learning list is OCM
上面的结果应该很好理解,有点还要说明的是迭代的最后一个值仍然有效,我可以通过在done后面重新显示出来,当然也可以再次修改它:
[root@wzp ~]# cat test3 
#!/bin/bash
for test in CCNA CCNP RHCE OCP OCM
do 
  echo the learning list is $test
done
echo the last list is $test
test='good job'
echo but the last point is $test
[root@wzp ~]# ./test3 
the learning list is CCNA
the learning list is CCNP
the learning list is RHCE
the learning list is OCP
the learning list is OCM
the last list is OCM
but the last point is good job
可以看到最后的迭代值是OCM,重新赋值可以被修改的!
1.2、读取列表中的复杂值
如果列表中出现一些单引号,可以通过转义符号(反斜杠符号)或者双引号来定义单引号的值;还有就是如果要处理空格问题,可以通过双引号括起来,实现最终效果,如下例:
[root@wzp ~]# cat test3 
#!/bin/bash
for cities in guang zhou shang hai bei jing
do 
 echo my favorite city is $cities
done
[root@wzp ~]# ./test3 
my favorite city is guang
my favorite city is zhou
my favorite city is shang
my favorite city is hai
my favorite city is bei
my favorite city is jing
很明显,这不是我要的结果,城市都被乱切了~
所以可以通过灵活使用双引号解决,如下:
[root@wzp ~]# cat test3 
#!/bin/bash
for cities in "guang zhou" "shang hai" "bei jing"
do 
 echo my favorite city is $cities
done
[root@wzp ~]# ./test3 
my favorite city is guang zhou
my favorite city is shang hai
my favorite city is bei jing
1.3、读取命令中的值
生成in后面这个列表的值可以通过使用命令的输出,但这里需要协助`这个少见但是shell中常用的反引号!看下例子:
[root@wzp ~]# cat somefile 
aa
bb
cc
dd
首先查看somefile这个文本文件内容
[root@wzp ~]# cat test3 
#!/bin/bash
file=somefile
for words in `cat $file`
do 
  echo the word is $words
done
[root@wzp ~]# ./test3 
the word is aa
the word is bb
the word is cc
the word is dd
执行后可以理解到,列表时调用了somefile文件的每一行,注意somefile所处的位置是shell脚本中可以调用的,不然必须使用绝对路径或相对路径了。
这里我故意把somefile文件mv到别的地方去,结果执行文件是报错:
[root@wzp ~]# ./test3 
cat: somefile: 没有那个文件或目录
1.4、使用通配符读取目录
文件通配是生产与指定通配符匹配的文件或者路径名的过程,如下例:
[root@wzp test]# pwd
/root/test
[root@wzp test]# touch aa bb cc
[root@wzp test]# mkdir dd ee ff
先创建三个文件格三个目录
[root@wzp ~]# cat test3
#!/bin/bash
for file in $HOME/test/*
do
 if [ -d "$file" ]
 then
    echo "$file is a directory"
 elif [ -f "$file" ]
 then
    echo "$file is a file"
 fi
done
[root@wzp ~]# ./test3
/root/test/aa is a file
/root/test/bb is a file
/root/test/cc is a file
/root/test/dd is a directory
/root/test/ee is a directory
/root/test/ff is a directory
通过if-then即可判断通配符*下的类型了~~
2、C语言式的for命令
这里顺便提及一下C语言式的for命令,在bash shell中也可执行。
不过C语言的for命令是通过指定变量为true值用于继续迭代的条件,当特定的条件为false时候就停止了循环,先看一个例子:
[root@wzp ~]# cat test3 
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
 echo the next num is $i
done
[root@wzp ~]# ./test3 
the next num is 1
the next num is 2
the next num is 3
the next num is 4
the next num is 5
the next num is 6
the next num is 7
the next num is 8
the next num is 9
the next num is 10
通过判断变量是否为true执行循环,这里的双括号有点似曾相识,不说,你懂得!~~
3、while命令
while命令有点如if-then和for循环的结合,while通过定义要测试的命令,如果命令返回0则循环命令,如果返回非0则停止了命令集。其格式为:
while xxx
do
  other xx
done
先来看一个例子:
[root@wzp ~]# cat test3 
#!/bin/bash
var=8
while [ $var -gt 0 ]
do 
  echo $var
  var=$[ $var -1 ]
done
[root@wzp ~]# ./test3 
8
7
6
5
4
3
2
1
当变量从8变到0不再大于0的时候则停止的循环
4、until命令
until命令刚好和while相反,until命令的测试命令退出状态非0,bash shell就执行列在循环中的命令,一旦测试条件返回0,就停止了循环,其格式:
until xxx
do 
 other xx
done
如上看起来跟while很相似,其实用法也很相似的!
先看下面的例子:
[root@wzp ~]# cat test3 
#!/bin/bash
var=24
until [ $var -eq 0 ]
do 
  echo $var
  var=$[ $var -8 ]
done
[root@wzp ~]# ./test3 
24
16
8
我拿24跟0比较是否相等,然后一直减去8,使得变量为0,则停止了循环。
5、嵌套循环
一条循环命令可以在循环中使用任何类型的命令,包括其他循环,比如在for循环中嵌套另一个for循环,或者在一个for循环中嵌套一个while循环或until循环。
下面举两个例子:
5.1、for循环嵌套到for循环
[root@wzp ~]# cat test3 
#!/bin/bash
for (( a=1; a<=3; a++ ))
do
 echo outside loop is $a:
 for (( b=1; b<=3; b++ ))
 do
   echo inside loop is $b:
 done
done
[root@wzp ~]# ./test3 
outside loop is 1:
inside loop is 1:
inside loop is 2:
inside loop is 3:
outside loop is 2:
inside loop is 1:
inside loop is 2:
inside loop is 3:
outside loop is 3:
inside loop is 1:
inside loop is 2:
inside loop is 3:
首先是外部循环,进入内部循环结束后又从外部循环重复一次,直到循环结束。
5.2、for循环嵌套到while循环
[root@wzp ~]# cat test3 
#!/bin/bash
var1=5
while [ $var1 -ge 0 ]
do 
  echo "outer loop is : $var1"
  for (( var2=1; var2<3; var2++ ))
  do
    echo "inner loop : $var2"
  done
  var1=$[ $var1-1 ]
done
[root@wzp ~]# ./test3 
outer loop is : 5
inner loop : 1
inner loop : 2
outer loop is : 4
inner loop : 1
inner loop : 2
outer loop is : 3
inner loop : 1
inner loop : 2
outer loop is : 2
inner loop : 1
inner loop : 2
outer loop is : 1
inner loop : 1
inner loop : 2
outer loop is : 0
inner loop : 1
inner loop : 2
从上面可以看出嵌套循环的作用体现了,不是很难理解。
6、控制循环
如上定义了数据的各种循环方式,当我们需要某时刻退出循环的时候,如内部循环和外部循环,这个时候需要用到两个命令:
*break命令
*continue命令
如下先说说break命令控制的循环:
6.1、跳出单循环
通过break命令使得for循环和while循环、until循环可以中断跳出,先看个例子:
[root@wzp ~]# cat 6.1test 
#!/bin/bash
for num in 1 2 3 4 5 6 7 8 9 10
do
  if [ $num -eq 5 ]
  then
     break
  fi
 echo "the num is : $num;"
done
echo 'the for loop is over !'
[root@wzp ~]# ./6.1test 
the num is : 1;
the num is : 2;
the num is : 3;
the num is : 4;
the for loop is over !
通过for循环迭代列表中的1到10,当if-then满足条件等于5的时候就跳出循环,显示循环结束,不难理解。
同样的例子,对于while循环,如下:
[root@wzp ~]# cat 6.1test 
#!/bin/bash
num=1
while [ $num -lt 10 ]
do
  if [ $num -eq 5 ]
  then
    break
  fi
  echo "the number is $num"
  num=$[ $num + 1 ]
done
echo 'the while loop is over !'
[root@wzp ~]# ./6.1test 
the number is 1
the number is 2
the number is 3
the number is 4
the while loop is over !
同样是当if-then满足条件的时候跳出循环。
6.2、跳出内循环
有时候循环油嵌套,这个时候我们可以通过break来跳出循环中的内部循环,先来看个例子:
[root@wzp ~]# cat 6.2test 
#!/bin/bash
for (( a=1; a<4; a++ ))
do
  echo "the outer loop is:$a"
  for (( b=1; b<100; b++ ))
  do
     if [ $b -eq 5 ]
     then
        break
     fi
     echo "inner loop is:$b"
 done
done  
[root@wzp ~]# ./6.2test 
the outer loop is:1
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
the outer loop is:2
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
the outer loop is:3
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
从上面的例子看,先是外部循环,进入内部循环满足if-then则跳出去执行外部循环,直到外部循环结束,整个循环就结束了。
6.3、外部循环
既然有了上面的内部循环,势必可以想到有外部循环的存在,这里需要借助break命令的参数值,其格式很简单:
break n
其中n表示循环级别,默认情况是1
[root@wzp ~]# cat 6.3test 
#!/bin/bash
for (( a=1; a<4; a++ ))
do 
  echo "the outer loop is:$a"
  for (( b=1; b<100; b++ ))
  do 
    if [ $b -gt 4 ]
    then 
      break 2
    fi
    echo "inner loop is:$b"
  done
done
[root@wzp ~]# ./6.3test 
the outer loop is:1
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
假如这里没有定义break等于2,那么结果应该如我们所想象的,如下:
[root@wzp ~]# ./6.3test 
the outer loop is:1
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
the outer loop is:2
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
the outer loop is:3
inner loop is:1
inner loop is:2
inner loop is:3
inner loop is:4
这样子我们就没有实现结束外循环的目的了,通过指定break为2,当内循环满足if-then条件的时候就把外循环指定的循环级别给结束了
OK,说完了break来说下continue了~~~
6.4、continue命令
该命令是一种提前停止循环内命令,但不结束循环。先来看一个for循环中使用continue的示例:
[root@wzp ~]# cat 6.4test 
#!/bin/bash
for (( a=1; a<24; a++ ))
do
  if [ $a -gt 8 ] && [ $a -lt 18 ]
  then 
     continue
  fi
  echo "the num is:$a"
done
[root@wzp ~]# ./6.4test 
the num is:1
the num is:2
the num is:3
the num is:4
the num is:5
the num is:6
the num is:7
the num is:8
the num is:18
the num is:19
the num is:20
the num is:21
the num is:22
the num is:23
如上表示说当满足a大于8并且小于18的时候就跳过该循环中余下的命令,但是循环继续进行。但满足if-then条件不成立的时候就继续循环下去。
7、处理循环的输出
在shell脚本中可以通过在done命令的末尾处添加处理命令实现,即为重定向>或者追加>>,看如下例子:
[root@wzp ~]# chmod u+x 7.1test 
[root@wzp ~]# ./7.1test 
[root@wzp ~]# cat 7.1test 
#!/bin/bash
for (( a=1; a<10; a++ ))
do
  echo "the num is $a"
done > num.txt
[root@wzp ~]# cat num.txt 
the num is 1
the num is 2
the num is 3
the num is 4
the num is 5
the num is 6
the num is 7
the num is 8
the num is 9
通过一个for循环,把指定的num内容重定向到num.txt文件中,该文件也就自动被创建在当前目录上了,这都好理解。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值