Shell programming 6


不管你怎么走,你会发现终点就是原点。


SHELL=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]

progress:[############ ] 60%


Ax Shell Loop Types | 轮回

循环是一个强大的编程工具,它能重复执行一组命令。shell中有以下类型的循环。

  • The while loop
  • The for loop
  • The until loop
  • The select loop

根据不同的情况使用不同的循环,while循环执行给定的命令,直到给定的条件保持为true。until循环执行直到给定的条件为真。

The while Loop

while循环重复执行一组命令,直到出现某种条件。

语法

while command
do
Statement(s) to be executed if command is true
done

条件结果值为true,则执行给定语句,否则跳至done的下一行。

显示0-9的示例

#!/bin/sh

a=0

while [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

执行脚本 −

0
1
2
3
4
5
6
7
8
9

The for Loop

for用于项目列表,它为列表中的每一项重复执行。

语法

for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done

每次循环,var变量的值会从word1到下一个值,直到wordN。

例1

#!/bin/sh

for var in 0 1 2 3 4 5 6 7 8 9
do
   echo $var
done

执行脚本 −

0
1
2
3
4
5
6
7
8
9

例2,编写一个显示Home下的.bash文件

#!/bin/sh

for FILE in $HOME/.bash*
do
   echo $FILE
done

执行脚本 −

/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc

The until Loop

while适合条件为真时执行一组命令,而有些时候需要条件直到为真。

语法

until command
do
Statement(s) to be executed until command is true
done

条件结果值为false,则执行给定语句,否则跳至done的下一行。

#!/bin/sh

a=0

until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

执行脚本 −

0
1
2
3
4
5
6
7
8
9

The select Loop

选择循环提供了一个简单的方法创建编号菜单,用户从中选择。在bash中可以,sh中不可用,ksh引入了该循环。

语法

select var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done

#!/bin/ksh

select DRINK in tea cofee water juice appe all none
do
   case $DRINK in
      tea|cofee|water|all) 
         echo "Go to canteen"
         ;;
      juice|appe)
         echo "Available at home"
      ;;
      none) 
         break 
      ;;
      *) echo "ERROR: Invalid selection" 
      ;;
   esac
done

执行脚本 −

$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? juice
Available at home
#? none
$

做出选择 −

$PS3 = "Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$

Nesting Loops

人生如梦幻泡影,每次遇见都是一个轮回。

例while嵌套

#!/bin/sh

a=0
while [ "$a" -lt 10 ]    # this is loop1
do
   b="$a"
   while [ "$b" -ge 0 ]  # this is loop2
   do
      echo -n "$b "
      b=`expr $b - 1`
   done
   echo
   a=`expr $a + 1`
done

echo -n 不换行,执行脚本

0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

Bx Shell Loop Control | 命运

人总会遇到倒霉的事情,但要相信,好运一定再前方。

控制语句

  • The break statement
  • The continue statement

The infinite Loop | 死亡

无论是什么循环都有生命,或真或假就会结束生命,或是永远执行,直到满足条件,但有些的条件是你一辈子都无法实现的,放下吧。

#!/bin/sh

a=10

until [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

a等于10,条件是小于十,没经历一次循环增加1,所以越尝试,你将离他越远,这是一个不可能的条件,你将经历越来越多,而离它越来越远。

┌─[✗]─[root@enomothem]─[~/script]
└──╼ #sh loopdead.sh 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
..
..
..
N

The break Statement | 离开

虽然中途离开,但并非是我无情,只是我有更重要的事情要去做。

break

break将跳出循环

通过参数用于嵌套循环中 −

break n

n指跳出几个循环

#!/bin/sh

a=10

until [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 11 ]
   then
     break
   fi
   a=`expr $a + 1`
done
echo new branch

使用break后,不再永远执行下去,因为错一步,不能步步错,及时止损,跳出循环。

┌─[root@enomothem]─[~/script]
└──╼ #sh loopdead.sh 
10
11
new branch

下面是嵌套循环的演示,var等于

#!/bin/sh

for var1 in 1 2 3
do
   for var2 in 0 5
   do
      if [ $var1 -eq 2 -a $var2 -eq 0 ]
      then
         break 2
      else
         echo "$var1 $var2"
      fi
   done
done

其中,n表示要跳出n层循环。默认值是1,表示跳出一层循环。若n=3,则表示一次跌出3层循环。

1 0
1 5

The continue statement | 重来

The continue statement 类似于 the break,但不是直接退出,而是退出当前的迭代中,continue 用来结束本次循环,直接跳到下一次循环,如果循环条件成立,还会继续循环。

continue

通过参数用于嵌套循环中

continue n

n表示指定第几个循环

#!/bin/sh

NUMS="1 2 3 4 5 6 7"

for NUM in $NUMS
do
   Q=`expr $NUM % 2`
   if [ $Q -eq 0 ]
   then
      echo "Number is an even number!!"
      continue
   fi
   echo "Found odd number"
done

执行脚本 −

Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number

文章作者: Enomothem
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Enomothem !
  目录