• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

Shell脚本编程学习——Shell函数基础【显哥出品,必为精品】

Shell编程 lixian 4年前 (2020-05-06) 1341次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、Shell函数基本介绍

1.什么是函数?

函数其实就是一堆命令的集合,用来完成特定的功能代码块,方便后期调用。

2.函数的作用

函数可以将代码进行模块化,便于后期的代码的复用,增加脚本的可读性
函数和变量类似,先定义,后引用,直接写函数名就能直接引用 不引用就不会执行函数

二、定义函数格式

定义函数有两种方式

1.方式一

函数名() {
    command1
    command2
    .....
}

2.方式二

function 函数名 {
    command1
    command2
    ........
}

三、函数的语法示例

[root@xian ~]# fun1() { echo "lixian"; echo "hen nice"; }
[root@xian ~]# fun1
lixian
hen nice
[root@xian /server/scripts]# cat fun1.sh 
#!/bin/bash
# File Name: fun1.sh
# Author: lixian
#######################
#定义函数
fun1() {
    echo "当前系统磁盘信息:";
    df -h;
}
#引用函数
fun1
[root@xian /server/scripts]# sh fun1.sh 
当前系统磁盘信息:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  2.5G   16G  14% /
devtmpfs        476M     0  476M   0% /dev
tmpfs           487M     0  487M   0% /dev/shm
tmpfs           487M  7.6M  479M   2% /run
tmpfs           487M     0  487M   0% /sys/fs/cgroup
/dev/sda1      1014M  127M  888M  13% /boot
tmpfs            98M     0   98M   0% /run/user/0

四、 函数的参数传递

[root@xian /server/scripts]# cat fun2.sh 
#!/bin/bash
# File Name: fun2.sh
# Author: lixian
#######################
fun1() {
    echo "$1 $2"           #只接收调用函数的第一个和第二个位置参数
}
fun1 $1 $2 $3             #只接收脚本输入的第一个、第二个和第三个位置参数
fun1 $1 $1               #只接收脚本输入的第一个和第一个参数
fun1 $3 $3               #只接收脚本输入的第三个和第三个参数
[root@xian /server/scripts]# sh fun2.sh aaa bbb ccc
aaa bbb
aaa aaa
ccc ccc

五、使用函数编写计算器脚本

