• shell脚本之八:函数知识与实践


    八、函数知识与实践

    (一)shell函数语法

    1、函数的表示方式

    第一种语法 第二种语法 第三种语法
    function 函数名(){ } function 函数名 {} 函数名() { }

    2、实例:函数的写法

    [root@centos6-kvm3 scripts]# cat 08-01.sh
    #!/bin/bash
    function oldboy(){
    echo "i am $1 teacher"
    
    }
    function oldgirl {
    echo "i am $1 teacher"
    }
    
    test() {
    echo "this is $1"
    }
    
    oldboy $1
    oldgirl $2
    test $3
    [root@centos6-kvm3 scripts]# bash 08-01.sh oldboy oldgirl test
    i am oldboy teacher
    i am oldgirl teacher
    this is test
    

    3、实例:检测web网站是否正常

    wget 命令:

    --spider 模拟爬虫(不下载)

    -q 安静访问

    -o /dev/null 不输出

    -T --timeout 超时时间

    -t --tries 重试次数

     wget --spider -T 5 -q -o /dev/null -t 2 www.baidu.com
     echo $?
    

    curl命令:

    -I 查看响应头

    -s 安静的

    -o /dev/null 不输出

    -w%{http_code} 返回状态码

    [root@centos6-kvm3 scripts]# curl -I -s -o /dev/null -w "%{http_code}
    " www.baidu.com
    200
    

    检测网站案例展示:

    [root@centos6-kvm3 scripts]# cat 08-02.sh
    #!/bin/bash
    function usage(){
    echo "usage:$0 url"
    exit 1
    }
    function url_check {
    wget -q -o /dev/null -T 5 -t 3 $1
    if [ $? -eq 0 ]
    then
       echo "$1 is ok!"
    else
       echo "$1 is fail!"
    fi
    }
    main(){
    if [ $# -ne 1 ]
    then
       usage
    else
       url_check $1
    fi
    }
    
    main $*
    
  • 相关阅读:
    Maven
    Maven
    Maven
    Maven
    Maven
    Maven
    Maven
    Python
    Maven
    include和require的区别
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/12798608.html
Copyright © 2020-2023  润新知