第1章 Shell编程函数应用
函数
函数也有类似于别名的作用,简单的说,函数的作用就是将程序里面多次被调用的代码组合起来,称为函数体,并取一个名字称为(函数名),当我们需要用到这段代码的时候,我们就可以直接来调用函数名。
Shell函数的语法
在shell中 if语句有它的语法,for循环也有它的语法,那么shell中的函数,那肯定也有它的语法有以下三种:
function 函数名 () {
指令...
return -n
}
function 函数名 {
指令...
return -n
}
函数名 () {
指令...
return -n
}
提示:在以上的函数语法中,前面的funcation 表示声明一个函数!!! 可以不写 return -n 是指退出函数
测试文件
[root@123 /server/scripts]# cat function.sh
#!/bin/bash
##############################################################
# File Name: function.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 09:09:20
# Description:
##############################################################
function fj () {
echo "我是风姐!"
}
function zhangsan () {
echo "我是张三!"
}
fj
zhangsan
[root@123 /server/scripts]# cat fun.sh
#!/bin/bash
##############################################################
# File Name: fun.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 09:15:14
# Description:
##############################################################
if [ -f /etc/init.d/functions ]
then
. /etc/init.d/functions
fi
zhangsan
[root@123 /server/scripts]# tail -n 3 /etc/init.d/functions
function zhangsan () {
echo "我就是张三"
}
编写带参数的shell函数实例
[root@123 /server/scripts]# cat lisi.sh
#!/bin/bash
##############################################################
# File Name: lisi.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 09:22:34
# Description:
##############################################################
function lisi () {
echo 我的名字叫:$1
}
lisi $1
[root@123 /server/scripts]# sh lisi.sh
我的名字叫:
[root@123 /server/scripts]# sh lisi.sh 李四
我的名字叫:李四
测试开发企业级URL检测脚本
[root@123 /server/scripts]# cat wangzhi.sh
#!/bin/bash
##############################################################
# File Name: wangmazi.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 10:40:23
# Description:
##############################################################
#no.1 if [ "$#" -ne 1 ] then echo "请您输入一个正确的网址" exit 1 fi #no.2wget --spider -q -o /dev/null --tries=1 -T 3 $1
if [ "$?" -eq 0 ]
then
echo "$1 检测是成功的!"
else
echo "$1 检测是失败的!"
exit 1
fi
url脚本
[root@123 /server/scripts]# cat url.sh
#!/bin/bash
##############################################################
# File Name: url.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 10:52:34
# Description:
##############################################################
function TS () {
echo "请您输入一个网址"
exit 1
}
function Check_url () {
wget --spider -q -o /dev/null --tries=1 -T 3 $1
if [ "$?" -eq 0 ]
then
echo "$1 检测成功!"
else
echo "$1 检测失败"
exit 1
fi
}
function JG () {
if [ "$#" -ne 1 ]
then
TS
else
Check_url $1
fi
}
JG $*
练习
[root@123 /server/scripts]# cat url.sh
#!/bin/bash
##############################################################
# File Name: url.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 10:52:34
# Description:
##############################################################
. /etc/rc.d/init.d/functions
function TS () {
echo "请您输入一个网址"
exit 1
}
function Check_url () {
wget --spider -q -o /dev/null --tries=1 -T 3 $1
if [ "$?" -eq 0 ]
then
action "$1 检测成功!" /bin/true
else
action "$1 检测失败" /bin/false
exit 1
fi
}
function JG () {
if [ "$#" -ne 1 ]
then
TS
else
Check_url $1
fi
}
JG $*
[root@123 /server/scripts]# sh url.sh
请您输入一个网址
[root@123 /server/scripts]# sh url.sh www.baidu.com
www.baidu.com 检测成功! [ OK ]
练习
[root@123 /server/scripts]# cat url.sh
#!/bin/bash
##############################################################
# File Name: url.sh
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-08-07 10:52:34
# Description:
##############################################################
. /etc/rc.d/init.d/functions
function TS () {
echo "请您输入一个网址"
exit 1
}
function check_http () {
if [[ $1 =~ http://www.* ]]
then
:
else
echo "地址必须以http://www开始"
exit 2
fi
}
function Check_url () {
wget --spider -q -o /dev/null --tries=1 -T 3 $1
if [ "$?" -eq 0 ]
then
action "$1 检测成功!" /bin/true
else
action "$1 检测失败" /bin/false
exit 3
fi
}
function JG () {
if [ "$#" -ne 1 ]
then
TS
else
check_http $1
Check_url $1
fi
}
JG $*
[root@123 /server/scripts]# sh url.sh www.baidu.com
地址必须以http://www开始
[root@123 /server/scripts]# sh url.sh http://www.baidu.com
http://www.baidu.com 检测成功! [ OK ]