shell流程控制之条件判断

1、ping主机测试,查看主机是否存活;

[root@localhost test]# cat 1.sh
#!/bin/bash
read -p "please input ip" ip
ping -c 2 -w 1 $ip &> /dev/null
if [ $? -eq 0 ];then
  echo "host is running"
else
  echo "host is not running"
fi
[root@localhost rest]# bash 1.sh 
输入主机ip192.168.159.132
host is running

2、判断一个用户是否存在;

[root@localhost test] cat 2.sh
#!/bin/bash
read -p "please input username:" username
id $user &> /dev/null
if [ "$?" = 0 ]
then
  echo "$username already exist"
else
  echo "$username is not exist"
fi
[root@localhost test]# bash 2.sh 
please input username: root
root already exist

3、判断当前内核主版本是否为3,且次版本是否大于10;

[root@localhost test]# uname -r
5.14.0-162.6.1.el9_1.x86_64
[root@localhost test]# cat 3.sh 
#!/bin/bash
main={ `uname -r | cut -d "." -f1` }
minor={ `uname -r | cut -d "." -f2` }
if [ "$main" -eq 3 ];then
  echo "main version is :$main"
else
  echo "main version is not 3"
fi
if [ "$minor" -gt 10 ];then
  echo "minor version is more than 10,minor version is:$minor"
else
  echo "minor version is not more than 10,minor version is:$minor"
fi
[root@localhost test]# bash 3.sh 
main version is :3
minor version is not more than 10,minor version is:10

4、判断vsftpd软件包是否安装,如果没有则自动安装;

[root@localhost test]# cat 4.sh 
#!/bin/bash
vsftpd -v &> /dev/null
if [ "$?" -eq 0 ]
then
  echo "vsftpd already install"
else
  echo "vsftpd not install"
  yum install vsftpd -y &>/dev/null
fi
[root@localhost test]# bash 4.sh 
vsftpd already install

5、判断httpd是否运行;如果没有,则运行它,并且开机启动

[root@localhost test]# cat 5.sh 
#!/bin/bash
status=(`ps -ef | grep httpd | grep -v grep | wc -l`)
if [ "$status" -ge 1 ]
then
  echo "httpd is running"
else
  echo "httpd is not running"
  systemctl start httpd
  systemctl enable httpd.service
fi
[root@localhost test]# bash 5.sh 
httpd is running

6、判断指定的主机是否能ping通,必须使用$1变量;

[root@localhost test]# cat 6.sh 
#!/bin/bash
ping -c 2 $1 &>/dev/null
if [ $? -eq 0 ];then
  echo "主机可以ping通"
else
  echo "主机不可以ping通"
fi
[root@localhost test]# bash 6.sh 192.168.159.132
主机可以ping通

7、报警脚本,要求如下:
根分区剩余空间小于20%
内存已用空间大于80%
向用户alice发送告警邮件
配合crond每5分钟检查一次
[root@locaklhost ~]# echo “邮件正文” | mail -s “邮件主题” alice

[root@localhost test]# cat 7.sh
#!/bin/bash
used_root=(`df -hl | grep /dev/mapper | tr -s ' ' |cut -d ' ' -f5`)
total_mem=(`free -m | grep Mem | tr -s ' ' | cut -d ' ' -f2`)
used_mem=(`free -m | grep Mem | tr -s ' ' | cut -d ' ' -f3`)
free_rootper=( "$used_root/$total_root*100")
if [ "$used_root" < 20% -a "$free_rootper" > 80 "]
then
 `echo "警告" | mail -s "警告信息" alice
else
 echo "正常使用"
fi

8、判断用户输入的是否是数字,如果是数字判断该数字是否大于10;

[root@localhost test]# cat 8.sh
#!/bin/bash
read -p "please enter a character,press enter to continue:" str
if echo "$str" | grep "[0-9]" > /dev/null
then
 echo "input is number."
 if [ $str -gt 10 ]
 then
   echo "the number is more than 10."
 else
   echo "the number is less than 10."
 fi
else
 echo "input is not a number."
fi
[root@localhost test]# bash 8.sh
please enter a character,press enter to continue:13
input is number.
the number is more than 10.
[root@localhost test]# bash 8.sh
please enter a character,press enter to continue:2
input is number.
the number is less than 10.
[root@localhost test]# bash 8.sh
please enter a character,press enter to continue:af
input is not a number.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值