case和for、while循环详解

  • case—流程控制语句
  • 循环语句
  • while循环语句和循环嵌套
  • 实战-3个shell脚本

一、流程控制语句:case

控制语句:用来实现对程序流程的选择、循环、转向和返回进行控制。case是开关语句的一个组成部分;它是根据变量的不同进行取值比较,然后针对不同的取值分别执行不同的命令操作
适用于多分支,是一个多选择语句
语法结构:
case 变量或表达式 in
变量或表达式1)
命令序列1
;;
变量或表达式2)
命令序列2
;;
*)
默认命令
esac
case语句的控制流程图:
在这里插入图片描述

例1:编写一个操作文件的脚本

[root@xuegod140 ~]# vim case-1.sh
[root@xuegod140 ~]# cat case-1.sh
#!/bin/bash
cat <<eof
-------------------
—1.backup—
—2.copy—
—3.exit—
eof
read -p "Please Enter a Num: " OP
case $OP in
1)
echo “backup…”
;;
2)
echo “copy”
;;
3)
exit
;;
*)
echo “Please Enter a Num 1-3”
esac
[root@xuegod140 ~]# sh case-1.sh
-------------------
—1.backup—
—2.copy—
—3.exit—
Please Enter a Num: 1
backup…
[root@xuegod140 ~]# sh case-1.sh
-------------------
—1.backup—
—2.copy—
—3.exit—
Please Enter a Num: 2
copy
[root@xuegod140 ~]# sh case-1.sh
-------------------
—1.backup—
—2.copy—
—3.exit—
Please Enter a Num: 3
[root@xuegod140 ~]# sh case-1.sh
-------------------
—1.backup—
—2.copy—
—3.exit—
Please Enter a Num: 4
Please Enter a Num 1-3

例2:编写一个apache服务启动脚本

[root@xuegod140 ~]# vim case-2.sh
[root@xuegod140 ~]# cat case-2.sh
#!/bin/bash
#Scripts is apache start
read -p "Please Enter start|stop|restart|status: " AA
case $AA in
start)
systemctl $AA httpd
;;
stop)
systemctl $AA httpd
;;
status)
systemctl $AA httpd
;;
restart)
systemctl $AA httpd
;;
*)
echo “USAGE:$0 start|stop|start”
esac
[root@xuegod140 ~]# sh case-2.sh
Please Enter start|stop|restart|status: start

二、for循环语句
1、语法结构
for var in list
do
commands
done
或者
for var in list ; do
commands
done
在这里插入图片描述
注:for循环有次数限制

2、list取值列表的多种方法
1)可以直接在in后面跟上多个值,以空格隔开

[root@xuegod140 ~]# vim for-1.sh
[root@xuegod140 ~]# cat for-1.sh
#!/bin/bash
for i in aa bb cc dd
do
echo “This Text is $i”
done
[root@xuegod140 ~]# sh for-1.sh
This Text is aa
This Text is bb
This Text is cc
This Text is dd

2)列表中的复杂值,可以使用引号或者转义符\来加以约束

[root@xuegod140 ~]# vim for-2.sh
[root@xuegod140 ~]# cat for-2.sh
#!/bin/bash
for i in aa bb ‘hello world’ “cc dd” I’s $b;do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-2.sh
The Text is aa
The Text is bb
The Text is hello world
The Text is cc dd
The Text is I’s
The Text is $b

3)从变量中取值

[root@xuegod140 ~]# vim for-3.sh
[root@xuegod140 ~]# cat for-3.sh
#!/bin/bash
var1=“aa bb cc dd”
for i in $var1;do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-3.sh
The Text is aa
The Text is bb
The Text is cc
The Text is dd

4)从命令中取值
第一种方法:$()调用命令

[root@xuegod140 ~]# vim for-4.sh
[root@xuegod140 ~]# cat for-4.sh
#!/bin/bash
for i in $(cat /etc/passwd);do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-4.sh
The Text is root❌0:0:root:/root:/bin/bash
The Text is bin❌1:1:bin:/bin:/sbin/nologin
The Text is daemon❌2:2:daemon:/sbin:/sbin/nologin

第二种方式:``调用命令

[root@xuegod140 ~]# vim for-4.sh
[root@xuegod140 ~]# cat for-4.sh
#!/bin/bash
#for i in $(cat /etc/passwd);do
for i in cat /etc/passwd;do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-4.sh
The Text is root❌0:0:root:/root:/bin/bash
The Text is bin❌1:1:bin:/bin:/sbin/nologin

