【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)(一)

简介: 【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)

本篇文章继续给大家介绍Shell编程,包括for循环、并发问题,while循环,流程控制语句,函数传参、函数变量、函数返回值,反向破解MD5等内容。


for循环

1. for 变量 in [取值列表]     取值列表可以是数字 字符串 变量 序列 命令
2. do                        for循环将取到的值以此赋值给变量
3.     命令即可
4. done
5. 
6. [root@LB00 Day04]# cat test.sh
7. #!/bin/bash
8. for i in a b c
9. do
10.   echo $i
11. done
12. [root@LB00 Day04]# sh test.sh
13. a
14. b
15. c
16. 
17. #也可以在命令行中写for循环
18. [root@LB00 Day04]# for i in `seq 10`;do echo $i;done
19. 1
20. 2
21. 3
22. 4
23. 5
24. 6
25. 7
26. 8
27. 9
28. 10
29. 
30. [root@LB00 Day04]# cat test.sh
31. #!/bin/bash
32. for i in "a b" c
33. do
34.   echo $i
35. done
36. [root@LB00 Day04]# sh test.sh
37. a b
38. c
39. 
40. [root@LB00 Day04]# cat test.sh
41. #!/bin/bash
42. for i in 10 20 30
43. do
44.   echo $i
45. done
46. [root@LB00 Day04]# sh test.sh
47. 10
48. 20
49. 30
50. 
51. [root@LB00 Day04]# cat test.sh
52. #!/bin/bash
53. for i in {1..5}
54. do
55.   echo $i
56. done
57. [root@LB00 Day04]# sh test.sh
58. 1
59. 2
60. 3
61. 4
62. 5
63. 
64. [root@LB00 Day04]# cat test.sh
65. #!/bin/bash
66. for i in {a..d}
67. do
68.   echo $i
69. done
70. [root@LB00 Day04]# sh test.sh
71. a
72. b
73. c
74. d

一、探测10.0.0.1-10.0.0.254哪些IP在线,ping的通则在线

1. [root@LB00 Day04]# cat test.sh
2. #!/bin/bash
3. for i in {1..254}
4. do
5.  IP=10.0.0.$i
6.  ping -c1 -W1 $IP &> /dev/null
7.  if [ $? -eq 0 ];then
8.  echo $IP 在线
9.  fi
10. done
11. [root@LB00 Day04]# sh test.sh
12. 10.0.0.1 在线
13. 10.0.0.2 在线
14. 10.0.0.4 在线
15. 10.0.0.7 在线
16. 10.0.0.8 在线
17. 10.0.0.31 在线
18. 10.0.0.51 在线
19. 10.0.0.61 在线
20. 
21. #可以再连接下Xshell过滤下ping
22. [root@LB00 ~]# ps axu|grep ping
23. root      13760  0.0  0.1 128552  1272 pts/0    T    09:25   0:00 ping -c1 -W1 10.0.0.107
24. root      13897  0.0  0.1 128552  1268 pts/0    S+   09:28   0:00 ping -c1 -W1 10.0.0.109
25. root      13899  0.0  0.0 112808   964 pts/2    S+   09:28   0:00 grep --color=auto ping

这样速度慢,我们可以用花括号括住循环体,后面再加&,实现并发,注意并不是所有的循环都使用并发快,如果循环次数过大的情况下,并发多了会过多的占用资源,不利于处理循环体的数据,有些时候甚至比不并发还要慢。

