一、for语法
for 变量 in 列表;do
循环体
done
二、常见用法
1、for用来遍历目录
1
2
3
4
5
6
7
8
9
10
|
#!/bin/bash #Version:0.1 #Author:lovelace #pragram:This
scripts is print all files in directory #difine
an varibale DIR= "/home/scripts/51cto" #All
files in directory traversal for f in $( ls $DIR); do echo $f done |
输出结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@lovelace for ] #
./dir.sh 1.sh 2curl.sh adduer.sh aliquot.sh argument.sh argusum.sh curl.sh dd .sh dirper.sh info.sh info.tt ipcheck.sh jugement.sh netcard.sh sum .sh test .sh The Frist week The Third week |
2、for ((初始条件;终止条件;异动项))
do
命令区域
done
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/bin/bash #Version:0.1 #Author:lovelace #pragram:This
pragram is and the sum from 1 to 100 #define
an integer declare -i
i #loops for ((i=1;i<=10;i=i+1)) do let sum +=1 done echo "The
result is:" $ sum |
输出结果为:
1
2
|
[root@lovelace for ] #
./sorsum.sh The
result is: 10 |
3、for 无穷循环
for ((;1;));do
命令区域
done
1
2
3
4
5
|
[root@lovelace for ] #
cat forover.sh #!/bin/bash for ((;1;)); do echo "forever..." done |
输出结果:
1
2
3
4
5
6
7
8
|
[root@lovelace for ] #
./forover.sh forever... forever... forever... forever... forever... forever... forever... |
三、关于break和continue
break、continue 一样可以运用在for while until select这4中循环中,
break :退出循环 提前退出
continue:提前进入下一轮循环 ,continue后面的语句将不再执行
示例(计算1到100内能被3整除的数之和):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/bin/bash #Verson:0.1 #Auther:lovelace #Pragram:This
pragram is calculation from 1 to 100 aliquot 3 sum # declare -i
i=0 declare -i sum =0 #use
loop traverse from 1 to 100 while [
$i -lt 100 ]; do let i++ #jugement
aliqotu 3 or not if [
$(($i%3)) - eq 0
]; then let sum +=i else continue fi done #print
sum echo "from
1 to 100 aliquot 3 sum is $sum" |
输出结果为:
1
2
|
[root@lovelace for ] #
./three.sh from
1 to 100 aliquot 3 sum is
1683 |
四、再次重提如何生成列表
如何生成列表:
1、整数列表
{1..100} 生存1到100的列表
2、seq
seq 10 1到10
seq 5 10 5到10
seq 5 10 2 返回列表为6 8 10
3、`ls /etc`
生成列表不单单只有我们列出的这些,实际案例上需要灵活运用
示例:(分别显示当前系统上所有默认shell中为bash的用户和默认为nologin的用户)
1
2
3
4
5
6
7
8
9
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
|
[root@lovelace for ] #
cat checkbash.sh #!/bin/bash #Version:0.1 #Author:lovelace #pragram:This
scripts is check user bash and print it #取出使用bash的用户个数 bashline=` grep 'bash$' /etc/passwd | wc -l` #取出使用nologin的用户个数 nologinline=` grep 'nologin$' /etc/passwd | wc -l` #取出bash用户列表 bashuser=` grep 'bash$' /etc/passwd | cut -d:
-f1` #取出nologin用户列表 nologin=` grep 'nologin$' /etc/passwd | cut -d:
-f1` #遍历使用bash的用户并打印出来 for x in $bashuser; do echo "bash
users is:$x" done #遍历使用nologin的用户并打印出来 for y in $nologin; do echo "nologin
users is:$y" done #结果如下 [root@lovelace for ] #
./checkbash.sh bash users is:root bash users is:nick bash users is:kale bash users is:user2 bash users is:user3 bash users is:user4 bash users is:user5 bash users is:user6 bash users is:user7 bash users is:user8 bash users is:user9 bash users is:user10 bash users is:mark bash users is:lovelace bash users is:lovetest nologin users is:bin nologin users is:daemon nologin users is:adm nologin users is:lp nologin users is:mail nologin users is:uucp nologin users is:operator nologin users is:games nologin users is:gopher nologin users is: ftp nologin users is:nobody nologin users is:nscd nologin users is:vcsa nologin users is:pcap nologin users is:ntp nologin users is:dbus nologin users is:avahi nologin users is:rpc nologin users is:mailnull nologin users is:smmsp nologin users is:sshd nologin users is:oprofile nologin users is:rpcuser nologin users is:nfsnobody nologin users is:xfs nologin users is:haldaemon nologin users is:avahi-autoipd nologin users is:gdm nologin users is:sabayon nologin users is:jack |