函数

什么是函数

函数其实就是一堆命令的集合,用来完成一些特定的代码块

作用:

便于代码的复用

跟脚本类似


函数的基本概述

#定义函数

#第一种

函数名() {

    命令集合
}

#第二种

function 函数名 {
    命令集合
}

#示例

#定义函数,函数的大括号的与命令之间要空格作为分隔符
[root@shell /scripts/shell11]# fun1() { echo "123"; }    

#调用函数
[root@shell /scripts/shell11]# fun1
123

[root@shell /scripts/shell11]# function fun2 { echo "456"; }
[root@shell /scripts/shell11]# fun2
456

#函数的内部位置变量

[root@shell /scripts/shell11]# fun2 () { echo "$1"; }
[root@shell /scripts/shell11]# fun2

[root@shell /scripts/shell11]# fun2 123
123
[root@shell /scripts/shell11]# fun2 123 456
123

[root@shell /scripts/shell11]# fun3() { echo "$1 $2"; } 
[root@shell /scripts/shell11]# fun3 123 456
123 456
[root@shell /scripts/shell11]# fun3 123 
123 

[root@shell /scripts/shell11]# fun4() { echo "$*"; }
[root@shell /scripts/shell11]# 
[root@shell /scripts/shell11]# fun4 123
123
[root@shell /scripts/shell11]# fun4 123 456
123 456
[root@shell /scripts/shell11]# fun4 123 456 789
123 456 789

[root@shell /scripts/shell11]# fun5() { echo "$@"; }
[root@shell /scripts/shell11]# fun5 123
123
[root@shell /scripts/shell11]# fun5 123 456
123 456


#如何向函数传递参数,都是使用$1..$9

[root@shell /scripts/shell11]# cat fun-1.sh
#!/bin/bash
fun_1() {
    echo "$1"
}
fun_1 123
[root@shell /scripts/shell11]# sh fun-1.sh
123
[root@shell /scripts/shell11]# vim fun-1.sh
[root@shell /scripts/shell11]# cat fun-1.sh
#!/bin/bash
fun_1() {
    echo "$Num"
}
Num=123
fun_1
[root@shell /scripts/shell11]# sh fun-1.sh
123
[root@shell /scripts/shell11]# vim fun-1.sh
[root@shell /scripts/shell11]# 
[root@shell /scripts/shell11]# cat fun-1.sh
#!/bin/bash
fun_1() {
    echo "$1"
}
fun_1 $1
[root@shell /scripts/shell11]# sh fun-1.sh

[root@shell /scripts/shell11]# sh fun-1.sh  123
123

[root@shell /scripts/shell11]# cat fun-1.sh
#!/bin/bash
fun_1() {
    echo "$1"
}
Num=$1
fun_1
[root@shell /scripts/shell11]# sh fun-1.sh 123 

[root@shell /scripts/shell11]# vim fun-1.sh 
[root@shell /scripts/shell11]# cat fun-1.sh
#!/bin/bash
fun_1() {
    echo "$Num"
}
Num=$1
fun_1
[root@shell /scripts/shell11]# sh fun-1.sh

[root@shell /scripts/shell11]# sh fun-1.sh 123
123

#注意:脚本中的位置变量不是函数中的位置变量

#编写多个位置变量

[root@shell /scripts/shell11]# cat fun-2.sh
#!/bin/bash
fun_2() {
    echo "$1"        #接收函数传递第一个位置变量
}
fun_2 $1        #接收脚本传递的第一个位置变量,传入函数中第一个参数
fun_2 $2        #接收脚本传递的第二个位置变量,传入函数中第一个参数
fun_2 $3        #接收脚本传递的第三个位置变量,传入函数中第一个参数
[root@shell /scripts/shell11]# sh fun-2.sh  123 456 789
123
456
789

[root@shell /scripts/shell11]# cat fun-2.sh
#!/bin/bash
fun_2() {
    echo "$1"
}
fun_2 $1 $2 $3
fun_2 $3
fun_2 $2
[root@shell /scripts/shell11]# sh fun-2.sh 123 456 789
123
789
456

