当前位置: 移动技术网 > 科技>操作系统>Linux > shell脚本函数知识与实践

shell脚本函数知识与实践

2020年04月29日  | 移动技术网科技  | 我要评论

八、函数知识与实践

(一)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}\n" 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 $*
转自:https://www.cnblogs.com/cuiyongchao007/p/12798608.html

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网