if控制语句的整数比较

-eq 等于则为真 [ 1 -eq 1 ]

-ne 不等于

-ge 大于等于

-le 小于等于

-gt 大于

-lt 小于

[root@shell /scripts]# [ 1 -eq 1 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 11 -eq 1 ] && echo "为真"  || echo "为假"

为假

[root@shell /scripts]# [ 11 -ne 1 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 1 -ne 1 ] && echo "为真"  || echo "为假"

为假

[root@shell /scripts]# [ 1 -ge 1 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 1 -ge 2 ] && echo "为真"  || echo "为假"

为假

[root@shell /scripts]# [ 1 -le 2 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 11 -le 2 ] && echo "为真"  || echo "为假"

为假

[root@shell /scripts]# [ 11 -gt 2 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 1 -gt 2 ] && echo "为真"  || echo "为假"

为假

[root@shell /scripts]# [ 1 -lt 2 ] && echo "为真"  || echo "为假"

为真

[root@shell /scripts]# [ 11 -lt 2 ] && echo "为真"  || echo "为假"

为假

#编写一个脚本,判断一个服务是否在运行

systemctl  status  nginx  查看服务的是否在运行

返回值等于0  服务正在运行

返回值等于3  服务没有在运行

返回值等于4  没有这个服务

[root@shell /scripts]# cat services.sh

#!/bin/bash

#1.提示用户输入要检测的服务名称

read -p "请输入你要检测的服务名称:" Ser

#2.判断服务所在的状态

systemctl status  $Ser  &>/dev/null

Rc=$?

#3.根据返回值的结果进行判断

if [ $Rc -eq 0 ];then

    echo "服务$Ser 正在运行中........"

elif [ $Rc -eq 3 ];then

    echo "服务$Ser 没有运行.........."

elif [ $Rc -eq 4 ];then

    echo "您所输入的服务,系统没有安装。"

else

    echo "您输入的服务名称不符合规范."

fi

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:nginx

您所输入的服务,系统没有安装。

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:mariadb

服务mariadb 没有运行..........

[root@shell /scripts]# systemctl  start mariadb.service

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:mariadb

服务mariadb 正在运行中........

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:firewalld

服务firewalld 没有运行..........

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:sshd

服务sshd 正在运行中........

[root@shell /scripts]# sh  services.sh

请输入你要检测的服务名称:ssh

您所输入的服务,系统没有安装。

#判断磁盘根分区的使用率,如果使用率大于80,则发邮件报警

1.什么命令查看磁盘的使用率

df -h | awk  '/\/$/{print $(NF-1)}'

[root@shell /scripts]# cat  disk.sh

#!/bin/bash

#1.获取磁盘根分区的使用率

Disk_Use=$(df -h | awk  '/\/$/{print $(NF-1)}')

#2.根据使用率值的大小进行判断是否触发报警

if [ ${Disk_Use/\%/} -gt 6 ];then

    echo "当前系统的根分区的使用率大于80%,使用率为$Disk_Use"

else

    echo "当前系统根分区的使用率正常,使用率为$Disk_Use"

fi

[root@shell /scripts]# sh disk.sh

当前系统根分区的使用率正常,使用率为5%

[root@shell /scripts]# sh disk.sh

当前系统的根分区的使用率大于80%,使用率为9%

[root@shell ~]# dd  if=/dev/zero of=/root/test.log  bs=100M count=100

#判断内存的使用率,大于80%则报警

#创建用户的脚本

1. 怎么创建用户  useradd

2. 如果用户已经存在,则不需要创建

3. 创建完用户,判断用户是否创建成功

[root@shell /scripts]# cat  useradd.sh

#!/bin/bash

#1.提示用户输入要创建的用户

read -p "请输入你要创建的用户:" User

#2. 判断该用户是否存在该系统

id  $User &>/dev/null 

if [ $? -eq 0 ];then

    echo "用户$User 已经存在!"

else

    useradd $User &>/dev/null

    if [ $? -eq 0 ];then

        echo "用户$User 创建成功!"

    else

        echo "用户$User 创建失败!"

    fi

fi

[root@shell /scripts]# sh  useradd.sh

请输入你要创建的用户:mysql

用户mysql 已经存在!

[root@shell /scripts]# sh  useradd.sh

请输入你要创建的用户:www

用户www 创建成功!

[root@shell /scripts]# id www

uid=1000(www) gid=1000(www) groups=1000(www)

[root@shell /scripts]# sh  useradd.sh

请输入你要创建的用户:www

用户www 已经存在!

#使用函数库,测试网址是否能通。

[root@shell /scripts]# cat url.sh

#!/bin/bash

#0.调用函数库

[ -f /etc/init.d/functions ] &&  source /etc/init.d/functions

#1.提示用户输入要进行测试的url网址

read -p  "请输入你要测试的Url地址:" Url

#2. 判断该网址是否能通

ping  -c1 -W1 $Url &>/dev/null

#3.根据返回值判断网址是否通畅

if [ $? -eq 0 ];then

    action  "Url网址$Url 是通畅的......."  /bin/true

else

    action  "Url网址$Url 是不通畅的....."  /bin/false

fi

[root@shell /scripts]# sh url.sh

请输入你要测试的Url地址:www.baidu.com

Url网址www.baidu.com 是通畅的.......                      [  OK  ]

[root@shell /scripts]# sh url.sh

请输入你要测试的Url地址:www.baiduy.com

Url网址www.baiduy.com 是不通畅的.....                      [FAILED]

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容