#函数中定义多个位置变量

[root@shell /scripts/shell11]# cat fun-3.sh
#!/bin/bash
fun_3() {
    echo "$1 $2 $3"
}
fun_3 123 456 789
[root@shell /scripts/shell11]# sh fun-3.sh 
123 456 789


[root@shell /scripts/shell11]# cat fun-3.sh
#!/bin/bash
fun_3() {
    echo "$1 $2 $3"
}
Num=$(fun_3 123 456 789)
echo $Num
[root@shell /scripts/shell11]# sh fun-3.sh
123 456 789

[root@shell /scripts/shell11]# cat fun-3.sh
#!/bin/bash
fun_3() {
    echo "$1 $2 $3"
}
Num=$(fun_3 $1 $2 $3)
echo $Num
[root@shell /scripts/shell11]# sh fun-3.sh

[root@shell /scripts/shell11]# sh fun-3.sh 123 456 789
123 456 789

1. 函数场景示例

写一个脚本,实现简单的计算器功能,能够实现加减乘除四种计算

执行脚本

sh  script.sh  1 + 1

1 + 1 = 2

sh  script.sh  1 - 1

1 - 1 = 0

#第一种
[root@shell /scripts/shell11]# cat cale.sh 
#!/bin/bash
cale() {
    echo $(( $1 $2 $3 ))
}
if [ $# -ne 3 ];then
    echo "你的传参不足3个!你可以这么写:1 + 1"
    exit
fi

echo "$1 $2 $3 = $(cale $1 "$2" $3)"

#第二种

[root@shell /scripts/shell11]# cat cale.sh
#!/bin/bash
cale() {
    case $2 in
        +|-|/)
            echo $1 $2 $3 = $(( $1 $2 $3 ))
            ;;
        x)
            echo $1 $2 $3 = $(( $1 * $3  ))
            ;;
        *)
            echo "你输入的不是一个计算操作符!"
            exit
    esac
}
if [ $# -ne 3 ];then
    echo "你的传参不足3个!你可以这么写:1 + 1"
    exit
fi
cale $1 $2 $3
[root@shell /scripts/shell11]# sh cale.sh
你的传参不足3个!你可以这么写:1 + 1
[root@shell /scripts/shell11]# sh cale.sh 1 + 1
1 + 1 = 2
[root@shell /scripts/shell11]# sh cale.sh 1 - 1
1 - 1 = 0
[root@shell /scripts/shell11]# sh cale.sh 1 / 1
1 / 1 = 1
[root@shell /scripts/shell11]# sh cale.sh 1 * 1
你的传参不足3个!你可以这么写:1 + 1
[root@shell /scripts/shell11]# sh cale.sh 1 \* 1
你输入的不是一个计算操作符!
[root@shell /scripts/shell11]# sh cale.sh 1 x 1
1 x 1 = 1
[root@shell /scripts/shell11]# sh cale.sh 1 x 4
1 x 4 = 4

2. Nginx启动停止脚本,使用函数

[root@shell /scripts/shell11]# cat nginx.sh
#!/bin/bash
#1.调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.判断当前用户是否为管理员
if [ $USER != "root" -a  $UID -ne 0 ];then
    echo "当前用户${USER}对此脚本${0}没有执行权限!"
    exit
fi
#3.判断位置变量个数是否为一
State=$1
if [ $# -ne 1 ];then
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit
fi
#加锁
Suo_file=/tmp/nginx.lock
if [ -f $Suo_file ];then
    echo "此脚本正在执行中!请稍后再运行!"
    exit
fi
#4.编写函数
Tong() {
    if  [ $? -eq 0 ];then
        action "Nginx Server $State Successfully." /bin/true
    else
        action "Nginx Server $State Failed!" /bin/false
    fi
}
Start() {
    if [ -f /var/run/nginx.pid ];then
        action "Nginx Server is running........." /bin/true
    else
        /usr/sbin/nginx &>/dev/null && sleep 3
        Tong
    fi
}
Stop() {
    if [ -f /var/run/nginx.pid ];then
        /usr/sbin/nginx -s stop &>/dev/null && sleep 3
        Tong
    else
        action "Nginx Server is stop..........." /bin/true
    fi
}
Status() {
    if [ -f /var/run/nginx.pid ];then
        action "Nginx Server is running........." /bin/true
    else
         action "Nginx Server is stop..........." /bin/true
    fi
}
Reload() {
    if [ -f /var/run/nginx.pid ];then
        /usr/sbin/nginx -s reload &>/dev/null && sleep 3
        Tong
    else
        action "Nginx Server not running,Unable to proceed reload!" /bin/false
    fi
}
Restart() {
    State=stop
    Stop  
    State=start
    Start
}
case $State in
    start)
            Start
            ;;
    stop)
            Stop
            ;;
    status)
            Status
            ;;
    reload)
            Reload
            ;;
    restart)
            Restart 
            ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