1. [root@LB00 Day04]# cat test.sh
2. #!/bin/bash
3. for i in {1..256}
4. do
5.  {
6.  IP=10.0.0.$i
7.  ping -c1 -W1 $IP &> /dev/null
8.  if [ $? -eq 0 ];then
9.  echo $IP 在线
10.   fi
11.   } &
12. done
13. echo "在线取IP完成......"
14. 
15. [root@LB00 Day04]# sh test.sh    #由于是并发,所以出现了顺序错乱,谁先ping好就先返回谁
16. 10.0.0.4 在线
17. 10.0.0.2 在线
18. 10.0.0.31 在线
19. 10.0.0.1 在线
20. 10.0.0.8 在线
21. 10.0.0.7 在线
22. 10.0.0.51 在线
23. 10.0.0.61 在线
24. 在线取IP完成......
25. 
26. #用另一个Xshell过滤,可以看到有很多进程
27. [root@LB00 ~]# ps axu|grep ping
28. root      13760  0.0  0.1 128552  1272 pts/0    T    09:25   0:00 ping -c1 -W1 10.0.0.107
29. root      13945  0.0  0.1 128552  1272 pts/0    T    09:29   0:00 ping -c1 -W1 10.0.0.155
30. root      13976  0.0  0.1 128552  1276 pts/0    T    09:30   0:00 ping -c1 -W1 10.0.0.26
31. root      14004  0.0  0.1 128552  1268 pts/0    R+   09:30   0:00 ping -c1 -W1 10.0.0.10
32. root      14005  0.0  0.1 128552  1272 pts/0    R+   09:30   0:00 ping -c1 -W1 10.0.0.5
33. root      14007  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.15
34. root      14008  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.16
35. root      14009  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.14
36. root      14010  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.12
37. root      14011  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.13
38. root      14012  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.17
39. root      14020  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.18
40. root      14040  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.19
41. root      14041  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.22
42. root      14042  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.24
43. root      14043  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.23
44. root      14044  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.20
45. root      14045  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.21
46. root      14075  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.33
47. root      14076  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.34
48. root      14077  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.36
49. root      14078  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.39
50. root      14079  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.42
51. root      14080  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.43
52. root      14081  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.32
53. root      14082  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.28
54. root      14083  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.41
55. root      14084  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.25
56. root      14085  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.26
57. root      14086  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.27
58. root      14087  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.30
59. root      14088  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.35
60. root      14089  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.37
61. root      14090  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.40
62. root      14091  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.29
63. root      14092  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.38
64. root      14093  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.69
65. root      14094  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.59
66. root      14095  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.68
67. root      14097  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.48
68. root      14098  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.50
69. root      14099  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.56
70. root      14100  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.62
71. root      14101  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.63
72. root      14102  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.53
73. root      14103  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.44
74. root      14104  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.52
75. root      14105  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.54
76. root      14106  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.58
77. root      14107  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.64
78. root      14108  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.70
79. root      14109  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.67
80. root      14110  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.55
81. root      14112  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.45
82. root      14113  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.46
83. root      14114  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.47
84. root      14115  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.49
85. root      14116  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.57
86. root      14117  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.60
87. root      14118  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.66
88. root      14119  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.71
89. root      14120  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.65
90. root      14126  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.74
91. root      14127  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.72
92. root      14128  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.73
93. root      14129  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.76
94. root      14130  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.75
95. root      14136  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.77
96. root      14142  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.78
97. root      14143  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.80
98. root      14144  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.79
99. root      14145  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.81
100. root      14146  1.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.83
101. root      14147  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.84
102. root      14148  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.85
103. root      14149  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.86
104. root      14150  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.82
105. root      14156  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.87
106. root      14157  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.88
107. root      14158  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.89
108. root      14159  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.90
109. root      14160  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.91
110. root      14170  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.93
111. root      14171  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.94
112. root      14172  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.95
113. root      14173  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.92
114. root      14175  0.0  0.0 112812   964 pts/2    S+   09:30   0:00 grep --color=auto ping
115. root      14176  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.97
116. root      14177  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.98
117. root      14178  0.0  0.1 128552  1268 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.96
118. root      14179  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.99
119. root      14180  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.100
120. root      14186  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.101
121. root      14187  0.0  0.1 128552  1276 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.103
122. root      14188  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.105
123. root      14189  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.104
124. root      14190  0.0  0.1 128552  1272 pts/0    S+   09:30   0:00 ping -c1 -W1 10.0.0.102

并发还可能出现,并发的内容没返回,但是脚本后面的语句已经执行完毕的情况,这种情况下对于有逻辑关系的语句不利,所以使用并发的时候需要考虑这种情况,这时候我们可以使用wait,解决这个问题。

