Shell script application (three)-case statement, for loop statement, while loop statement

for loop statement

The structure of the for statement
reads different variable values ​​to execute the same group of commands one by one

for 变量名 in 取值列表
do
   命令序列
done

There are many ways to select values ​​in the value list. For example, you can read the value after in directly, and it is separated by spaces by default.
Insert picture description here
1. Use the for statement to create a script
[Add users in batches]
1). The user name is stored in the 1.txt file, one per line

[root@localhost ~]# vim 1.txt
zhangsan
lisi
wangwu
xiaoming

2). Create a for loop statement script

[root@localhost ~]# vim 1.sh
#!/bin/bash
for a in `cat /root/1.txt`
#从列表文件读取用户名
do
useradd $a &>/dev/null
#通过管道指定密码字串
echo "123" | passwd --stdin $a &>/dev/null
done

3). Execute the script

[root@localhost ~]# chmod +x 1.sh 
[root@localhost ~]# ./1.sh 
[root@localhost ~]# tail -4 /etc/passwd
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
xiaoming:x:1004:1004::/home/xiaoming:/bin/bash

[Check the host status based on the IP address]
1). The IP address is stored in the 2.txt file, one per line

[root@localhost ~]# vim 2.txt
192.168.1.1
192.168.1.110
192.168.1.2

2). Create a for loop statement script

[root@localhost ~]# vim 2.sh
#!/bin/bash
for b in $(cat /root/2.txt)
do
ping -c 3 -i 0.2 -W 3 $b &>/dev/null
#【嵌套if语句判断连通性】
if [ $? -eq 0 ]
then
echo "Host $b is up"
else
echo "Host $b is down"
fi
done

3). Execute the script

[root@localhost ~]# chmod +x 2.sh 
[root@localhost ~]# ./2.sh 
Host 192.168.1.1 is down
Host 192.168.1.110 is up  因为虚拟机IP是1.110,所以可以通信
Host 192.168.1.2 is down

【99 Multiplication Table】

[root@localhost ~]# vim 99.sh
#!/bin/bash
for a in {
    
    1..9}
do
for b in {
    
    1..9}
do
if [ $a -ge $b ]
then
#echo -e表示支持反斜线控制的字符转换
#echo -n表示输出结果后不换行
#\t表示制表符,让形式看起来更美观
echo -ne "$a*$b=$[$a*$b]\t"
fi
done
echo ""
done
[root@localhost ~]# sh 99.sh 
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

while loop statement

The structure of the while statement
repeatedly tests a certain condition. As long as the condition is established, the while statement is executed repeatedly.
There are two special condition test operations, true (true) and false (false). Only when the condition test is true, the command sequence will be executed .

while  条件测试操作
do
  命令序列
done

Insert picture description here
【Add users in batches】

[root@localhost ~]# vim 3.sh
#!/bin/bash
PREFIX=“stu”
i=1
#循环条件:序号<=20
while [ $i -le 20 ]
do
useradd ${
    
    PREFIX}$i &>/dev/null
echo "123" | passwd --stdin ${
    
    PREFIX}$i &>/dev/null
#序号递增,避免死循环
let i++
done
[root@localhost ~]# chmod +x 3.sh 
[root@localhost ~]# sh 3.sh 
[root@localhost ~]# cat /etc/passwd | tail -20
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

[Guess the commodity price game]
Obtain a random number through the variable RANDOM to prompt the user to guess and record the number of times. After the guess is successful
, exit the loop

[root@localhost ~]# vim 4.sh
#!/bin/bash
A=$(expr $RANDOM % 100)
i=0
echo "商品实际价格为0-99之间,猜猜看是多少?"
while true   #循环条件:true(真)
do
read -p "请输入你认为的价格:" B
let i++
#提示猜测并记录次数
if [ $B -eq $A ];then
echo "恭喜你,猜对了"
echo "你总共猜了 $i 次"
exit 0   #若猜中退出脚本
#与实际价格比较,给出提示
elif [ $B -gt $A ];then
echo "太高了"
else
echo "太低了"
fi
done
[root@localhost ~]# chmod +x 4.sh
[root@localhost ~]# ./4.sh
[root@localhost ~]# sh 4.sh 
商品实际价格为0-99之间,猜猜看是多少?
请输入你认为的价格:50
太高了
请输入你认为的价格:30
太高了
请输入你认为的价格:20
太低了
请输入你认为的价格:23
恭喜你,猜对了
你总共猜了 4 

case branch statement

It compares values ​​according to different variables, and then executes different command operations for different values
. It is suitable for multiple branches and is a multi-branch statement

case  变量值  in
模式1)
    命令序列1
    ;;
模式2)
    命令序列2
    ;;
 ……
* )
    默认命令序列
esac


case的行尾必须为单词"in",每一个模式都要以右括号")"结束。
双分号";;"表示命令序列结束。
模式字符串中,可以用方括号表示一个连续的范围,如"[0-9]";还可以用竖杠符号"|"表示或,如"A|B"。
"*)"表示默认模式,其中的"*"相当于通配符
case支持通配符:
*:任意长度字符
?:任意单个字符
[]:指定范围内的任意单个字符
Insert picture description hereCase statement execution flow control 2. Case statement application example 1
1). Check the type of character entered by the user,
prompt the user to enter a character to
determine whether the character is a letter, number or other character

[root@localhost ~]# vim 5.sh
#!/bin/bash
read -p "请输入一个字符,并按Enter键确认:" A
case $A in
[0-9])  #匹配数字0-9
echo "你输入的是数字"
;;
[a-z]|[A-Z])  #匹配小写或大写字母
echo "你输入的是字母"
;;
*)
echo "你输入的是空格、功能键或其他控制字符"
;;
esac

2). Execute the script

[root@localhost ~]# chmod +x 5.sh 
[root@localhost ~]# sh 5.sh
[root@localhost ~]# sh 5.sh 
请输入一个字符,并按Enter键确认:6
你输入的是数字
[root@localhost ~]# sh 5.sh 
请输入一个字符,并按Enter键确认:y
你输入的是字母
[root@localhost ~]# sh 5.sh 
请输入一个字符,并按Enter键确认:@
你输入的是空格、功能键或其他控制字符

3. Case statement application example 2
1). Write a script to enable apache service

[root@localhost ~]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim fuwu.sh
#!/bin/bash
case $1 in
start)
systemctl $1 httpd
echo "httpd服务已启用"
;;
stop)
systemctl $1 httpd
echo "httpd服务已关闭"
;;
restart)
systemctl $1 httpd
echo "服务已重启"
;;
status)
netstat -anpt | grep httpd &>/dev/null
if [ $? -eq 0 ]
then
echo "httpd监听端口:$(netstat -anpt | grep httpd | awk '{print $4}')"
else
echo "httpd未启用"
fi;;
*)
echo "只能使用,start,stop,restart,status参数"
esac

2). Execute the script

[root@localhost ~]# chmod +x fuwu.sh 
[root@localhost ~]# ./fuwu.sh start
httpd服务已启用
[root@localhost ~]# ./fuwu.sh stop
httpd服务已关闭
[root@localhost ~]# ./fuwu.sh restart
服务已重启
[root@localhost ~]# ./fuwu.sh status
httpd监听端口::::80
[root@localhost ~]# ./fuwu.sh aaa
只能使用,start,stop,restart,status参数

Guess you like

Origin blog.csdn.net/weixin_46902396/article/details/108520740