当前位置: 移动技术网 > 科技>操作系统>Linux > 分支与循环结构

分支与循环结构

2018年08月23日  | 移动技术网科技  | 我要评论
分支与循环结构 if语句是实际生产工作中最重要且最常用的语句,所以,必须掌握牢固。 if条件句 if条件句语法 单分支结构 语法 if [ 条件 ] then 指令 fi 或 if [ 条件 ];then 指令 fi 条件表达式[ -f "$file1" ]&& echo 1,相当于下面的if语句。... ...

分支与循环结构

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

 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网