需求是:远程监控web是否正常 1)使用传参方式 2)判断参数是正确 3)有输出提示 脚本实践

#!/bin/sh #引用系统函数 [ -f /etc/init.d/functions ] && . /etc/init.d/functions #判断输入的参数是否正确 usage(){ echo "USAGE:$0 url" exit 1 } #定义吧变量 RETVAL=0 funCheckUrl(){ wget -T 10 --spider -t 2 $1 >/dev/null 2>&1 #&>/dev/null RETVAL=$? if [ $RETVAL -eq 0 ];then action "$1 url" /bin/true else action "$1 url" /bin/false fi return $RETVAL } main(){ if [ $# -ne 1 ];then usage fi funCheckUrl $1 RETVAL=$? return $RETVAL } main $*

#脚本名称叫test.sh 入参三个: 1 2 3 #运行test.sh 1 2 3后 #$*为"1 2 3"(一起被引号包住) #$@为"1" "2" "3"(分别被包住) #$#为3(参数数量) #2>/dev/null 错误定向空 #>/dev/null 2>&1相等&>/dev/null 正确错误都定向空