3、自定义shell分隔符
默认情况下,shell是以空格、制表符、换行符作为分隔符、听过IFS来自定义为分隔符
指定单个字符作为分隔符:
IFS=: #以冒号作为分隔符
可以指定多个
IFS=’\n’:;” #以\,n,:,;,”,作为分隔符
注意:
IFS=’\n’ 表示以\和n作为分隔符
IFS=$’\n’ 表示以\n即换行符作为分隔符
例1:以换行符作为

[root@xuegod140 ~]# cat for-4.sh
#!/bin/bash
#for i in $(cat /etc/passwd);do
IFS=$’\n’
for i in `cat /etc/hosts`;do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-4.sh
The Text is 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
The Text is ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

例2:以空格作为分隔符

[root@xuegod140 ~]# vim for-4.sh
[root@xuegod140 ~]# cat for-4.sh
#!/bin/bash
#for i in $(cat /etc/passwd);do
for i in `cat /etc/hosts`;do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh for-4.sh
The Text is 127.0.0.1
The Text is localhost
The Text is localhost.localdomain
The Text is localhost4
The Text is localhost4.localdomain4
The Text is ::1
The Text is localhost
The Text is localhost.localdomain
The Text is localhost6
The Text is localhost6.localdomain6

4、C语言风格的for循环
语法格式:
for ((i=0;i<10;i++))
do
commands
done
例1:输入单个变量。输出1-10之间的数字

[root@xuegod140 ~]# vim c-1.sh
[root@xuegod140 ~]# cat c-1.sh
#!/bin/bash
for ((i=1;i<=10;i++));do
echo “The Text is $i”
done
[root@xuegod140 ~]# sh c-1.sh
The Text is 1
The Text is 2
The Text is 3
The Text is 4
The Text is 5
The Text is 6
The Text is 7
The Text is 8
The Text is 9
The Text is 10

例2:另外几种自增的写法

[root@xuegod140 ~]# vim c-1.sh
[root@xuegod140 ~]# cat c-1.sh
#!/bin/bash
#for ((i=1;i<10;i++));do
for ((i=1;i<10;));do
echo “The Text is $i”
\let i++
#i=$((i+1))
#i=$(expr $i + 1)
done
[root@xuegod140 ~]# sh c-1.sh
The Text is 1
The Text is 2
The Text is 3
The Text is 4
The Text is 5
The Text is 6
The Text is 7
The Text is 8
The Text is 9

例3:多个变量同时输出1-9的升序和降序

[root@xuegod140 ~]# cat c-2.sh
#!/bin/bash
for ((i=1,j=9;i<10;i++,j–))
do
echo num is $i–$j
done
[root@xuegod140 ~]# sh c-2.sh
num is 1–9
num is 2–8
num is 3–7
num is 4–6
num is 5–5
num is 6–4
num is 7–3
num is 8–2
num is 9–1

例4: 99乘法表

[root@xuegod140 ~]# vim 99.sh
[root@xuegod140 ~]# cat 99.sh
#!/bin/bash
for i in $(seq 9);do
for j in $(seq $i);do
sum=$(($i*$j))
echo -n "$i * $j = $sum "
done
echo " "
done
[root@xuegod140 ~]# 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循环语句和循环嵌套
1、语法格式
重复测试指令的条件,只要条件成立就反复执行对应的命令操作,直到命令不成立或为假
while 测试命令
do
命令
done
在这里插入图片描述

例1:降序输出10-1的数字

[root@xuegod140 ~]# vim while-1.sh
[root@xuegod140 ~]# cat while-1.sh
#!/bin/bash
var=10
while [ $var -gt 0 ];do
echo\ $var
#var=$((var-1))
var=$[$var-1]
#let var–
done
[root@xuegod140 ~]# sh while-1.sh
10
9
8
7
6
5
4
3
2
1

例2:输出两个数字想乘的结果

[root@xuegod140 ~]# cat while-2.sh
#!/bin/bash
var1=1
while [ $var1 -le 10 ];do
sum=$(( $var1*$var1))
echo “$var1*$var1 = $sum”
var1=$((var++))
done
[root@xuegod140 ~]# sh while-2.sh
11 = 1
0
0 = 0
11 = 1
2
2 = 4
33 = 9
4
4 = 16
55 = 25
6
6 = 36
77 = 49
8
8 = 64
99 = 81
10
10 = 100

2、循环嵌套
例1:批量创建用户
先创建一个用户文件

[root@xuegod140 opt]# vim a.txt
[root@xuegod140 opt]# cat a.txt
aa
bb
cc
dd

编写脚本
方法1:

