正则表达式----流量控制

本文详细介绍了Linux Shell脚本中的流程控制结构,包括if条件语句(单分支、双向分支、多分支)、case语句,以及循环控制的for、while和until循环,通过实例展示了各种结构的用法,适用于Shell脚本编写和系统管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、if条件语句

1、(单分支)if条件语句

if [ 条件判断式 ];then
程序
fi
或者
if [ 条件判断式 ]
then
程序
fi

查看一下根分区

[root@localhost ~]# cd /bin/
[root@localhost bin]# df -h        查看一下根分区
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   37G  928M   37G   3% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.7M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0
[root@localhost bin]# df -h |grep /dev/mapper/centos-root
/dev/mapper/centos-root   37G  928M   37G   3% /
[root@localhost bin]# df -h |grep /dev/mapper/centos-root |awk '{print $5}'     提取(过滤)3%
3%
[root@localhost bin]# df -h |grep /dev/mapper/centos-root |awk '{print $5}' |cut -d "%" -f 1
3

判断分区使用率

[root@localhost bin]# vi disk1.sh
统计根分区使用率
#!/bin/bash
把根分区使用率作为变量值赋予变量rate
rate=$(df -h |grep /dev/mapper/centos-root |awk '{print $5}' |cut -d "%" -f 1)
if [ $rate -ge 3 ]
       then
             echo "waring / rate>=80 $rate"
fi
[root@localhost bin]# bash disk1.sh
waring / rate>=80 3

[root@localhost bin]# vi disk1.sh
#!/bin/bash
rate=$(df -h |grep /dev/mapper/centos-root |awk '{print $5}' |cut -d "%" -f 1)
if [ $rate -ge 3 ]
       then
             echo "waring / rate>=$rate"
fi
[root@localhost bin]# bash disk1.sh
waring / rate>=3

2、双向分支if条件语句

if [ 条件判断式 ]
then
条件成立时,执行的程序
else
条件不成立时,执行的另一个程序
fi

备份

[root@localhost bin]# yum install -y ntpdate
[root@localhost bin]# ntpdate asia.pool.ntp.org
16 Apr 11:04:13 ntpdate[10265]: step time server 80.241.0.72 offset -2.543060 sec
[root@localhost bin]# ntpdate asia.pool.ntp.org &>/dev/null
[root@localhost bin]# date +%y%m%d
210416
[root@localhost bin]# date +%Y%m%d
20210416
[root@localhost bin]# du -sh /etc/
30M     /etc/

[root@localhost bin]# vi db.sh
#备份
#!/bin/bash
#同步时间
ntpdate asia.pool.ntp.org &>/dev/null
#把当前系统时间按照“年月日”格式赋予变量date
date=$(date +%Y%m%d)
#统计mysql数据库的大小,并把大小赋予size变量              
size=$(du -sh /etc)

if    [ -d /tmp/dbbak ]
      then
           echo "Date: $date!" > /tmp/dbbak/dbinfo.txt
           echo "Date size: $size!" >> /tmp/dbbak/dbinfo.txt
           cd /tmp/dbbak      进入路径
           tar -zcf etc$date.tar.gz /etc dbinfo.txt &>/dev/null        打包
           rm -rf /tmp/dbbak/dbinfo.txt
      else
           mkdir /tmp/dbbak
           echo "Date: $date!" > /tmp/dbbak/dbinfo.txt
           echo "Date size: $size!" >> /tmp/dbbak/dbinfo.txt
           cd /tmp/dbbak
           tar -zcf etc$date.tar.gz /etc dbinfo.txt &>/dev/null
           rm -rf /tmp/dbbak/dbinfo.txt
fi
[root@localhost bin]# bash db.sh
[root@localhost bin]# ls /tmp/dbbak/
etc20210416.tar.gz

判断apache是否启动

