• shell编程实战学习(3)


    shell编程实战学习(3)

    一 、Shell if循环语句

    1.1.1 if 循环语句

    1. 单分支结构
    if [表达式] 
     then
      指令
    fi
    

    2.例子

    [root@web01 /server/scripts]# cat read.sh 
    #!/bin/bash 
    read -p "please input two nmb: " a b
    if [ -z "$a" -a -z "$b" ] 
     then
      echo "Usage:$0{num1 num2}"
     exit 0
    fi
    
    expr $a + $b + 9 &>/dev/null
    if [ $? -ne 0 ] 
     then 
      echo "Usage:$0{num1 num2} "
      exit 0 
    fi 
    echo "a+b=$((a+b))"
    echo "a*b=$((a*b))"
    echo "a/b=$((a/b))"
    echo "a-b=$((a-b))"
    echo "a%b=$((a%b))"
    echo "a**b=$((a**b))"
    ###################################
    [root@web01 /server/scripts]# sh read.sh 
    please input two nmb: 9 6
    a+b=15
    a*b=54
    a/b=1
    a-b=3
    a%b=3
    a**b=531441
    [root@web01 /server/scripts]# sh read.sh 
    please input two nmb: 
    Usage:read.sh{num1 num2}
    [root@web01 /server/scripts]# sh read.sh 
    please input two nmb: 1 l
    Usage:read.sh{num1 num2} 
    [root@web01 /server/scripts]# sh read.sh 
    please input two nmb: k 3
    Usage:read.sh{num1 num2} 
    
    1. 双分支结构
    if [表达式] 
     then
      指令
    else
    指令
    fi
    
    1. 例子
    [root@web01 /server/scripts]# cat read1.sh 
    #!/bin/bash 
    if [ -e /etc/localdir ] 
     then
      echo "this fiel exist"
    else
      echo "this file not exist"
    fi
    ======================================
    [root@web01 /server/scripts]# sh read1.sh 
    this file not exist
    [root@web01 /server/scripts]# mkdir /etc/localdir
    [root@web01 /server/scripts]# sh read1.sh 
    this fiel exist
    
    1. 监控内存报警
    [root@web01 /server/scripts]# cat memory_check.sh 
    #!/bin/bash
    #auto by chenhj 2020-4-15
    #mail 1010235677@qq.com
    #memcache check shell file
    MEM_CMD=`free -m|awk 'NR==2{print $7}'`
    if (($MEM_CMD < 500))
      then
        echo "$MEM_CMD < 500M"
        #echo "$MEM_CMD < 100M" | mail -s "Memory capacity warning" 1010235677@qq.com
    else
       echo  "Memory capacity $MEM_CMD"
       #echo  "$MEM_CMD" | mail -s "Memory capacity"  1010235677@qq.com
    fi
    ========================================
    [root@web01 /server/scripts]# sh memory_check.sh 
    Memory capacity 1697
    [root@web01 /server/scripts]# sh memory_check.sh 
    Memory capacity 1697
    

    5.多分支结构

    if [表达式] 
     then
      指令
    elif [表达式]
     then
      指令
    else
     指令
    fi
    
    1. 例子
    [root@web01 /server/scripts]# cat diff2.sh 
    #!/bin/bash
    expr $1 + $2 + 3 &>/dev/null
    [ $? -eq 0 ] ||{
     echo "Usage:$0 num1 num2"
     exit 0
    }
    if [ $1 -gt $2 ] 
      then
        echo "$1 > $2"
        exit 1
    elif [ $1 -lt $2 ] 
      then
        echo "$1 < $2"
        exit 2
     
    elif [ $1 -eq $2 ]
      then
       echo "$1 = $2"
    fi
    ============================================
    [root@web01 /server/scripts]# sh diff2.sh 1 3
    1 < 3
    [root@web01 /server/scripts]# sh diff2.sh 4 3
    4 > 3
    [root@web01 /server/scripts]# sh diff2.sh 4 4
    4 = 4
    [root@web01 /server/scripts]# sh diff2.sh 4 ll
    Usage:diff2.sh num1 num2
    

    二、Shell 函数介绍

    2.1.1 Shell 函数介绍

    1. 函数的功能之一就类似别名的作用,例如可以简化程序代码量,让程序更加易读,易改,易用。简单来说,函数的作用就是将程序里多次被调用的相同代码组合起来(函数体),并为其起名(即函数名),其它所有想要重复调用这部分代码的地方都只需要调用这个名字就可以了。当需要修改这部分重复代码时,也只需要改变函数体内的一部分代码即可实现所有调用的修改
    2. 函数的优势
      1、把相同的程序段定义成函数,可以减少整个程序的代码量,提升开发效率。
      2、增加程序的可读、易读,提升管理效率。
      3、可以实现程序功能模块化,是的程序具备通用性(可移植性)。

    2.1.1 函数语法

    1. 函数语法格式
    function 函数名() {  #function 可以省略
      指令集    
      return n    #与exit功能相似
    }
    
    1. 函数的调用执行
    直接调用函数名即可
    
    1. 例子
    [root@web01 /server/scripts]# cat func.sh 
    #!/bin/bash
    
    function linux(){
      echo "linux server"
    }
    
    for i in {1..5}
    do 
     linux
    done
    [root@web01 /server/scripts]# sh func.sh 
    linux server
    linux server
    linux server
    linux server
    linux server
    ======================================
    [root@web01 /server/scripts]# cat rsyncd.sh 
    #!/bin/bash
    # chkconfig: 2345 21 81
    # description: startup rsync scripts
    start(){
    	if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
    	   then
    	       :
    	   else
    	       rsync --daemon
    	fi
    }
    stop(){
        if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
        then
            kill `cat /var/run/rsyncd.pid`
        else
            echo "Failed to stop rsync Unit rsync not loaded."
            exit 1
        fi
    
    }
    main(){
    	if [ "$1" = "start" ]
    	then
    	   start
    	elif [ "$1" = "stop" ]
    	then
    	   stop
    	elif [ "$1" = "restart" ]
    	then
    	   $0 stop
    	   sleep 2
    	   $0 start
    	else
    	    echo "Usage;$0 {start|stop|restart}"
    	fi
    }
    main $*
    
    1. 函数传参
    函数后接的参数说明:
    	Shell的位置参数($1、$2…、$#、$*、$?以及$@)都可以作为函数的参数使用。
    	此时父脚本的参数临时地被函数参数所掩盖或隐藏。
    	$0比较特殊,它仍然是父脚本的名称。
    	当函数执行完成时,原来的命令行脚本的参数即恢复。
    	函数的参数变量是在函数体里面定义的。
    
    1. 例子
    [root@web01 /server/scripts]# cat func1.sh 
    #!/bin/bash
    
    function linux(){
      echo "linux $1"
    }
    
    for i in {1..5}
    do 
     linux $1
    done
    [root@web01 /server/scripts]# sh func1.sh redhat
    linux redhat
    linux redhat
    linux redhat
    linux redhat
    linux redhat
    [root@web01 /server/scripts]# sh func1.sh ubuntu
    linux ubuntu
    linux ubuntu
    linux ubuntu
    linux ubuntu
    linux ubuntu
    [root@web01 /server/scripts]# sh func1.sh kali
    linux kali
    linux kali
    linux kali
    linux kali
    linux kali
    
    1. 利用脚本检查检测网站URL是否异常
    [root@web01 /server/scripts]# cat check_url.sh 
    #!/bin/bash
    #auth chenhj 1010235677@qq.com
    #check url script file
    wget -q $1 &>/dev/null
    RC=$?                     #注意$?变量不能定义在wget的前面,否则检测会不正确。
    if [ $RC -ne 0 ]
      then
        echo "error:url is not ok"
    else
        echo "normal:url is  ok"
    fi
    =====================================================
    [root@web01 /server/scripts]# sh check_url.sh 10.0.0.7
    normal:url is  ok
    [root@web01 /server/scripts]# nginx -s stop
    [root@web01 /server/scripts]# sh check_url.sh 10.0.0.7
    error:url is not ok
    ==========================================================
    [root@web01 /server/scripts]# sh check_url.sh www.baidu.com
    normal:url is  ok
    [root@web01 /server/scripts]# sh check_url.sh www.jd.com
    normal:url is  ok
    
    1. 函数传参的方法
    [root@web01 /server/scripts]# cat check_url1.sh 
    #!/bin/bash
    #auth chenhj 1010235677@qq.com
    #check url script file
    check_url(){
    	wget -q $1 &>/dev/null
    	RC=$?
    	if [ $RC -ne 0 ]
    	  then
    	    echo "error:url is not ok"
    	else
    	    echo "normal:url is  ok"
    	fi
    }
    
    main(){
    
        check_url $1 
    
    }
    
    main $*   #s* 是函数的传参
    =============================================================
    [root@web01 /server/scripts]# sh check_url1.sh 10.0.0.7
    error:url is not ok
    [root@web01 /server/scripts]# sh check_url1.sh www.jd.com
    normal:url is  ok
    [root@web01 /server/scripts]# sh check_url1.sh www.baidu.com
    normal:url is  ok
    
    1. 判断限制用户输入的参数
    [root@web01 /server/scripts]# vim check_url2.sh
    #!/bin/bash
    #auth chenhj 1010235677@qq.com
    #check url script file
    usage(){
            if [[ ! $1 =~ http://www.* ]]
              then
              echo "Usage:$0 http://www.xxx.{com|org|vip|cn}"
            fi
            exit 0
    }
    check_url(){
            wget -q $1 &>/dev/null
            RC=$?
            if [ $RC -ne 0 ]
              then
                echo "error:url is not ok"
            else
                echo "normal:url is  ok"
            fi
    }
    
    main(){
        usage $1
    "check_url2.sh" 28L, 382C written                                                                                  
    [root@web01 /server/scripts]# sh check_url2.sh 10.0.0.7
    Usage:check_url2.sh http://www.xxx.{com|org|vip|cn}
    [root@web01 /server/scripts]# cat check_url2.sh 
    #!/bin/bash
    #auth chenhj 1010235677@qq.com
    #check url script file
    usage(){
    	if [[ ! $1 =~ http://www.* ]]    #注意只有[[]] 才能匹配正则。
    	  then
    	  echo "Usage:$0 http://www.xxx.{com|org|vip|cn}"
    	  exit 0
    	fi
            
    }
    check_url(){
    	wget -q $1 &>/dev/null
    	RC=$?
    	if [ $RC -ne 0 ]
    	  then
    	    echo "error:url is not ok"
    	else
    	    echo "normal:url is  ok"
    	fi
    }
    
    main(){
        usage $1
        check_url $1 
    
    }
    
    main $*
    =======================================================
    [root@web01 /server/scripts]# sh check_url2.sh http://www.jd.com
    normal:url is  ok
    [root@web01 /server/scripts]# sh check_url2.sh http://www.baidu.com
    normal:url is  ok
    [root@web01 /server/scripts]# sh check_url2.sh 10.0.0.7
    Usage:check_url2.sh http://www.xxx.{com|org|vip|cn}
    
  • 相关阅读:
    简单对拍
    搜索感想
    L1434滑雪
    记忆化搜索
    L3956棋盘
    USACO 数字三角形
    枚举顺序
    蓝桥计算
    用户态和内核态IO过程
    Mybatis的结果集中的Do要不要有setter
  • 原文地址:https://www.cnblogs.com/woaiyunwei/p/12953008.html
Copyright © 2020-2023  润新知