1. #并发导致逻辑出问题
2. [root@LB00 Day04]# cat test.sh
3. #!/bin/bash
4. for i in {1..256}
5. do
6.         {
7.         IP=10.0.0.$i
8.         ping -c2 -W2 $IP &> /dev/null
9. if [ $? -eq 0 ];then
10.         echo $IP 在线
11.         fi
12.         } &
13. done
14. echo "在线取IP完成......"
15. [root@LB00 Day04]# sh test.sh
16. 在线取IP完成......
17. [root@LB00 Day04]# 10.0.0.7 在线
18. 10.0.0.4 在线
19. 10.0.0.8 在线
20. 10.0.0.1 在线
21. 10.0.0.2 在线
22. 10.0.0.31 在线
23. 10.0.0.51 在线
24. 10.0.0.61 在线
25. 
26. [root@LB00 Day04]#
27. 
28. #使用wait控制逻辑 
29. [root@LB00 Day04]# cat test.sh
30. #!/bin/bash
31. for i in {1..256}
32. do
33.   {
34.   IP=10.0.0.$i
35.   ping -c2 -W2 $IP &> /dev/null
36.   if [ $? -eq 0 ];then
37.   echo $IP 在线
38.   fi
39.   } &
40. done
41. wait
42. echo "在线取IP完成......"
43. [root@LB00 Day04]# sh test.sh
44. 10.0.0.31 在线
45. 10.0.0.2 在线
46. 10.0.0.1 在线
47. 10.0.0.7 在线
48. 10.0.0.4 在线
49. 10.0.0.8 在线
50. 10.0.0.51 在线
51. 10.0.0.61 在线
52. 在线取IP完成......

二、从1加到100

1. [root@LB00 Day04]# cat 100.sh
2. #!/bin/bash
3. sum=0
4. for i in `seq 100`
5. do 
6.  sum=`echo "$i+$sum"|bc`
7. done
8. echo $sum
9. [root@LB00 Day04]# sh 100.sh
10. 5050
11. 
12. #用命令也可以
13. [root@LB00 Day04]# seq -s + 100|bc    #-s是用什么分割
14. 5050

三、99乘法表

1. [root@LB00 Day04]# cat 99x.sh
2. #!/bin/bash
3. for i in `seq 9`
4. do
5.  for b in `seq $i`
6.  do
7.  printf "$b x $i = $[i*b]\t"    #printf是格式化输出
8.  done
9.  printf "\n"
10. done
11. [root@LB00 Day04]# sh 99x.sh
12. 1 x 1 = 1 
13. 1 x 2 = 2 2 x 2 = 4 
14. 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 
15. 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12  4 x 4 = 16  
16. 1 x 5 = 5 2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  
17. 1 x 6 = 6 2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  
18. 1 x 7 = 7 2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  
19. 1 x 8 = 8 2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  
20. 1 x 9 = 9 2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81

四、for循环读取文件,默认以空格取值

1. [root@LB00 Day04]# cat for_file.sh
2. #!/bin/bash
3. for i in `cat /etc/hosts`
4. do
5.  echo $i
6. done
7. [root@LB00 Day04]# sh for_file.sh
8. 127.0.0.1
9. localhost
10. localhost.localdomain
11. localhost4
12. localhost4.localdomain4
13. ::1
14. localhost
15. localhost.localdomain
16. localhost6
17. localhost6.localdomain6
18. 
19. [root@LB00 Day04]# cat user.txt 
20. zhangsan
21. lisi
22. wangwu
23. [root@LB00 Day04]# cat for_file.sh
24. #!/bin/bash
25. for i in `cat /server/scripts/Day04/user.txt`
26. do
27.   useradd $i
28. done
29. [root@LB00 Day04]# sh for_file.sh
30. [root@LB00 Day04]# tail -3 /etc/passwd
31. zhangsan:x:1007:1007::/home/zhangsan:/bin/bash
32. lisi:x:1008:1008::/home/lisi:/bin/bash
33. wangwu:x:1009:1009::/home/wangwu:/bin/bash

五、批量创建用户和删除用户