[root@xuegod140 opt]# vim adduser.sh
[root@xuegod140 opt]# cat adduser.sh
#!/bin/bash
for i in cat /opt/a.txt;do
if grep $i /etc/passwd &>/dev/null;then
echo “user $i is exits…”
else
useradd $i
echo “123456” | passwd --stdin $i &>/dev/null
fi
done

方法二:

[root@xuegod140 opt]# vim adduser1.sh
[root@xuegod140 opt]# cat adduser1.sh
#!/bin/bash
for name in $(cat /opt/a.txt);do
id $name &>/dev/null
if [ $? -ne 0 ];then
useradd $name
echo “123456” | passwd --stdin $name &>/dev/null
echo “user $name created…”
else
echo “user $name is exist…”
fi
done
[root@xuegod140 opt]# sh adduser1.sh
user aa created…
user bb created…
user cc created…
user dd created…

四、实战-3个shell脚本
1、将/var/log目录下的所有日志文件全自动打包到/opt/backup/下,并以日期命令

[root@xuegod140 opt]# vim log-back.sh
[root@xuegod140 opt]# cat log-back.sh
#!/bin/bash
backupdir=/var/log/
backupfilename=/opt/backup/date +%Y%m%d
if [ ! -d $backupfilename ];then
mkdir -p $backupfilename
fi
for i in find $backupdir -name "*.log";do
tar zcvf ${i}.tar.gz $i &>/dev/null
done
mv /var/log/*.tar.gz $backupfilename
ls $backupfilename -lh
echo “The scripts exec end, Files tar sucessfully~”
[root@xuegod140 opt]# sh log-back.sh
total 36K
-rw-r–r-- 1 root root 3.8K May 4 00:40 boot.log.tar.gz
-rw-r–r-- 1 root root 2.9K May 4 00:40 vmware-vmsvc.log.tar.gz
-rw-r–r-- 1 root root 1.3K May 4 00:40 vmware-vmusr.log.tar.gz
-rw-r–r-- 1 root root 167 May 4 00:40 wpa_supplicant.log.tar.gz
-rw-r–r-- 1 root root 5.5K May 4 00:40 Xorg.0.log.tar.gz
-rw-r–r-- 1 root root 4.9K May 4 00:40 Xorg.9.log.tar.gz
-rw-r–r-- 1 root root 240 May 4 00:40 yum.log.tar.gz
The scripts exec end, Files tar sucessfully~
[root@xuegod140 opt]# ls backup/20190504/
boot.log.tar.gz vmware-vmsvc.log.tar.gz vmware-vmusr.log.tar.gz wpa_supplicant.log.tar.gz Xorg.0.log.tar.gz Xorg.9.log.tar.gz yum.log.tar.gz

2、实战-找出192.168.1.1-10网段中,服务器已经关机的IP地址

[root@xuegod140 opt]# vim ping.sh
[root@xuegod140 opt]# cat ping.sh
#!/bin/bash
i=1
for ((i=1;i<=10;i++))
do
ping -c 3 192.168.1.$i &>/dev/nul
if [ $? -ne 0 ];then
echo “192.168.1.$i is shutdown”
else
echo “192.168.1.$i is no shutdown”
fi
done
[root@xuegod140 opt]# sh ping.sh
192.168.1.1 is no shutdown
192.168.1.2 is shutdown
192.168.1.3 is shutdown
192.168.1.4 is shutdown
192.168.1.5 is shutdown
192.168.1.6 is shutdown
192.168.1.7 is shutdown
192.168.1.8 is shutdown
192.168.1.9 is shutdown
192.168.1.10 is shutdown

3、批量创建账号并生成随机密码

[root@xuegod140 opt]# vim adduser2.sh
[root@xuegod140 opt]# cat adduser2.sh
#!/bin/bash
for name in `cat /opt/a.txt`;do
id $name &>/dev/null
if [ $? -ne 0 ];then
useradd $name
PASS=`date +%s|md5sum|cut -c 1-8`
sleep=1
echo “$name:$PASS” >> /tmp/passwd.log
echo “$PASS” | passwd --stdin $name &>/dev/null
else
echo “user $name is exist…”
fi
done
[root@xuegod140 opt]# sh adduser2.sh
user aa is exist…
user bb is exist…
user cc is exist…
user dd is exist…
[root@xuegod140 opt]# userdel -r aa
[root@xuegod140 opt]# userdel -r bb
[root@xuegod140 opt]# userdel -r cc
[root@xuegod140 opt]# userdel -r dd
[root@xuegod140 opt]# sh adduser2.sh
[root@xuegod140 opt]# ls /home/
aa bb cc dd zx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值