shell脚本第三章循环语句

目录

一、循环

1.循环的含义

2.for

2.1语法结构

2.2例子

2.2.1列表循环

2.2.2不带列表循环

2.2.3类似C的for语句

求和实验

九九乘法表

 回收站

2.3实验操作

2.3.1将目录文件夹下的.sh结尾的文件改为.txt结尾

2.3.2100以内数字的求和结果

2.3.3批量添加用户

2.3.4验证网段内的主机网络是否畅通

方法一

方法二

2.3.5九九乘法表

 3.while

3.1语法

3.2死循环 

3.3实验

3.3.1累加求和

3.3.2批量添加用户

 3.3.3批量删除用户

 3.3.4盲猜商品价格

4.跳出循环

4.1break——跳出单个循环

 4.2Continue——跳出本次循环 只结束某次循环

 4.3exit——跳出整个脚本

 5.截取网卡流量

6.until——当不成立时才会执行

6.1求和

6.2约吗


一、循环

1.循环的含义

将某代码段重复运行多次,通常有进入循环的条件和退出循环的条件

重复运行次数

  • 循环次数事先已知
  • 循环次数事先未知

常见的循环的命令:for, while, until

for:已知次数;while,until:已知条件

shell脚本第三章循环语句_第1张图片

2.for

2.1语法结构
  • 列表循环
  • 不带列表循环
  • 类C风格的for循环
[root@localhost ~]#help for
for: for 名称 [in 词语 ... ] ; do 命令; done
    为列表中的每个成员执行命令。
    
    `for' 循环为列表中的每个成员执行一系列的命令。如果没有
    `in WORDS ...;'则假定使用 `in "$@"'。对于 WORDS 中的每
     个元素,NAME 被设定为该元素,并且执行 COMMANDS 命令。
    
    退出状态:
    返回最后执行的命令的状态。
