image.png
image.png
image.png
image.png
image.png
[root@web01 /etc/init.d]# cat rsyncd
#!/bin/bash
# chkconfig: 2345 21 81
# description: startup rsync scripts
if [ "$1" = "start" ]
then
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
:
else
rsync --daemon
fi
elif [ "$1" = "stop" ]
then
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
kill `cat /var/run/rsyncd.pid`
else
echo "Failed to stop rsync Unit rsync not loaded."
exit 1
fi
elif [ "$1" = "restart" ]
then
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
kill `cat /var/run/rsyncd.pid`
fi
sleep 2
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
:
else
rsync --daemon
fi
else
echo "Usage;$0 {start|stop|restart}"
fi
[root@web01 /etc/init.d]# cat rsyncd1
#!/bin/bash
# chkconfig: 2345 21 81
# description: startup rsync scripts
start(){
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
:
else
rsync --daemon
fi
}
stop(){
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
kill `cat /var/run/rsyncd.pid`
else
echo "Failed to stop rsync Unit rsync not loaded."
exit 1
fi
}
if [ "$1" = "start" ]
then
start
elif [ "$1" = "stop" ]
then
stop
elif [ "$1" = "restart" ]
then
$0 stop
sleep 2
$0 start
else
echo "Usage;$0 {start|stop|restart}"
fi
[root@web01 /etc/init.d]# cat rsyncd2
#!/bin/bash
# chkconfig: 2345 21 81
# description: startup rsync scripts
start(){
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
:
else
rsync --daemon
fi
}
stop(){
if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]
then
kill `cat /var/run/rsyncd.pid`
else
echo "Failed to stop rsync Unit rsync not loaded."
exit 1
fi
}
main(){
if [ "$1" = "start" ]
then
start
elif [ "$1" = "stop" ]
then
stop
elif [ "$1" = "restart" ]
then
$0 stop
sleep 2
$0 start
else
echo "Usage;$0 {start|stop|restart}"
fi
}
main $*
函数传参
[root@backup ~]# sh b.sh ppp
I am ppp
[root@backup ~]# cat b.sh
oldboy(){
echo "I am $1"
}
oldboy $1
[root@backup ~]# sh a.sh
I am ooo
[root@backup ~]# cat a.sh
oldboy(){
echo "I am $1"
}
oldboy ooo
[root@web01 /server/scripts]# cat check_url1.sh
#!/bin/sh
wget -q 127.0.0.1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "url is ok."
else
echo "url is no."
fi
改成传参:
[root@web01 /server/scripts]# cat check_url2.sh
#!/bin/sh
wget -q $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "url is ok."
else
echo "url is no."
fi
专业写法:
[root@web01 /server/scripts]# cat check_url3.sh
#!/bin/sh
check_url(){
wget -q $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "url is ok."
else
echo "url is no."
fi
}
main(){
check_url $1
}
main $*
[root@web01 /server/scripts]# sh check_url3.sh 127.0.0.1
url is ok.
[root@web01 /server/scripts]# cat check_url3.sh
#!/bin/sh
usage(){
if [[ ! $1 =~http://www.*com ]]
then
echo "Usage:$0 http://www.xx.com"
exit 1
fi
}
check_url(){
wget -q $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
echo "url is ok."
else
echo "url is no."
fi
}
main(){
usage $1
check_url $1
}
main $*
[root@web01 /server/scripts]# sh check_url4.shhttp://www.baidu.com
url is ok. [ 确定 ]
[root@web01 /server/scripts]# cat check_url4.sh
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
usage(){
if [[ ! $1 =~ http://www.*com ]]
then
echo "Usage:$0http://www.xx.com"
exit 1
fi
}
check_url(){
wget -q $1 &>/dev/null
retval=$?
if [ $retval -eq 0 ]
then
action "url is ok." /bin/true
else
action "url is no." /bin/false
fi
}
main(){
usage $1
check_url $1
}
main $*
网友评论