八、函式知識與實踐
(一)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.uj5u.com/caozuo/91656.html
標籤:Linux