[root@localhost bin]# yum install -y nmap
[root@localhost bin]# yum install -y httpd
[root@localhost bin]# systemctl start httpd
[root@localhost bin]# nmap
[root@localhost bin]# nmap -sT 192.168.200.10

Starting Nmap 6.40 ( http://nmap.org ) at 2021-04-16 15:33 CST
Nmap scan report for 192.168.200.10
Host is up (0.00068s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 13.10 seconds
[root@localhost bin]# ps aux |grep httpd
root      10484  2.0  0.2 221936  4976 ?        Ss   15:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache    10485  0.0  0.1 221936  2956 ?        S    15:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache    10486  0.0  0.1 221936  2956 ?        S    15:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache    10487  0.0  0.1 221936  2956 ?        S    15:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache    10488  0.0  0.1 221936  2956 ?        S    15:37   0:00 /usr/sbin/httpd -DFOREGROUND
apache    10489  0.0  0.1 221936  2956 ?        S    15:37   0:00 /usr/sbin/httpd -DFOREGROUND
root      10491  0.0  0.0 112660   968 pts/1    S+   15:37   0:00 grep --color=auto httpd
[root@localhost bin]# nmap -sT 192.168.200.10 |grep tcp
22/tcp open  ssh
80/tcp open  http
[root@localhost bin]# nmap -sT 192.168.200.10 |grep tcp |grep http
80/tcp open  http
[root@localhost bin]# nmap -sT 192.168.200.10 |grep tcp |grep http |awk '{print $2}'
open
[root@localhost bin]# echo $?
0

3、多分支if条件语句

if [ 条件判断式1 ]
then
当条件判断式1成立时,执行程序1
elif [ 条件判断式2 ]
then
当条件判断式2成立时,执行程序2 „省略更多条件…
else
当所有条件都不成立时,最后执行此程序
fi

判断用户输入的是什么文件

[root@localhost bin]# vi db1.sh
#!/bin/bash
read -p "Please input a filename: " file
#接收键盘的输入,并赋予变量file
if [ -z "$file" ] #判断file变量是否为空
then
 echo "Error,please input a filename"
 exit 1
 elif [ ! -e "$file" ] #判断file的值是否存在
then
 echo "Your input is not a file!"
 exit 2
elif [ -f "$file" ] #判断file的值是否为普通文件
then
 echo "$file is a regulare file!"
elif [ -d "$file" ] #判断file的值是否为目录文件
then
 echo "$file is a directory!"
else
 echo "$file is an other file!"
fi
[root@localhost bin]# bash db1.sh

二、case语句

case 语句和 if…elif…else 语句一样都是多分支条件语句。case 语句只能判断一种条件关系, 而 if
语句可以判断多种条件关系。

在这里插入图片描述

[root@jing ~]# mkdir sh      创建一个目录,运行语句
[root@jing ~]# ls
anaconda-ks.cfg  sh  student.txt
[root@jing sh]# yum install -y vim
[root@jing sh]# vim case.sh       进行编辑
#!/bin/bash
read -p "please choose yes/no:" -t 30 cho 
case $cho in
       "yes")
             echo "you choose is yes!"
              ;;
       "no")
             echo "you choose is no!"
              ;;
        *)
             echo "you choose is error!"
              ;;
esac
[root@jing sh]# bash case.sh    输入yes,就输出yes
please choose yes/no:yes
you choose is yes!

[root@jing sh]# bash case.sh
please choose yes/no:no
you choose is no!
[root@jing sh]# bash case.sh
please choose yes/no:jhgz
you choose is error!

三、for 循环

语句一:
for 变量 in 值1 值2 值3…
do
程序
done

打印时间

[root@jing sh]# vim for1.sh
#!/bin/bash
for time in morning noon afternoon evening     把morning赋值给time,变为morning
 do
 echo "This time is $time!"
 done
[root@jing sh]# bash for1.sh
This time is morning!
This time is noon!
This time is afternoon!
This time is evening!

批量解压缩脚本

