分支与循环结构

分支与循环结构

if语句是实际生产工作中最重要且最常用的语句,所以,必须掌握牢固。

if条件句

if条件句语法
  • 单分支结构

语法

  1. if [ 条件 ]
  2.   then
  3.     指令
  4. fi
  5. if [ 条件 ];then
  6.     指令
  7. fi

条件表达式[ -f "$file1" ]&& echo 1,相当于下面的if语句。

  1. if [ -f "$file1" ];then
  2.     echo 1
  3. fi
  • 双分支结构
  1. if [ 条件 ]
  2.   then
  3.     指令集1
  4. else
  5.     指令集2
  6. fi

条件表达式[ -f "$file1" ]&& echo 1||echo 0,相当于双分支if [ -f "$file1" ];then echo 1;else echo 0;fi。

  • 多分支结构
  1. if 条件
  2.   then
  3.     指令
  4. elif 条件
  5.   then
  6.     指令
  7. elif 条件
  8.   then
  9.     指令
  10.     ...
  11.   else
  12.     指令
  13. fi
单分支if条件句

开发shell脚本判断系统剩余内存的大小,如果低于100M就邮件报警给管理员,并加入系统定时任务每三分钟执行一次检查。

free -m|awk 'NR==2{print $4}'

  1. [root@lamp ~]# cat free_m.sh
  2. #!/bin/bash
  3.  
  4. FREE=`free -m|awk 'NR==3{print $4}'`
  5. if [ $FREE -lt 100 ]
  6.   then
  7.     echo "warning:The available memory $FREE."
  8.     exit 0
  9. fi
  10. echo "The available memory $FREE."
双多分支if条件句

用if双分支实现read读入的方式比较两个数的大小。

  1. [root@lamp ~]# cat c3.sh
  2. #!/bin/bash
  3. read -p "Pls input two nums: " num01 num02
  4. [ -z $num01 ]&&{
  5.   echo "the num01 you input must be int."
  6.   exit 2
  7. }
  8. [ -z $num02 ]&&{
  9.   echo "the num02 you input must be int."
  10.   exit 2
  11. }
  12. expr $num01 + $num02 + 1 &>/dev/null
  13. [ $? -ne 0 ]&&{
  14.   echo "the num you input must be int."
  15.   exit 2
  16. }
  17. if [ $num01 -lt $num02 ]
  18.   then
  19.     echo "$num01 < $num02."
  20. elif [ $num01 -gt $num02 ]
  21.   then
  22.     echo "$num01 > $num02."
  23.   else
  24.     echo "$num01 = $num02."
  25. fi

用if双分支实现对nginx或mysql服务是否正常进行判断,使用进程数、端口、url的方式判断,如果进程没起,把进程启动。

web服务和数据库(mysql)的监控方法。

1、端口监控

本地监控:netstat、ss、lsof

远程监控:telnet、nmap、nc

telnet监控端口

  1. [root@lamp ~]# echo -e "\n"|telnet www.baidu.com 80|grep Connected|wc -l
  2. Connection closed by foreign host.
  3. 1

nmap监控端口

  1. [root@lamp ~]# nmap www.baidu.com -p 80|grep open|wc -l
  2. 1

nc监控端口

  1. [root@lamp ~]# nc -z 192.168.163.128 22|grep succeeded|wc -l
  2. 1

2、进程监控

本地监控:ps -ef|grep mysql|wc -l

3、wget、curl,http方式根据返回值或者返回内容判断。

4、header(http),http方式根据状态码判断。

5、数据库特有通过mysql客户端连接,根据返回值或者返回内容判断。

  1. [root@lamp ~]# cat check_db.sh
  2. #!/bin/bash
  3. #local
  4. if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]
  5. #if [ `ps -ef|grep mysql|grep -v grep|wc -l` -gt 0 ]
  6. #if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]
  7. #if [ `lsof -i tcp:3306|wc -l` -gt 0 ]
  8.  
  9. #remote
  10. #if [ `nmap 192.168.1.123 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ]
  11. #if [ `nc -w 2 192.168.1.123 3306 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ]
  12.   then
  13.     echo "Mysql is Running."
  14. else
  15.     echo "Mysql is Stopped."
  16.     /deta/mysql start
  17. fi

 

  1. [root@lamp ~]# cat check_web.sh
  2. #!/bin/bash
  3. if [ "`curl -I -s -o /dev/null -w "%{http_code}\n" http://192.168.1.123`" = "200" ]
  4. #if [ `curl -I http://192.168.1.123 2>/dev/null|head -1|egrep "200|302|301"|wc -l` -eq 1 ]
  5. #curl -s http://192.168.1.123 &>/dev/null
  6. #if [ $? -eq 0 ]
  7. #if [ "`curl -s http://192.168.1.123 &>/dev/null&&echo $?`" = "0" ]
  8. #if [ "`curl -s http://192.168.1.123`" = "bbs" ]
  9.   then
  10.     echo "httpd is running."
  11. else
  12.     echo "httpd is stopped."
  13. fi

 

通过传参的方式往/etc/user.conf里添加用户,具体要求如下:

1、命令用法:USAGE:sh adduser {-add|-del|-search} username

2、传参要求:如果参数为-add,表示添加后面接的用户名;如果参数为-del,表示删除后面接的用户名;如果参数为-search,表示查找后面接的用户名。

3、如果有同名的用户则不能添加,没有对应用户则无需删除,查找到用户以及没有用户时给出明确提示。

4、/etc/user.conf不能被所有外部用户之间删除或修改。

  1. [root@lamp ~]# cat user.sh
  2. #!/bin/bash
  3. ROOT_UID=0
  4.  
  5. if [ "$UID" -ne "$ROOT_UID" ]
  6.   then
  7.     echo "Mast be root to run this script."
  8.     exit 1
  9. fi
  10. if [ $# -ne 2 ]
  11.   then
  12.     echo "USAGE:sh $0 {-add|-del|-search} username."
  13.     exit 2
  14. fi
  15. check=$1
  16. name=$2
  17. if [ "$check" = "add" ]
  18.   then
  19.     result=`cat /etc/user.conf|grep -Fx "$name"`
  20.     [ -z $result ]&&{
  21.       echo "$name" >> /etc/user.conf
  22.       echo "user add "$name" is ok."
  23.       exit 0
  24.     }
  25.     echo "user $name is in."
  26.     exit 0
  27. elif [ "$check" = "del" ]
  28.   then
  29.     result=`cat /etc/user.conf|grep -Fx "$name"`
  30.     [ -z $result ]&&{
  31.       echo "user "$name" not find."
  32.       exit 0
  33.     }
  34.     sed -ri /^$name$/d /etc/user.conf
  35.     echo "user del "$name" is ok."
  36. elif [ "$check" = "search" ]
  37.   then
  38.     result=`cat /etc/user.conf|grep -Fx "$name"`
  39.     [ -z $result ]&&{
  40.       echo "user $name not find."
  41.       exit 0
  42.     }
  43.     echo $result
  44.     exit 0
  45. else
  46.     echo "USAGE:sh $0 {-add|-del|-search} username."
  47.     exit 1
  48. fi

 

获取文件md5值,防篡改。

  1. [root@lamp ~]# find ./ -type f|xargs md5sum

将md5值写入文件。

  1. [root@lamp ~]# find ./ -type f|xargs md5sum >/tmp/md5list

比较md5值。

  1. [root@lamp ~]# md5list -c /tmp/md5list

 

转载于:https://www.cnblogs.com/yinshoucheng-golden/p/9514892.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值