esac
#解锁
rm -f $Suo_file

1.1 写一个操作系统初始优化脚本

见附件

2. 函数状态返回

返回值在shell中有两种方式

echo        #返回数据,自定义的输出数据  字符串

return        #命令的执行结果返回值  0-255 之间的正整数  $?    0  默认表示成功  非0表示失败

[root@shell /scripts/shell12]# cat return-1.sh
#!/bin/bash
fun() {
    echo "123"
    return 1
}
Test=$(fun)
echo "函数的状态返回码为:$?"
echo "函数的返回数据为:$Test"
[root@shell /scripts/shell12]# sh return-1.sh
函数的状态返回码为:1
函数的返回数据为:123

[root@shell /scripts/shell12]# cat return-2.sh
#!/bin/bash
File() {
    if [ -f /etc/hosts ];then
        return 0
    else
        return 1
    fi
}
File && echo "文件存在!" || echo "文件不存在!"
[root@shell /scripts/shell12]# sh return-2.sh
文件存在!
[root@shell /scripts/shell12]# vim return-2.sh
[root@shell /scripts/shell12]# cat return-2.sh
#!/bin/bash
File() {
    if [ -f /etc/hosts ];then
        return 1
    else
        return 0
    fi
}
File && echo "文件存在!" || echo "文件不存在!"
[root@shell /scripts/shell12]# sh return-2.sh
文件不存在!

3. 状态返回案例

猜数字的游戏

1. 用户输入的数值大于等于0且小于10  返回状态码为0

2. 用户输入的数值大于等于10且小于20 返回状态码为1

3. 用户输入的数值大于等于20且小于30 返回状态码为2 

4. 输入的其他数值 返回状态码为3

[root@shell /scripts/shell12]# cat return-3.sh
#!/bin/bash
check_num() {
    read -p "请输入一个数字:" Num
    if [ $Num -ge 0 -a $Num -lt 10 ];then
        return 0
    elif [ $Num -ge 10 -a $Num -lt 20 ];then
        return 1
    elif [ $Num -ge 20 -a $Num -lt 30 ];then
        return 2
    else
        return 3
    fi
}
check_num
Rc=$?
if [ $Rc -eq 0 ];then
    echo "你输入的数字大于等于0且小于10."
elif [ $Rc -eq 1 ];then
    echo "你输入的数字大于等于10且小于20."
elif [ $Rc -eq 2 ];then
    echo "你输入的数字大于等于20且小于30."
else
    echo "你输入的数值超出范围!请重新输入!"
    echo $Rc
fi
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:0
你输入的数字大于等于0且小于10.
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:10
你输入的数字大于等于10且小于20.
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:20
你输入的数字大于等于20且小于30.
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:30
你输入的数值超出范围!请重新输入!
3
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:40
你输入的数值超出范围!请重新输入!
3
[root@shell /scripts/shell12]# sh return-3.sh
请输入一个数字:a
return-3.sh: line 4: [: a: integer expression expected
return-3.sh: line 6: [: a: integer expression expected
return-3.sh: line 8: [: a: integer expression expected
你输入的数值超出范围!请重新输入!
3
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:44

results matching ""

    No results matching ""