1. [root@LB00 Day04]# cat yonghu.sh
2. #!/bin/bash
3. read -p "请输入用户的前缀: " qianzhui
4. if ! [[ $qianzhui =~ ^[a-z] ]];then
5.  echo 请注意前缀输入格式 
6.  exit  
7. fi
8. read -p "请输入要操作的数量: " num1
9. if ! [[ $num1 =~ ^[0-9]+$ ]];then
10.         echo 请注意输入数量的格式
11. exit
12. fi
13. echo 为您生成如下用户
14. shuchu=""
15. for i1 in $(seq $num1)
16. do
17.   shuchu="$shuchu $qianzhui$i1"
18. done
19. echo $shuchu
20. read -p "您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]" caozuo
21. if [ $caozuo == y ];then
22.   for i2 in $(seq $num1)
23.   do    
24.     id $qianzhui$i2 &> /dev/null
25.     if [ $? -eq 0 ];then
26.       echo "$user 已存在"
27.     else
28.             useradd $qianzhui$i2
29.       if [ $? -eq 0 ];then
30.         echo "$user 创建成功"
31.       fi
32.     fi
33.   done  
34. elif [ $caozuo == d ];then
35.   for i3 in $(seq $num1)
36.         do
37.                 id $qianzhui$i3 &> /dev/null
38. if [ $? -ne 0 ];then
39.                         echo "$user 不存在不需要删除"
40.       exit
41. 
42. else
43.                         userdel -rf $qianzhui$i3
44. if [ $? -eq 0 ];then
45.                                 echo "$user 删除成功"
46.                         fi
47.                 fi
48.         done
49. elif [ $caozuo == i ];then
50. for i4 in $(seq $num1)
51.         do
52.                 id $qianzhui$i4
53.         done
54.         echo "用户查询完毕"
55. else
56.   echo 请输入正确的内容[y|d|i]
57. fi
58. [root@LB00 Day04]# sh yonghu.sh
59. 请输入用户的前缀: qwer
60. 请输入要操作的数量: 5
61. 为您生成如下用户
62. qwer1 qwer2 qwer3 qwer4 qwer5
63. 您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]d
64.  不存在不需要删除
65. [root@LB00 Day04]# sh yonghu.sh
66. 请输入用户的前缀: qwer
67. 请输入要操作的数量: 5
68. 为您生成如下用户
69. qwer1 qwer2 qwer3 qwer4 qwer5
70. 您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]y
71.  创建成功
72.  创建成功
73.  创建成功
74.  创建成功
75.  创建成功
76. [root@LB00 Day04]# sh yonghu.sh
77. 请输入用户的前缀: qwer
78. 请输入要操作的数量: 5
79. 为您生成如下用户
80. qwer1 qwer2 qwer3 qwer4 qwer5
81. 您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]i  uid=1010(qwer1) gid=1010(qwer1) groups=1010(qwer1)
82. uid=1011(qwer2) gid=1011(qwer2) groups=1011(qwer2)
83. uid=1012(qwer3) gid=1012(qwer3) groups=1012(qwer3)
84. uid=1013(qwer4) gid=1013(qwer4) groups=1013(qwer4)
85. uid=1014(qwer5) gid=1014(qwer5) groups=1014(qwer5)
86. 用户查询完毕
87. [root@LB00 Day04]# sh yonghu.sh
88. 请输入用户的前缀: qwer   
89. 请输入要操作的数量: 5
90. 为您生成如下用户
91. qwer1 qwer2 qwer3 qwer4 qwer5
92. 您可以选择操作y为创建,d为删除,i为查看用户id[y|d|i]d
93.  删除成功
94.  删除成功
95.  删除成功
96.  删除成功
97.  删除成功

六、创建user1-user10,共10个用户,只有user5不创建家目录,不允许登录

1. [root@LB00 Day04]# cat yonghu2.sh
2. #!/bin/bash
3. for i in `seq 10`
4. do
5.  if [ $i == 5 ];then
6.    useradd user$i -M -s /sbin/nologin
7.  else  
8.    useradd user$i
9.  fi
10. done
11. [root@LB00 Day04]# sh yonghu2.sh
12. [root@LB00 Day04]# tail -10 /etc/passwd
13. user1:x:1013:1013::/home/user1:/bin/bash
14. user2:x:1014:1014::/home/user2:/bin/bash
15. user3:x:1015:1015::/home/user3:/bin/bash
16. user4:x:1016:1016::/home/user4:/bin/bash
17. user5:x:1017:1017::/home/user5:/sbin/nologin
18. user6:x:1018:1018::/home/user6:/bin/bash
19. user7:x:1019:1019::/home/user7:/bin/bash
20. user8:x:1020:1020::/home/user8:/bin/bash
21. user9:x:1021:1021::/home/user9:/bin/bash
22. user10:x:1022:1022::/home/user10:/bin/bash
23. [root@LB00 Day04]# ls /home/|grep user
24. user1
25. user10
26. user2
27. user3
28. user4
29. user6
30. user7
31. user8
32. user9
33. [root@LB00 Day04]# su - user5
34. su: warning: cannot change directory to /home/user5: No such file or directory
35. This account is currently not available.

while循环