[root@xian /server/scripts]# cat jisuan.sh 
#!/bin/bash
# File Name: jisuan.sh
# Author: lixian
#######################
fun1() {
    case $2 in
    +|-|%|^)
        awk "BEGIN{print $1 $2 $3}" 
        ;;
    x)
        awk "BEGIN{print $1 * $3}"
        ;;
    ÷)
        awk "BEGIN{print $1 / $3}"
        ;;
    *)
        echo "错误!你的第二个参数不是操作符!"
        exit
    esac
}
if [ $# -ne 3 ];then
    echo "错误!你的操作符不是三个!重新输入!"
fi
echo "$1 $2 $3 = `fun1 $1 "$2" $3`"

[root@xian /server/scripts]# sh jisuan.sh 10 + 2
10 + 2 = 12
[root@xian /server/scripts]# sh jisuan.sh 9 - 3
9 - 3 = 6
[root@xian /server/scripts]# sh jisuan.sh 2 x 7
2 x 7 = 14
[root@xian /server/scripts]# sh jisuan.sh 12 ÷ 3
12 ÷ 3 = 4
[root@xian /server/scripts]# sh jisuan.sh 12 % 7
12 % 7 = 5
[root@xian /server/scripts]# sh jisuan.sh 3 ^ 5
3 ^ 5 = 243
[root@xian /server/scripts]# sh jisuan.sh 12 ÷ 7
12 ÷ 7 = 1.71429

六、使用函数编写Nginx启动脚本

1.脚本代码

[root@xian /server/scripts]# cat nginx.sh 
#!/bin/bash
# File Name: nginx.sh
# Author: lixian
#######################
#调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#判断是否为root用户
if [ $USER != "root" -o $UID -ne 0 ];then
    action "错误:当前用户${USER}没有权限执行脚本!" /bin/false
    exit
fi
#判断位置变量是否存在且只有一个
if [ $# -ne 1 ];then
    action "Usage: $0 {start|stop|status|restart|reload}" /bin/false
    exit
fi
#加锁机制
Suo=/tmp/nginx.lock
if [ -f $Suo ];then
    action "此脚本${0}正在执行中!" /bin/false
    exit
fi
touch $Suo  &>/dev/null
#编写函数
State=$1
Pid_File=/var/run/nginx.pid
#定义返回状态的函数
Return() {
    if [ $? -eq 0 ];then
        action "Nginx服务 $State 成功!" /bin/true
    else
        action "Nginx服务 $State 失败!" /bin/false
    fi
}
#编写语法错误函数
Syntax_Err() {
    local Err_Log=/tmp/nginx_err.log
    /usr/sbin/nginx -t &>$Err_Log
    Err_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Err_Log)
    Err_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Err_Log)
    /usr/sbin/nginx -t
    action "检查Nginx配置文件错误!文件为:$Err_File,错误的行为第 $Err_Line 行!" /bin/false
    read -p "请问你是否选择进行修改配置文件[Yes|No]: " Confirm
    case $Confirm in
        y|Y|Yes|yes)
            vim +$Err_Line $Err_File
            /usr/sbin/nginx -t &>/dev/null
            if [ $? -eq 0 ];then
                State_1() {
                    /usr/sbin/nginx &>/dev/null && sleep 3
                }
                State_2() {
                    /usr/sbin/nginx -s reload &>/dev/null && sleep 3
                }
                if [ $State == "start" ];then
                    State_1
                    Return
                fi
                if [ $State == "reload" ];then
                    State_2
                    Return
                fi
                if [ $State == "stop" -o $State == "restart" ];then
                    /usr/sbin/nginx -s stop &>/dev/null && sleep 3
                    Return
                fi
            else
                action "Nginx配置文件错误!" /bin/false
            fi
            ;;
        n|N|No|NO)
            echo "你选择不进行修改配置文件!"
            ;;
        *)
            echo "你输入的不符合要求!"
    esac
}
#编写启动函数
Start() {
    if [ -f $Pid_File ];then
        action "Nginx服务正在运行中!"  /bin/true
    else
        /usr/sbin/nginx -t &>/dev/null
        if [ $? -eq 0 ];then
            /usr/sbin/nginx &>/dev/null && sleep 3
            Return
        else
            action "Nginx配置文件错误!" /bin/false
            Syntax_Err
        fi
    fi
}
#编写停止函数
Stop() {
    if [ -f $Pid_File ];then
        /usr/sbin/nginx -t &>/dev/null
        if [ $? -eq 0 ];then
            /usr/sbin/nginx -s stop &>/dev/null && sleep 3
            Return
        else
            Syntax_Err
        fi
    else
        action "Nginx服务未在运行!" /bin/true
    fi
}
#编写状态函数
Status() {
    if [ -f $Pid_File ];then
        action "Nginx服务正在运行中!"  /bin/true
    else
        action "Nginx服务未在运行!" /bin/true
    fi
}
#编写重启函数
Restart() {
    if [ -f $Pid_File ];then
        local State=stop
        Stop 
        local State=start
        Start
    else
        action "Nginx服务未在运行!" /bin/true
        Start 
    fi
}
#编写平滑重启操作
Reload() {
    if [ -f $Pid_File ];then
        /usr/sbin/nginx -t &>/dev/null
        if [ $? -eq 0 ];then
            /usr/sbin/nginx -s reload &>/dev/null && sleep 3
            Return
        else
            Syntax_Err
        fi
    else
        action "Nginx服务未在运行!不能重启!" /bin/false
    fi
}
#编写case语句
case $1 in
    start)
        Start
        ;;
    stop)
        Stop
        ;;
    status)
        Status
        ;;
    restart)
        Restart
        ;;
    reload)
        Reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
esac
#解锁
rm -f $Suo

2.执行结果

[root@xian /server/scripts]# sh nginx.sh
Usage: nginx.sh {start|stop|status|restart|reload}         [  失败  ]
[root@xian /server/scripts]# sh nginx.sh start
Nginx服务正在运行中!                                      [  成功  ]
[root@xian /server/scripts]# sh nginx.sh status
Nginx服务正在运行中!                                      [  成功  ]
[root@xian /server/scripts]# sh nginx.sh stop
Nginx服务 stop 成功!                                      [  成功  ]
[root@xian /server/scripts]# sh nginx.sh restart
Nginx服务未在运行!                                        [  成功  ]
Nginx服务 restart 成功!

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:Shell脚本编程学习——Shell函数基础【显哥出品,必为精品】
喜欢 (0)

您必须 登录 才能发表评论!