封装自定义函数使用,为了以后调用函数比较方便
这是一个非函数脚本:
#!/bin/bash
#针对脚本,编写一个提示信息
if [ $# -ne 1 ];then
echo "Usage: $0 url"
exit 1
fi
wget --spider -q -o /dev/null --tries=1 -T 5 $1
if [ $? -eq 0 ];then
echo "$1 is running..."
else
echo "$1 is down..."
fi
升级脚本即使用函数脚本:
#!/bin/bash
#针对脚本,编写一个提示信息
#if [ $# -ne 1 ];then
# echo "Usage: $0 url"
# exit 1
#fi
function usage(){
echo "Usage: $0 url"
exit 1
}
check_url(){
wget --spider -q -o /dev/null --tries=1 -T 5 $1
if [ $? -eq 0 ];then
echo "$1 is running..."
else
echo "$1 is down..."
fi
}
main(){
if [ $# -ne 1 ];then
usage
fi
check_url $1
}
main $*