1. while [条件表达式]    #表达式成立则执行,不成立不执行
2. do
3.     可执行命令
4. done
5. 
6. #死循环,里面如果有read -p就会卡住
7. while true
8. do
9. echo hehe
10. done
11. 
12. while [ -f /etc/passwd ]
13. do
14. echo hehe
15. done
16. 
17. #循环1-100
18. i=1
19. while [ $i -le 100 ]
20. do
21. echo $i
22. let i++
23. done

一、while从1加到100

1. [root@LB00 Day04]# cat test.sh 
2. #!/bin/bash
3. i=1
4. while [ $i -le 100 ]
5. do
6. sum=$[sum+i]
7.     let i++
8. done
9. echo $sum
10. [root@LB00 Day04]# sh test.sh 
11. 5050

二、while读取文件,按照行读取内容

用for也能做,用while方便些

1. [root@LB00 Day04]# cat yonghu.txt
2. zs 123
3. ls 456
4. lw 789
5. [root@LB00 Day04]# cat duqu.sh
6. while read line
7. do
8.  user=`echo $line|awk '{print $1}'`
9.  pass=`echo $line|awk '{print $2}'`
10.   useradd $user
11.   echo $pass|passwd --stdin $user
12. done<yonghu.txt
13. [root@LB00 Day04]# sh duqu.sh
14. Changing password for user zs.
15. passwd: all authentication tokens updated successfully.
16. Changing password for user ls.
17. passwd: all authentication tokens updated successfully.
18. Changing password for user lw.
19. passwd: all authentication tokens updated successfully.
20. [root@LB00 Day04]# su - zs
21. [zs@LB00 ~]$ su - ls
22. Password: 
23. [ls@LB00 ~]$ exit
24. logout
25. [zs@LB00 ~]$ exit
26. logout

用for来操作,需要新设个变量除以2判断奇数还是偶数,奇数是用户名,偶数是密码

1. [root@LB00 Day04]# cat duqu_for.sh
2. #!/bin/bash
3. for i in `cat yonghu.txt`
4. do 
5.  let q++
6.  re=`echo $q%2|bc`
7.  if [ "$re" == 1 ];then
8.    user=$i
9.    useradd $user
10.   else
11.     pass=$i
12.     echo $pass|passwd --stdin $user
13.   fi
14. done
15. [root@LB00 Day04]# sh duqu_for.sh
16. Changing password for user zs.
17. passwd: all authentication tokens updated successfully.
18. Changing password for user ls.
19. passwd: all authentication tokens updated successfully.
20. Changing password for user lw.
21. passwd: all authentication tokens updated successfully.
22. [root@LB00 Day04]# tail -3 /etc/passwd
23. zs:x:1023:1023::/home/zs:/bin/bash
24. ls:x:1024:1024::/home/ls:/bin/bash
25. lw:x:1025:1025::/home/lw:/bin/bash

三、while统计行数

1. [root@LB00 Day04]# cat hangshu.sh
2. #!/bin/bash
3. wenjian=/etc/passwd
4. while read line
5. do
6.  let i++
7. done<$wenjian
8. echo "$wenjian 中总共 $i 行"
9. [root@LB00 Day04]# sh hangshu.sh
10. /etc/passwd 中总共 34 行


目录
相关文章
|
19天前
|
存储 运维 Shell
shell中for while until 三种循环的用法
shell编程中,有几种常见的循环结构,包括for循环、while循环和until循环,总的来说,循环shell编程中扮演着至关重要的角色,它们使得自动化任务变得更加容易,提高了效率,并且可以处理各种各样的编程需求。
shell中for while until 三种循环的用法
|
2月前
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
61 0
|
11天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
11天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
30天前
|
存储 Java Shell
bigdata-04-shell编程基础
bigdata-04-shell编程基础
13 0
|
1月前
|
Shell Linux C++
【Shell 编程设计】 编写自己的清理后台的Shell脚本
【Shell 编程设计】 编写自己的清理后台的Shell脚本
33 1
|
1月前
|
存储 Shell 数据安全/隐私保护
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
【Shell 编程指南】Shell read命令 (从标准输入读取数值)
27 0
|
1月前
|
Shell C语言 C++
【Shell 编程指南】shell中的(),{}几种语法用法
【Shell 编程指南】shell中的(),{}几种语法用法
17 0
|
1月前
|
Shell 程序员 Linux
【Shell 编程指南】shell运算操作符之(())
【Shell 编程指南】shell运算操作符之(())
19 0
|
3月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
67 0
Shell 编程快速入门 之 函数基础知识