shell函数介绍语法说明及基本例子

shell函数_介绍语法:

简单的语法:

函数名(){

  指令...

  return n

}

规范的语法:

function 函数名(){

  指令...

  return n

}

return命令的功能与工作方式与exit相同,用于跳出函数

shell函数_说明:

shell函数的执行

调用函数

1)直接执行函数名即可

2)带参数的函数执行方法:

函数名 参数1 参数2

3)函数必须先定义然后再使用

shell函数_基本例子:

函数基本例子一:

[root@www 2017_08_31.sh]# cat hanshu.sh
#!/bin/bash
xiaochao(){
        echo "I am xiaochao"
}
function xiaoyongzhuo(){
        echo "I am xiaoyongzhuo"
}

echo "“恭喜您shell函数入门!”"
xiaochao
xiaoyongzhuo

函数基本例子二:把函数体和执行的脚本分离

chmod +x *

vi dy_hanshu.sh(重新写个脚本调用例子一中的函数,没有执行权限退出)

#!/bin/bash
[ -x /root/2017_08_31.sh/hanshu.sh ] && . /root/2017_08_31.sh/hanshu.sh||exit 1

[root@www 2017_08_31.sh]# sh dy_hanshu.sh
I am xiaochao
I am xiaoyongzhuo

“恭喜您shell函数入门!”

例子二:函数传参

cat func.sh
#!/bin/bash
while true
do
function check_usl(){
curl -I -s $1|head -1 && return 0||return 1
}
echo "`date "+%Y-%m-%d %H:%M:%S"`" >> test.txt
check_usl www.baidu.com >> test.txt
sleep 10
done

改成命令行传参数

(注释掉上面的那行,然后替换这行,然后执行。)

check_usl $1 >> test.txt

例子三:使用脚本+函数打印不同颜色的文字

cat plus_color.sh
#!/bin/bash
function new_chars(){
RED_COLOR='E[1;31m'
GREEN_COLOR='E[1;32m'
YELLOW_COLOR='E[1;33m'
BLUE_COLOR='E[1;34m'
PINK_COLOR='E[1;35m'
RES='E[0m'
if [ $# -ne 2 ];then
echo "usage $0 content {red|yellow|blue|green}"
exit 1
fi
case "$2" in
red|RED)
echo -e "${RED_COLOR}$1${RES}"
;;
yellow|YELLOW)
echo -e "${YELLOW_COLOR}$1${RES}"
;;
green|GREEN)
echo -e "${GREEN_COLOR}$1 ${RES}"
;;
blue|BLUE)
echo -e "${BLUE_COLOR}$1${RES}"
;;
pink|PINK)
echo -e "${PINK_COLOR}$1${RES}"
;;
esac
}
new_chars xixi red
new_chars haha green
new_chars "welcome to xyz linux" yellow

命令传参模式 

new_chars “$1” “$2”

案例四:

使用函数来编写nginx启动脚本

#!/bin/bash
# chkconfig: - 99 20
# description:nginx
. /etc/init.d/functions

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case $1 in
start)
$PROG >& /dev/null
[ $? -eq 0 ] && action "nginx is started" /bin/true||
action "nginx is started" /bin/false
;;
stop)
kill -s QUIT $(cat $PIDF) >& /dev/null
[ $? -eq 0 ] && action "nginx is stop" /bin/true||
action "nginx is stop" /bin/false
;;
restart)
$0 stop >& /dev/null
$0 start >& /dev/null
[ $? -eq 0 ] && action "nginx is restart" /bin/true||
action "nginx is restart" /bin/false
;;
reload)
kill -s HUP $(cat $PIDF)
[ $? -eq 0 ] && action "nginx is reload" /bin/true||
action "nginx is reload" /bin/false
;;
*)
echo "useage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0

#!/bin/bash
# chkconfig: - 99 20
# description:nginx
. /etc/init.d/functions

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

start(){
$PROG >& /dev/null
[ $? -eq 0 ] && action "nginx is started" /bin/true||
action "nginx is started" /bin/false
}
stop(){
kill -s QUIT $(cat $PIDF) >& /dev/null
[ $? -eq 0 ] && action "nginx is stop" /bin/true||
action "nginx is stop" /bin/false
}
restart(){
$0 stop >& /dev/null
$0 start >& /dev/null
[ $? -eq 0 ] && action "nginx is restart" /bin/true||
action "nginx is restart" /bin/false
}
reload(){
kill -s HUP $(cat $PIDF)
[ $? -eq 0 ] && action "nginx is reload" /bin/true||
action "nginx is reload" /bin/false
}
other(){
echo "useage:$0 {start|stop|restart|reload}"
exit 1
}

case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
*)
other
;;
esac
exit 0

案例五:检测url是否为200OK(人性化输出)

#!/bin/bash
. /etc/rc.d/init.d/functions
while true
do
function check_url(){
curl -I -s --connect-timeout 10 $1|head -1|awk -F'[ ]+' '{print $2}' && return 0||return 1
}
#check_url $1 #命令行传参模式
statu=$(check_url www.baidu.com)
if [ $statu = 200 ];then
action "url this is yes!" /bin/true
else
action "url this is no!" /bin/false
fi
sleep 1
done

案例六: 批量检查web服务是否正常并发送相关邮件                          

原文地址:https://www.cnblogs.com/xiaoyongzhuo/p/7459188.html