for ((: for (( 表达式1; 表达式2; 表达式3 )); do 命令; done
    算术 for 循环。
    
    等价于
    	(( EXP1 ))
    	while (( EXP2 )); do
    		COMMANDS
    		(( EXP3 ))
    	done
    EXP1、EXP2 和 EXP3都是算术表达式。如果省略任何表达式,
    则等同于使用了估值为1的表达式。
    
    退出状态:
    返回最后执行的命令的状态。
[root@localhost ~]#for i in a b c d;do echo i=$i;done
i=a
i=b
i=c
i=d
[root@localhost ~]#for i in a b c d;do echo hello world;done
hello world
hello world
hello world
hello world
[root@localhost ~]#for i in `seq 10`;do echo hello world;done
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
 
#for 变量名 in {list}
do
  command
done
2.2例子
2.2.1列表循环
for 变量名 in {list}
do
  command
done
[root@localhost ~]#for i in {1..50..2};do echo i=$i;done
#1-50的奇数
i=1
i=3
i=5
i=7
i=9
i=11
i=13
i=15
i=17
i=19
i=21
i=23
i=25
i=27
i=29
i=31
i=33
i=35
i=37
i=39
i=41
i=43
i=45
i=47
i=49
 
 
[root@localhost ~]#for i in {2..50..2};do echo i=$i;done
#1-50的偶数
i=2
i=4
i=6
i=8
i=10
i=12
i=14
i=16
i=18
i=20
i=22
i=24
i=26
i=28
i=30
i=32
i=34
i=36
i=38
i=40
i=42
i=44
i=46
i=48
i=50
 
 
[root@localhost ~]#for i in {10..1};do echo i=$i;done
#10-1倒序排列
i=10
i=9
i=8
i=7
i=6
i=5
i=4
i=3
i=2
i=1
 
 
 
[root@localhost ~]#for i in $(seq 10);do echo i=$i;done
#1-10正序排列
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
 
 
[root@localhost ~]#for i in $(seq 10 -1 1);do echo i=$i;done
#10-1的倒序排列
i=10
i=9
i=8
i=7
i=6
i=5
i=4
i=3
i=2
i=1
 
 
[root@localhost ~]#for i in $(seq 1 2 10);do echo i=$i;done
#1-10的奇数
i=1
i=3
i=5
i=7
i=9
 
 
 
 
[root@localhost ~]#for i in $(seq 0 2 10);do echo i=$i;done
#0-10的偶数
i=0
i=2
i=4
i=6
i=8
i=10
2.2.2不带列表循环
for 变量名 
do
    command
done
[root@localhost ~]#vim hello.sh
 
#!/bin/bash
for i  in  {1..10}
do
echo "hello"
done
 
 
[root@localhost ~]#bash hello.sh 
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
2.2.3类似C的for语句
for ((expr1;expr2;expr3))
do
       command
done
 
expr1:定义变量并赋初值
expr2:决定是否循环
expr3:决定循环变量如何改变,决定循环什么时候退出

 sum+=i    等于   sum=sum+i  
#需要使用 let  命令

  • ++    自身变量+1
  • --      自身变量-1
  • +=5  自身变量+5
  • -=5    自身变量-5
  • *=5    自身变量*5
  • /=5    自身变量/5
  • %=5  自身变量%5
[root@localhost ~]#vim qh.sh
 
#!/bin/bash
for ((i=1;i<=10;i+=2))
do
     echo $i
done
 
 
[root@localhost ~]#bash qh.sh 
1
3
5
7
9
求和实验
[root@localhost ~]#vim qh.sh
 
#!/bin/bash
 
sum=0
for ((i=0;i<=100;i++))
do
sum=$[i+sum]
done
echo "求和结果为 $sum"
 
 
[root@localhost ~]#bash qh.sh 
求和结果为 5050
[root@localhost ~]#vim qh1.sh 
 
#!/bin/bash
 
sum=0
for i in `seq 10`
do
sum=$[i+sum]
done
echo "求和结果为 $sum"
 
[root@localhost ~]#bash qh1.sh 
求和结果为 55
九九乘法表
[root@localhost ~]#vim 99cfb.sh 
#i是列  j是行
#!/bin/bash
for ((i=1;i<=9;i++))
do
  for((j=1;j<=i;j++))
  do
  echo -e "${i}x${j}=$[i*j] \t\c"
  done
echo
done
 
 
[root@localhost ~]#bash 99cfb.sh 
1x1=1 	
2x1=2 	2x2=4 	
3x1=3 	3x2=6 	3x3=9 	
4x1=4 	4x2=8 	4x3=12 	4x4=16 	
5x1=5 	5x2=10 	5x3=15 	5x4=20 	5x5=25 	
6x1=6 	6x2=12 	6x3=18 	6x4=24 	6x5=30 	6x6=36 	
7x1=7 	7x2=14 	7x3=21 	7x4=28 	7x5=35 	7x6=42 	7x7=49 	
8x1=8 	8x2=16 	8x3=24 	8x4=32 	8x5=40 	8x6=48 	8x7=56 	8x8=64 	
9x1=9 	9x2=18 	9x3=27 	9x4=36 	9x5=45 	9x6=54 	9x7=63 	9x8=72 	9x9=81
 回收站
[root@localhost ~]#vim hsz.sh
 
#!/bin/bash
WARNING_COLOR="echo -e \E[1;36m"
END="\E[0m"
 
DIR=/tmp/`date +%F_%H-%M-%S`
 
mkdir  $DIR
mv $*  $DIR       
${WARNING_COLOR} Move $* to $DIR $END
 
 
 
[root@localhost ~]#chmod +x hsz.sh
#给回收站加一个执行权限
 
 
[root@localhost ~]#alias rm=hsz.sh
#定义别名rm为hsz.sh
 
[root@localhost ~]#touch 123.txt
[root@localhost ~]#ls
123.txt          hello.sh              qh.sh                      xxgjx.sh
99cfb.sh         hh.sh                 scandisk.sh                yunsuan.sh
99.sh            h.sh                  score1.sh                  公共
adduser.sh       hsz.sh                score2.sh                  模板
anaconda-ks.cfg  initial-setup-ks.cfg  score3.sh                  视频
az.sh            jttl.sh               score.sh                   图片
bjc1.sh          ljf.sh                tbj.sh                     文档
bjc.sh           menu.sh               test1.sh                   下载
case.sh          mv.sh                 test.sh                    音乐
fenshu.sh        ping1.sh              webmin-2.100-1.noarch.rpm  桌面
h1.sh            ping.sh               xtuser.sh
 
 
[root@localhost ~]#bash hsz.sh  123.txt
#使用回收站
 Move 123.txt to /opt/2024-01-25_16-45-15
#回收站的脚本运行   移动123.txt文件导/opt/下并建立名为2024年1月25日16时45分15秒的文件夹
[root@localhost ~]#ls /opt/
#去查看/opt文件夹下 是否有回收站的文件
2024-01-25_16-45-15  hostonline.txt  off.txt
hostoffline.txt      name.txt        on.txt
[root@localhost ~]#cd /opt/2024-01-25_16-45-15/
[root@localhost 2024-01-25_16-45-15]#ls
123.txt
2.3实验操作
2.3.1将目录文件夹下的.sh结尾的文件改为.txt结尾
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
[root@localhost opt]#touch {1..10}.sh
[root@localhost opt]#ls
10.sh  1.sh  2.sh  3.sh  4.sh  5.sh  6.sh  7.sh  8.sh  9.sh
[root@localhost opt]#cd
[root@localhost ~]#vim mv.sh 
 
 
#/bin/bash
for i  in  `ls /opt`
do
name=`echo ${i}|cut -d "." -f1`
 
mv  /opt/${i}   /opt/${name}.txt
 
 
done
 
[root@localhost ~]#bash mv.sh 
[root@localhost ~]#ls /opt
10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
2.3.2100以内数字的求和结果
[root@localhost ~]#vim ljf.sh 
 
#!/bin/bash
 
sum=0
for i  in  {1..100}
do
sum=$[i+sum]
done
echo "求和结果为 $sum"
 
 
 
#求奇数和
sum=0
for i in {1..100..2}
do
let sum+=$i
done
 
echo "奇数和为$sum"
 
 
#求偶数和
sum=0
for i in {0..100..2}
do
let sum=$[i+sum]
done
 
echo "偶数和为$sum"
 
 
 
# i=1  sum=0  sum1=1+0=1
# i=2  sum=1  sum2=1+2=3
# i=3  sum=3  sum3=3+3=6
# i=4  sum=6  sum4=4+6=10
 
 
 
[root@localhost ~]#bash ljf.sh 
求和结果为 5050
奇数和为2500
偶数和为2550
2.3.3批量添加用户
  • 用户名存放在users.txt文件中,每行一个
  • 初始密码均设置为123123
  • 验证脚本
[root@localhost opt]#vim name.txt
 
wyq
xyl
cxk
 
[root@localhost opt]#cd
[root@localhost ~]#vim adduser.sh 
 
#!/bin/bash
for i in `cat /opt/name.txt`
do
id $i  &>>/dev/null
if [ $? -eq 0 ]
then
echo ${i}"用户已存在"
else
useradd $i
echo 123123 | passwd --stdin $i &>>/dev/null
fi
done
 
 
[root@localhost ~]#bash adduser.sh 
正在创建信箱文件: 文件已存在
正在创建信箱文件: 文件已存在
cxk用户已存在
[root@localhost ~]#bash adduser.sh 
wyq用户已存在
xyl用户已存在
cxk用户已存在
[root@localhost ~]#vim /opt/name.txt 
 
wyq
xyl
cxk
wsc
 
 
[root@localhost ~]#bash adduser.sh 
wyq用户已存在
xyl用户已存在
cxk用户已存在
正在创建信箱文件: 文件已存在
[root@localhost ~]#bash adduser.sh 
wyq用户已存在
xyl用户已存在
cxk用户已存在
wsc用户已存在
2.3.4验证网段内的主机网络是否畅通
方法一
[root@localhost ~]#vim ping.sh 
 
#!/bin/bash
for i in  {1..254}
do
{
ping  -c2  -W2  192.168.241.$i &>/dev/null && echo "$i  is online" >> /opt/hostonline.txt     ||echo "$i is  offline"  >>  /opt/hostoffline.txt
} &
done
 
[root@localhost ~]#bash ping.sh
[root@localhost ~]#ls /opt
hostonline.txt  name.txt
#此时正在后台执行ping offline还未ping
[root@localhost ~]#ls /opt
hostoffline.txt  hostonline.txt  name.txt
#此时online 和 offline 都已完成ping
[root@localhost ~]#cat /opt/hostonline.txt 
1  is online
11  is online
2  is online
[root@localhost ~]#cat /opt/hostoffline.txt 
5 is  offline
3 is  offline
8 is  offline
9 is  offline
12 is  offline
14 is  offline
16 is  offline
30 is  offline
29 is  offline
25 is  offline
27 is  offline
19 is  offline
21 is  offline
23 is  offline
33 is  offline
37 is  offline
34 is  offline
49 is  offline
45 is  offline
51 is  offline
43 is  offline
39 is  offline
47 is  offline
54 is  offline
41 is  offline
52 is  offline
67 is  offline
57 is  offline
59 is  offline
65 is  offline
6 is  offline
61 is  offline
113 is  offline
68 is  offline
7 is  offline
82 is  offline
4 is  offline
99 is  offline
86 is  offline
70 is  offline
111 is  offline
60 is  offline
79 is  offline
95 is  offline
76 is  offline
93 is  offline
106 is  offline
73 is  offline
101 is  offline
88 is  offline
85 is  offline
71 is  offline
90 is  offline
81 is  offline
38 is  offline
75 is  offline
69 is  offline
44 is  offline
28 is  offline
10 is  offline
87 is  offline
50 is  offline
53 is  offline
77 is  offline
32 is  offline
80 is  offline
63 is  offline
66 is  offline
83 is  offline
72 is  offline
35 is  offline
104 is  offline
89 is  offline
91 is  offline
110 is  offline
108 is  offline
92 is  offline
31 is  offline
97 is  offline
100 is  offline
22 is  offline
98 is  offline
48 is  offline
102 is  offline
74 is  offline
96 is  offline
94 is  offline
103 is  offline
64 is  offline
13 is  offline
105 is  offline
15 is  offline
124 is  offline
24 is  offline
55 is  offline
17 is  offline
112 is  offline
18 is  offline
131 is  offline
20 is  offline
126 is  offline
36 is  offline
109 is  offline
107 is  offline
84 is  offline
56 is  offline
114 is  offline
40 is  offline
118 is  offline
58 is  offline
46 is  offline
62 is  offline
119 is  offline
120 is  offline
78 is  offline
122 is  offline
42 is  offline
132 is  offline
115 is  offline
116 is  offline
138 is  offline
140 is  offline
117 is  offline
135 is  offline
121 is  offline
231 is  offline
128 is  offline
207 is  offline
143 is  offline
212 is  offline
137 is  offline
227 is  offline
245 is  offline
203 is  offline
229 is  offline
145 is  offline
149 is  offline
193 is  offline
168 is  offline
146 is  offline
169 is  offline
154 is  offline
222 is  offline
205 is  offline
233 is  offline
175 is  offline
151 is  offline
165 is  offline
156 is  offline
180 is  offline
167 is  offline
161 is  offline
238 is  offline
157 is  offline
163 is  offline
189 is  offline
225 is  offline
184 is  offline
208 is  offline
159 is  offline
198 is  offline
171 is  offline
240 is  offline
178 is  offline
241 is  offline
123 is  offline
176 is  offline
179 is  offline
142 is  offline
141 is  offline
177 is  offline
148 is  offline
164 is  offline
170 is  offline
210 is  offline
220 is  offline
181 is  offline
162 is  offline
125 is  offline
183 is  offline
133 is  offline
186 is  offline
26 is  offline
155 is  offline
188 is  offline
190 is  offline
166 is  offline
192 is  offline
187 is  offline
223 is  offline
194 is  offline
199 is  offline
196 is  offline
236 is  offline
197 is  offline
200 is  offline
144 is  offline
206 is  offline
216 is  offline
201 is  offline
230 is  offline
202 is  offline
185 is  offline
211 is  offline
243 is  offline
191 is  offline
204 is  offline
134 is  offline
226 is  offline
228 is  offline
152 is  offline
147 is  offline
221 is  offline
237 is  offline
129 is  offline
160 is  offline
209 is  offline
182 is  offline
195 is  offline
217 is  offline
218 is  offline
214 is  offline
219 is  offline
139 is  offline
246 is  offline
127 is  offline
234 is  offline
172 is  offline
239 is  offline
136 is  offline
235 is  offline
158 is  offline
232 is  offline
150 is  offline
224 is  offline
153 is  offline
244 is  offline
174 is  offline
213 is  offline
215 is  offline
242 is  offline
173 is  offline
130 is  offline
247 is  offline
251 is  offline
250 is  offline
254 is  offline
252 is  offline
253 is  offline
248 is  offline
249 is  offline
方法二
[root@localhost ~]#vim ping1.sh
 
#!/bin/bash
{
for i in  {1..254}
do
 
ping  -c2  -W2  192.168.91.$i &>/dev/null
if [ $? -eq 0 ]
then
echo "host is online"  >>/opt/on.txt
else
echo "host is offline"  >>/opt/off.txt
fi
 
done
}&
 
 
[root@localhost ~]#bash ping1.sh 
[root@localhost ~]#ls /opt
hostoffline.txt  hostonline.txt  name.txt  on.txt
[root@localhost ~]#ls /opt
hostoffline.txt  hostonline.txt  name.txt  on.txt
[root@localhost ~]#ls /opt
hostoffline.txt  hostonline.txt  name.txt  off.txt  on.txt
#此方法后台执行比较慢  建议使用第一种方法
2.3.5九九乘法表

我们要知道九九乘法表是直角三角形  形状的

首先 要知道如何打印横向和纵向的*

[root@localhost ~]#for i in {1..9};do echo "*";done
*
*
*
*
*
*
*
*
*
[root@localhost ~]#for i in {1..9};do echo -e "*\c";done
*********[root@localhost ~]#

如何打印矩形

[root@localhost ~]#vim 99.sh
 
#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq 9`
   do
   echo -e "*\c"
  done
echo
done
 
 
[root@localhost ~]#bash 99.sh 
*********
*********
*********
*********
*********
*********
*********
*********
*********

然后我们探讨如何打印出直角三角形

[root@localhost ~]#vim 99.sh
 
 
#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "*\c"
  done
echo
done
 
 
[root@localhost ~]#bash 99.sh 
*
**
***
****
*****
******
*******
********
*********

然后我们可以打印出算式

[root@localhost ~]#vim 99.sh 
 
#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "${i}x${j} \c"
  done
echo
done
 
 
 
[root@localhost ~]#bash 99.sh 
1x1 
1x2 2x2 
1x3 2x3 3x3 
1x4 2x4 3x4 4x4 
1x5 2x5 3x5 4x5 5x5 
1x6 2x6 3x6 4x6 5x6 6x6 
1x7 2x7 3x7 4x7 5x7 6x7 7x7 
1x8 2x8 3x8 4x8 5x8 6x8 7x8 8x8 
1x9 2x9 3x9 4x9 5x9 6x9 7x9 8x9 9x9

最后我们可以将运算结果添加进去

[root@localhost ~]#vim 99.sh 
 
#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "${i}x${j}=$[i*j] \c"
  done
echo
done
 
 
[root@localhost ~]#bash 99.sh 
1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

我们发现有点不太美观  可以调整一下echo  -e  \t

[root@localhost ~]#vim 99.sh 
 
 
#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "${i}x${j}=$[i*j] \t\c"
  done
echo
done
 
[root@localhost ~]#bash 99.sh 
1x1=1 	
1x2=2 	2x2=4 	
1x3=3 	2x3=6 	3x3=9 	
1x4=4 	2x4=8 	3x4=12 	4x4=16 	
1x5=5 	2x5=10 	3x5=15 	4x5=20 	5x5=25 	
1x6=6 	2x6=12 	3x6=18 	4x6=24 	5x6=30 	6x6=36 	
1x7=7 	2x7=14 	3x7=21 	4x7=28 	5x7=35 	6x7=42 	7x7=49 	
1x8=8 	2x8=16 	3x8=24 	4x8=32 	5x8=40 	6x8=48 	7x8=56 	8x8=64 	
1x9=9 	2x9=18 	3x9=27 	4x9=36 	5x9=45 	6x9=54 	7x9=63 	8x9=72 	9x9=81

如果我们再美观一点  搞点颜色看看

shell脚本第三章循环语句_第2张图片 

#!/bin/bash
#j 行数  i 列数
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "\E[1;35m"${i}x${j}=$[i*j]"\E[0m\t\c"
  done
echo
done

 如果想随机颜色查看九九乘法表

shell脚本第三章循环语句_第3张图片

#/bin/bash
#j 行数  i 列数
R=$[RANDOM%7+31]
for j  in {1..9}
do
   for i  in `seq $j`
   do
   echo -e "\E[1;$[R]m"${i}x${j}=$[i*j]"\E[0m\t\c"
  done
echo
done

倒序九九乘法表

[root@localhost ~]#vim dx99.sh
 
#!/bin/bash
for j in {1..9}
do
for i in `seq $[10-$j]`
do
echo -ne " ${i}x`echo $[10-j]`=$[(10 -j)*i]\t"
done
echo
done
 
[root@localhost ~]#bash dx99.sh 
 1x9=9	 2x9=18	 3x9=27	 4x9=36	 5x9=45	 6x9=54	 7x9=63	 8x9=72	 9x9=81	
 1x8=8	 2x8=16	 3x8=24	 4x8=32	 5x8=40	 6x8=48	 7x8=56	 8x8=64	
 1x7=7	 2x7=14	 3x7=21	 4x7=28	 5x7=35	 6x7=42	 7x7=49	
 1x6=6	 2x6=12	 3x6=18	 4x6=24	 5x6=30	 6x6=36	
 1x5=5	 2x5=10	 3x5=15	 4x5=20	 5x5=25	
 1x4=4	 2x4=8	 3x4=12	 4x4=16	
 1x3=3	 2x3=6	 3x3=9	
 1x2=2	 2x2=4	
 1x1=1

 3.while

相对于for,需要知道循环次数;我们只知道停止条件,不知道次数,就需要使用while,直到直达条件

shell脚本第三章循环语句_第4张图片

3.1语法
while  条件判断
do
done
3.2死循环 
while死循环1.2.3
 
1.while [ 1 -eq 1 ]
#写一个永远为真的表达式,1等于1这个条件永远为真,所以这个脚本会一直循环下去
do
    command
done
 
2.while true
do
    command
done
 
3.while :
do
    command
done
 
 
[root@localhost ~]#help :
:: :
    空的命令。
    
    没有效果; 此命令不做任何操作。
    
    退出状态:
    总是成功。
[root@localhost ~]#help true
true: 真
    返回一个成功结果。
    
    退出状态:
    总是成功

while循环一般用于有条件判断的循环,若判断条件为真,则进入循环,当条件为假就跳出循环

3.3实验
3.3.1累加求和
[root@localhost ~]#vim ljqh.sh
 
#!/bin/bash
 
 
i=0
sum=0
while [ $i -le 100 ]
do
 
sum=$[i+sum]
 
let i++
#每次执行一次循环  i自加1
done
echo $sum
 
[root@localhost ~]#bash ljqh.sh 
5050
3.3.2批量添加用户
[root@localhost ~]#vim useradd.sh
 
#!/bin/bash
 
cxk=1
while  [ $cxk -le 20 ]
do
useradd stu$cxk
echo "123123"|passwd --stdin stu$cxk  &>>/dev/null
let cxk++
done
 
[root@localhost ~]#bash useradd.sh 
[root@localhost ~]#cat /etc/passwd|tail -n 25
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
cxk:x:1001:1001::/home/cxk:/bin/bash
wyq:x:1002:1002::/home/wyq:/bin/bash
xyl:x:1003:1003::/home/xyl:/bin/bash
wsc:x:1004:1004::/home/wsc:/bin/bash
stu1:x:1005:1005::/home/stu1:/bin/bash
stu2:x:1006:1006::/home/stu2:/bin/bash
stu3:x:1007:1007::/home/stu3:/bin/bash
stu4:x:1008:1008::/home/stu4:/bin/bash
stu5:x:1009:1009::/home/stu5:/bin/bash
stu6:x:1010:1010::/home/stu6:/bin/bash
stu7:x:1011:1011::/home/stu7:/bin/bash
stu8:x:1012:1012::/home/stu8:/bin/bash
stu9:x:1013:1013::/home/stu9:/bin/bash
stu10:x:1014:1014::/home/stu10:/bin/bash
stu11:x:1015:1015::/home/stu11:/bin/bash
stu12:x:1016:1016::/home/stu12:/bin/bash
stu13:x:1017:1017::/home/stu13:/bin/bash
stu14:x:1018:1018::/home/stu14:/bin/bash
stu15:x:1019:1019::/home/stu15:/bin/bash
stu16:x:1020:1020::/home/stu16:/bin/bash
stu17:x:1021:1021::/home/stu17:/bin/bash
stu18:x:1022:1022::/home/stu18:/bin/bash
stu19:x:1023:1023::/home/stu19:/bin/bash
stu20:x:1024:1024::/home/stu20:/bin/bash
 3.3.3批量删除用户
[root@localhost ~]#vim userdel.sh
 
#!/bin/bash
 
cxk=1
while  [ $cxk -le 20 ]
do
userdel stu$cxk
let cxk++
done
 
[root@localhost ~]#bash userdel.sh 
[root@localhost ~]#cat /etc/passwd|tail -n 25
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
chrony:x:995:991::/var/lib/chrony:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin
sssd:x:992:987:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
ghd:x:1000:1000:ghd:/home/ghd:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
cxk:x:1001:1001::/home/cxk:/bin/bash
wyq:x:1002:1002::/home/wyq:/bin/bash
xyl:x:1003:1003::/home/xyl:/bin/bash
wsc:x:1004:1004::/home/wsc:/bin/bash
 3.3.4盲猜商品价格
[root@localhost ~]#vim caicai.sh 
 
 
 
#!/bin/bash
 
p=`echo $[RANDOM%1000+1]`
time=0
while  :
do
read -p "请输入商品的价格(1-1000):" h
let time++
if  [ $p -eq $h ]
then
echo "恭喜你猜对了,商品价格是${h};您一共猜了$time次"
exit
#跳出循环
elif [ $p -gt $h -a $h -le 1000 -a $h -gt 0 ]
then
echo "您猜的价格过低,请往高了猜"
elif [ $p -lt $h -a $h -le 1000 -a $h -gt 0 ]
then
echo "您猜的价格过高"
else
echo "输入有误,请重新输入"
 
fi
 
done
 
 
 
[root@localhost ~]#bash caicai.sh 
请输入商品的价格(1-1000):10000
输入有误,请重新输入
请输入商品的价格(1-1000):1000
您猜的价格过高
请输入商品的价格(1-1000):500
您猜的价格过低,请往高了猜
请输入商品的价格(1-1000):600
您猜的价格过低,请往高了猜
请输入商品的价格(1-1000):700
您猜的价格过高
请输入商品的价格(1-1000):650
您猜的价格过高
请输入商品的价格(1-1000):630
您猜的价格过低,请往高了猜
请输入商品的价格(1-1000):640
您猜的价格过低,请往高了猜
请输入商品的价格(1-1000):645
恭喜你猜对了,商品价格是645;您一共猜了9次

4.跳出循环

  • break跳出单个循环 break n 数字数字是几代表跳出n层循环
  • continue终止某次循环中的命令,但是不会完全终止命令
  • exit 直接退出脚本
4.1break——跳出单个循环
[root@localhost ~]#vim break.sh
 
#!/bin/bash
for j in {1..5}
do
  for i in {1..10}
  do
    echo i=$i
  done
echo "-------------------------------"
done
 
 
[root@localhost ~]#bash break.sh 
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
-------------------------------
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
-------------------------------
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
-------------------------------
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
-------------------------------
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
-------------------------------
[root@localhost ~]#vim break2.sh 
 
#!/bin/bash
for i in {1..10}
do
if [ $i -eq 5 ]
then break 2
fi
echo i=$i
done
 
 
[root@localhost ~]#bash break2.sh 
i=1
i=2
i=3
i=4

break 截止到当前为止就结束;

break默认为1;

break n代表跳出几次循环。 

 4.2Continue——跳出本次循环 只结束某次循环
[root@localhost ~]#vim continue.sh
 
#!/bin/bash
for j in {1.5}
do
  for i in {1..10}
  do
     if [ $i -eq 5 ]
     then
     continue
     fi
    echo i=$i
  done
echo "-----------------"
done
 
 
[root@localhost ~]#bash continue.sh 
i=1
i=2
i=3
i=4
i=6
i=7
i=8
i=9
i=10
-----------------
 4.3exit——跳出整个脚本
[root@localhost ~]#vim exit.sh
 
#!/bin/bash
for i in {1..10}
do
if [ $i -eq 5 ]
then
echo i=$i
fi
echo "hello"
done
 
[root@localhost ~]#bash exit.sh 
hello
hello
hello
hello
i=5
hello
hello
hello
hello
hello
hello
 
 
 
 
 
[root@localhost ~]#vim exit.sh
 
#!/bin/bash
for i in {1..10}
do
if [ $i -eq 5 ]
then
echo i=$i
fi
echo "hello"
exit
done
 
[root@localhost ~]#bash exit.sh 
hello

 5.截取网卡流量

[root@localhost opt]#ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.241.11  netmask 255.255.255.0  broadcast 192.168.241.255
        inet6 fe80::de6f:32c8:5a64:a6b2  prefixlen 64  scopeid 0x20
        ether 00:0c:29:51:4b:b5  txqueuelen 1000  (Ethernet)
        RX packets 31858  bytes 2477287 (2.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19352  bytes 2764635 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
[root@localhost opt]#ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.241.11  netmask 255.255.255.0  broadcast 192.168.241.255
        inet6 fe80::de6f:32c8:5a64:a6b2  prefixlen 64  scopeid 0x20
        ether 00:0c:29:51:4b:b5  txqueuelen 1000  (Ethernet)
        RX packets 31862  bytes 2477595 (2.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19356  bytes 2765571 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
 
[root@localhost opt]#ifconfig ens33|grep "RX pa"
        RX packets 31957  bytes 2484729 (2.3 MiB)
[root@localhost opt]#ifconfig ens33|grep "RX pa"|awk '{print $3}'
32017
[root@localhost opt]#ifconfig ens33|grep "RX pa"|awk '{print $3}'
32025

因为网卡流量是不断变化的    所以每次截取的流量不同

6.until——当不成立时才会执行

6.1求和
[root@localhost ~]#vim until.sh
 
#!/bin/bash
 
sum=0
i=0
until [ $i -gt 100 ]
do
sum=$[i+sum]
 
let i++
 
done
echo $sum
 
 
[root@localhost ~]#bash until.sh 
5050
6.2约吗
  • 为指定用户发送在线消息
  • 若用户不在线(未登录系统),则每10分钟试一次,直至用户登录系统后再发送消息
  • 用户名与消息通过位置参数传递给脚本
[root@localhost ~]#vim until.sh 
 
#!/bin/bash
#判断输入的参数是否满足条件
if [ $# -lt 2 ] &>/dev/null
then
echo "位置参数不足,需要在脚本后面添加用户名和发送消息"
exit 1
fi
 
 
#判断用户是否存在
if  grep -q "^$1:" /etc/passwd
then :
#   :表示打印一个空行  否则该命令有错误
else
echo -e "用户不存在\t"
exit 1
fi
 
 
#判断用户是否在线
until who |grep $1  &>/dev/null
do
echo "用户不在线"
sleep 5
done
 
echo $2 | write ghd
 
 
[root@localhost ~]#bash until.sh ghd hejiu
#用户未登录会每五秒提示“用户不在线”
用户不在线
用户不在线
用户不在线
用户不在线
write: wyq has messages disabled
 
[root@localhost ~]#bash until.sh ghd hejiu
#信息会显示在配置文件中的客户终端

shell脚本第三章循环语句_第5张图片

你可能感兴趣的:(linux,运维,服务器)