Shell函数
function 函数名() { #<==作者推荐的书写函数的方法(带括号)
指令集...
return n
}
简化写法1:
function 函数名 {
#<==不推荐读者使用此方法(无括号,函数名和左大括号之间要有空格。)
指令集...
return n
}
在Shell函数的语法中,当有function时,函数名后面的小括号“()”部分可以省略不写。
简化写法2:
函数名() { #<==不用function的方法
指令集...
return n
}
在Shell函数的语法中,function表示声明一个函数,这部分可以省略不写。
oldboy(){
echo "I am oldboy."
echo "I am oldgirl."
}
for n in {1..5}
do
oldboy
done
[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 $*
函数的参数: 脚本的参数:
函数后接的参数说明:
Shell的位置参数($1、$2…、$#、$*、$?以及$@)都可以作为函数的参数使用。
此时父脚本的参数临时地被函数参数所掩盖或隐藏。
$0比较特殊,它仍然是父脚本的名称。
当函数执行完成时,原来的命令行脚本的参数即恢复。
函数的参数变量是在函数体里面定义的。
[root@web01 /server/scripts]# cat func3.sh
oldboy(){
echo "I am $1"
}
oldboy xiangbo
[root@web01 /server/scripts]# cat func4.sh
oldboy(){
echo "I am $1."
}
oldboy $1
练习1:检测网站URL是否异常(可以用本地URL或者www.baidu.com)
练习2:用函数实现上述功能。
练习3:将函数的传参转成脚本文件命令行传参,对命令行接任意指定URL判断是否异常。
[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]# cat check_url4.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && ./etc/init.d/functions
function usage(){
if [[ ! $1 =~ https://* ]]
then
echo "Usage:$0 https://www.XX.com"
exit 1
fi
}
function check_url(){
wget -q $1 $>/dev/null
reteval=$?
if [ $reteval -eq 0 ]
then
action "url is ok" /bin/true
else
action "url is no" /bin/false
fi
}
function main(){
usage $1
check_url
}
main $*
[root@web01 /server/scripts]# sh check_url4.sh http://www.baidu.com
url is ok.