[root@jing sh]# cd /opt
[root@jing opt]# ls     在/opt中上传一个小的压缩包
2  apache-tomcat-8.5.60.tar.gz
[root@jing sh]# ls
case.sh  for1.sh
[root@jing sh]# ls *.sh > ls.log
[root@jing sh]# ls
case.sh  for1.sh  ls.log
[root@jing sh]# cat ls.log
case.sh
for1.sh
[root@jing sh]# vim for2.sh
#!/bin/bash
cd /opt      进入脚本所在目录
ls *.tar.gz >ls.log      查看所有压缩文件,追加到ls.log 
for i in $(cat ls.log)       循环ls.log 里的内容
        do
             tar -zxf $i &>/dev/null     解压i变量,放到/dev/null下
        done
rm -rf /opt/ls.log
[root@jing sh]# bash for2.sh
[root@jing sh]# ls
case.sh  for1.sh  for2.sh  ls.log
[root@jing sh]# ls /opt/
2  apache-tomcat-8.5.60  apache-tomcat-8.5.60.tar.gz

语句二:
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done

从1加到100

[root@jing sh]# vim for3.sh
#!/bin/bash
s=0
for((i=1;i<=100;i=i+1))
       do
           s=$(($s+$i))
       done
echo "the sum of 1+2+...+100 is : $s"
[root@jing sh]# bash for3.sh
the sum of 1+2+...+100 is : 5050

结果:5050
批量添加指定数量的用户

[root@jing sh]# vim for4.sh
#!/bin/bash
read -p "Please input user name: " -t 30 name
read -p "Please input the number of users: " -t 30 num
read -p "Please input the password of users: " -t 30 pass
if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]          -z判断字符串是否为空,变量满足继续运行
 then
 y=$(echo $num | sed 's/[0-9]//g')
 if [ -z "$y" ]
 then
 for (( i=1;i<=$num;i=i+1 ))
 do
 /usr/sbin/useradd $name$i &>/dev/null
 echo $pass | /usr/bin/passwd --stdin $name$i &>/dev/null
 done
 fi
fi
[root@jing sh]# ls /etc/passwd    查看一下
/etc/passwd
[root@jing sh]# vi /etc/passwd      
[root@jing sh]# bash for4.sh  
Please input user name: user    创建用户
Please input the number of users: 5    用户个数为五个
Please input the password of users: 000000     密码
[root@jing sh]# vi /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
以下为新添的内容
user1:x:1000:1000::/home/user1:/bin/bash     
user2:x:1001:1001::/home/user2:/bin/bash
user3:x:1002:1002::/home/user3:/bin/bash
user4:x:1003:1003::/home/user4:/bin/bash
user5:x:1004:1004::/home/user5:/bin/bash

四、while 循环

while 循环是不定循环,也称作条件循环 。只要条件判断式成立,循环就会一直继续,直到
条件判断式不成立,循环才会停止。这就和 for 的固定循环不太一样了。

while [ 条件判断式 ]
do
程序
done

从1加到100

[root@localhost sh]# vim while1.sh
#!/bin/bash
i=1
s=0
while [ $i -le 100 ]      #如果变量i的值小于等于100,则执行循环
 do
 s=$(( $s+$i ))
 i=$(( $i+1 ))
 done
echo "The sum is: $s"
[root@localhost sh]# bash while1.sh
The sum is: 5050

结果:5050

##五、until循环

until循环,和while循环相反,until循环时
只要条件判断式不成立则进行循环,并执
行循环程序。一旦循环条件成立,则终止
循环。

从1加到100

[root@localhost sh]# vim until1.sh    
#!/bin/bash
i=1
s=0
until [ $i -gt 100 ]   #循环直到变量i的值大于100,就停止循环
 do
 s=$(( $s+$i ))
 i=$(( $i+1 ))
 done
echo "The sum is: $s"
[root@localhost sh]# bash until1.sh
The sum is: 5050

结果:5050

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值