shell实战_函数应用实例

应用实例
检测网站存活

1.给脚本传入参数,检测url是否正常,并且要去函数开发

[root@localhost myfunction]# cat myfunctiontest3.sh 
#!/bin/bash

#检测网站是否存活


function usage(){

	echo "Usage: ${0}             url"
	exit 1
}
#功能检测url函数
function 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
}
# 参考c 语言开放形式,设立一个main函数,入口函数
main(){

     #判断用户输入
   if [ "$#" -ne 1 ] ;then
	usage
   fi
    check_url $1

}
main $*
[root@localhost myfunction]# 

###########################脚本执行结果
[root@localhost myfunction]# bash myfunctiontest3.sh  afa faf
Usage: myfunctiontest3.sh             url
[root@localhost myfunction]# bash myfunctiontest3.sh  www.baidu.com
www.baidu.com is running ...
[root@localhost myfunction]# bash myfunctiontest3.sh  www.baidu.com
www.baidu.com is running ...
[root@localhost myfunction]# bash myfunctiontest3.sh  www.baidu.coms
www.baidu.coms is down.........
[root@localhost myfunction]# 

开发rsync管理脚本函数版本

/etc/init.d/mysql|network 可以参考

########################1.  函数脚本
[root@localhost myfunction]# cat myfunctiontest2.sh 
#!/bin/bashi

#rsync启停脚本 改成函数
#Auther:Mrxu

function errmsg(){
	echo "启停rsync脚本,你只能录入{start|stop|restart}"

	exit 1

}

function startRsync(){
	/usr/bin/rsync --daemon
	sleep 2
	if [ `netstat -tunlp|grep 873|wc -l ` -ge "1" ]; then
	echo "rsync服务启动成功"
	return 0
	else 
	echo "rsync服务启动失败"
	fi
	return 1
}

function stopRsync(){
	if [ `netstat -tunlp |grep 873|wc -l` -ne 0 ] ; then
         killall rsync
	 sleep 2
		if [ `netstat -tunlp |grep 873 |wc -l` -eq "0" ]; then
		echo "rsync服务已停止"
		exit 0
		else
		echo "rsync 服务停止失败"
		exit 1
		fi
	else   
 		echo  "rsync未启动"
	fi
}

function restartRsync(){
	if [ `netstat -tunlp|grep 873|wc -l` -ne 0 ]; then
	killall rsync
	fi
	stopvar=`netstat -tunlp|grep 873|wc -l`
	sleep 1
	/usr/bin/rsync --daemon
	sleep 1
	startvar=`netstat -tunlp|grep 873|wc -l`
	echo stopvar:$stopvar--------------startvar :$startvar
	if [ $stopvar -eq "0" -a ${startvar} -ge "1"  ]; then
	echo "rsync服务已经重启成功"
	exit 0
	else
	echo "rsync服务重启失败"
	exit 1
	fi
}

[root@localhost myfunction]# 

#######################2.加载该函数的脚本
[root@localhost myfunction]# cat testfunction2.sh 
#!/bin/bash
###检查函数文件是否存在
[ -f /root/tmp/myfunction/myfunctiontest2.sh ] && source  /root/tmp/myfunction/myfunctiontest2.sh ||exit

if [ "$#" -ne 1 ]; then
	errmsg
	exit 1
fi

if [ "${1}" = "start" ];then
	
	startRsync
	exit 0
elif [ "${1}" = "stop" ]; then
	stopRsync
	exit 0
elif [ "${1}" = "restart" ] ; then
	restartRsync
	exit 0
else
	errmsg
fi

[root@localhost myfunction]# 


######################3.执行结果

[root@localhost myfunction]# bash testfunction2.sh  af fa fa 
启停rsync脚本,你只能录入{start|stop|restart}
[root@localhost myfunction]# bash testfunction2.sh  start
rsync服务启动成功
[root@localhost myfunction]# bash testfunction2.sh  stop
rsync服务已停止
[root@localhost myfunction]# bash testfunction2.sh  restart
stopvar:0--------------startvar :2
rsync服务已经重启成功
[root@localhost myfunction]# netstat -tunlp |grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      9599/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      9599/rsync          
[root@localhost myfunction]# 
posted @   翻滚的小井蛙  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示