Shell编程--函数function

1.定义函数

函数声明:
function_name () {
    list of commands
}
函数名 function_name,这就是你将使用它从其他地方在你的脚本调用。
function (功能) 功能函数

  • 计算机函数:就是固定的模块、固定的功能做固定的操作,相当于linux中的固定的功能,当需要使用的时候,就去调用函数
  • ​作用:完成特定功能的代码片段(函数必须先定义才能使用)
  • 优点:避免重复的代码
  • 命名空间:在shell语言中命名空间函数内和函数外(不包括脚本)是一样的,函数内外不能赋值同样名字的变量
  • 变量:如果在同一个命名空间(同一个脚本内)可以用,如果不再同一个命名空间就不能用
  • 分开函数和shell的命名空间:如果不让在其他空间用使用,local 分开
  • 函数变量使用的范围:
        默认,函数里的变量会在函数外面生效(全局变量)
        local 变量名称(变量只在函数内生效,属于局部变量)
[root@localhost ~]# vim test.sh
a=10  # 全局变量
func1() {
  local b=20   # 局部变量
  echo $b
  c=30        # 全局变量
}
func1       #调用函数
echo $a   # 10
echo $b   # 错误,b是func1的局部变量
echo $c   # 30//a 是全局变量,能在整个脚本内用
//b 是func1的局部变量,只在func1内可用
//c 是全局变量,定义在func1内,但属于整个脚本的变量
[root@localhost ~]# bash test.sh
20
1030

总结
在同一命名空间(同一个shell脚本)内定义的变量,能全局使用
局部变量只在函数定义它的地方有效,出了这个函数,就无法使用。
​返回值:return value
value不能超过0-255,是函数里面函数最后一条执行命令的返回值,默认返回值是由这条命令执行结果确定的

[root@localhost ~]# vim re.sh
#!/usr/bin/bash
func(){
        echo "hello"
        # return 250 返回250并退出了函数,下面的语句不执行
        if [ $? -eq 0 ];then
                return 250   #返回的是执行函数的返回值
        else
                return 251
        fi
}
func1(){
        echo "hello"
}
func    #调用函数func
echo $?     #打印状态码,看看是否为250
func1    #调用函数func1
echo $?     #打印状态码,看看是否为0//执行
[root@localhost ~]# bash re.sh
hello
250
hello
0

总结
exit:返回结果并退出程序
return:返回结果并退出函数
函数的返回值,返回的是函数体内【最后一条命令】是否成功的返回值

[root@localhost ~]# systemctl stop httpd 
​[root@localhost ~]# systemctl stop vsftpd
​[root@localhost ~]# vim fun02.sh 
#!/bin/bash
fun() {
        systemctl status httpd &>/dev/null
        systemctl status vsftpd &>/dev/null
}
fun 
echo $?
​
​[root@localhost ~]# bash fun02.sh 
3

2.调用函数

2.1.取消函数

取消函数 unset myfunc

[root@localhost ~]# vim fun01.sh
#!/bin/bash
fun () {
echo "hello"
}
fun    #调用函数
unset fun   #取消函数
fun    #再次调用函数
​
[root@localhost ~]# bash fun01.sh 
hello
fun01.sh: line 7: fun: command not found

2.2.其他脚本调用

[root@localhost ~]# cat a.sh 
#!/usr/bin/bash
check_net() {
        echo "正在检查网络通信"
        ping -c1 www.baidu.com 2&> /dev/null
                if [ $? -eq 0 ];then
                        echo "你的网络是没有问题的"
                else
                        echo "你的网络有问题,请先检查网络"
                        exit 2
                fi
}
check_net
echo "+++++++++++++++++++++++++++++++++++++++++++++"

check_yum() {
        echo "正在检查yum源是否可用"
        yum repolist
                if [ $? -eq 0 ];then
                        echo "你的yum源可以正常使用"
                else
                        echo "yum源不能用,请手动配置网络yum源"
                        exit 3
                fi
}
#check_yum
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++"

install_nginx() {
#检查网络是否可以上网
check_net
echo "正在配置nginx的yum源"
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF
yum clean all && sleep 2
yum install -y nginx 2&> /dev/null
if [ $? -eq 0 ];then
        echo "nginx install successfull and start nginx"
        sleep 2
        systemctl start nginx
        if [ $? -eq 0 ];then
                echo "nginx started is success,plaess check port!"
                port=`netstat -lntp | grep nginx | awk '{print $4}' | awk -F ":" '{print $NF}'`
                echo "nginx is port $port"
        else
                echo "nginx 启动失败,请手动检查!"
                exit 4
        fi
else
        echo "nginx install failed!将会自动退出"
        exit 5
fi
}
#install_nginx

//函数调用
[root@localhost ~]# cat b.sh #通过其他脚本调用函数脚本 
#!/usr/bin/bash
while :
do
echo "这是服务器基本检测功能脚本"
echo "
++++++++++++++++++++++++++++++++++
+       1. 检查yum源             +
+       2. 检查网络              +
+       3. 安装nginx             +
+       4. 退出                  +
++++++++++++++++++++++++++++++++++
"
source ./a.sh  #写你函数脚本的绝对路径,这里指的是执行函数脚本,让它为下面调用函数生效
read -p "请输入你的选项: " num
case $num in
        1)
        check_yum
        ;;
        2)
        check_net
        ;;
        3)
        install_nginx
        ;;
        4)
        exit;;
        *)
        echo "你输入的选项失败请重新输入"
esac
done

3.函数传参

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数

[root@localhost ~]# vim fun03.sh
#!/bin/bash
if [ ! $# -eq 3 ];then
        echo "Must Input Three number: " p1 p2 p3
exit 
fi
fun() {
        echo $[$1*$2*$3] 
}
fun 1 2 3  #进行传参

[root@localhost ~]# bash fun03.sh 1 3 4 6  //这个时候只是传参到了脚本,并没有传到函数里面 
Must Input Three number:  p1 p2 p3

//修改版:
[root@localhost ~]# vim fun04.sh
fun() {
        echo $[$1*$2*$3]
}
fun $1 $2 $3

[root@localhost ~]# bash fun04.sh 1 3 5
15

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA548464

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值