Shell编程函数实战

Shell编程函数实战

  • Shell允许将一组命令集或语句形成一个可用块,这些块称为Shell函数,Shell函数的用于在于只需定义一次,后期随时使用即可,无需在Shell脚本中添加重复的语句块,其语法格式以function name(){开头,以}结尾。
  • Shell编程函数默认不能将参数传入()内部,Shell函数参数传递在调用函数名称传递,例如name args1 args2
function name (){
        command1
command2
        ........
}
name args1 args2

函数语句Shell脚本编程案例如下:

  1. SHELL编程开发Nginx多版本软件安装脚本
    要求如下:
  • 支持多版本的安装
  • 支持创建多个虚拟主机
  • 支持删除多个虚拟主机
#!/bin/bash
#2020年7月24日09:37:02
#auto congfig nginx virtual hosts
#by author Xiao Yu er
########################
#NGX_VER="$*"
NGX_YUM="yum install -y"
NGX_DIR="/usr/local/nginx"
NGX_URL="http://nginx.org/download"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"

function nginx_help(){
        echo -e "\033[33mNginx VIrtual Manager SHELL Scripts\033[0m" 
        echo -e "\033[33m-------------------------------\033[0m" 
        echo -e "\033[33m1)-I 1.12.2|1.15.0|1.16.0\033[0m" 
        echo -e "\033[33m2)-A v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m" 
        echo -e "\033[33m3)-D v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m" 
        echo -e "\033[35mUsage:{/bin/bash $0 -I(Install) -A(add) or -D(del) -H(help)\033[0m" 
        exit 0
}

if [ $# -lt 2 ];then
        nginx_help
        exit 1
fi
        if [ ! -f $NGX_DIR/sbin/nginx ];then
                #Install nginx web
                $NGX_YUM wget gzip tar make gcc
                $NGX_YUM pcre pcre-devel zlib-devel
                cd /usr/src
                wget -c ${NGX_URL}/nginx-$*.tar.gz
                ls -l nginx-$*.tar.gz
                tar -xzvf nginx-$*.tar.gz
                cd nginx-$*/
                useradd -s /sbin/nologin www -M
                ./configure --prefix=$NGX_DIR/ $NGX_ARGS
                make
                make install
                $NGX_DIR/sbin/nginx
                ps -ef|grep nginx
                netstat -tnlp|grep 80
                setenforce 0
                systemctl stop firewalld.service
        else    
                echo -e "\033[33m-----------------------\033[0m"                                   
                echo -e "\033[32mThe Nginx WEB Already Install,Please Exit.\033[0m"  
                echo "ls -l $NGX_DIR"
                ls -l $NGX_DIR 
        fi      
}       

function config_vhost(){
        #Config nginx virtual hosts
        cd $NGX_DIR/conf/
        \cp nginx.conf nginx.conf.bak
        grep "vhost" nginx.conf >/dev/null
        if [ $? -ne 0 ];then
                cat>nginx.conf<<-EOF
                worker_processes  1;
                events {
                    worker_connections  1024;
                }
                http {
                    include       mime.types;
                    default_type  application/octet-stream;
                    sendfile        on;
                    keepalive_timeout  65;
                    include /usr/local/nginx/conf/vhost/*.conf
                }
                EOF
                mkdir -p $NGX_DIR/conf/vhost
        fi
        for NGX_VHOST in $*
        do
                cd $NGX_DIR/conf/vhost/
                if [ ! -f ${NGX_VHOST}.conf ];then
                        cat>${NGX_VHOST}.conf <<-EOF
                        server {
                                listen       80;
                                server_name  $NGX_VHOST;
                                location / {
                                    root   html/$NGX_VHOST;
                                    index  index.html index.htm;
                                }
                                error_page   500 502 503 504  /50x.html;
                                location = /50x.html {
                                    root   html;
                                }
                        }
                        EOF
                        mkdir -p $NGX_DIR/html/$NGX_VHOST/
                        cd $NGX_DIR/html/$NGX_VHOST/
                        cat>index.html<<-EOF
                        <html>
                        <h1>$NGX_VHOST Test Pages</h1>
                        <hr color=red>
                        </html>
                        EOF
                        echo -e "\033[32mThe $NGX_VHOST Add success.\033[0m"
                        echo -e "\033[32m---------------\033[0m"
                        cat $NGX_DIR/conf/domains/$NGX_VHOST
                        echo -e "\033[32m---------------\033[0m"
                        $NGX_DIR/sbin/nginx -t
                        $NGX_DIR/sbin/nginx -s reload
                else
                        echo -e "\033[32mThe $NGX_VHOST already exists.\033[0m"
                        echo -e "\033[32m---------------\033[0m"
                        cat $NGX_DIR/conf/domains/$NGX_VHOST
                fi
        done
}

function delete_vhost(){
        for NGX_VHOST in $*
        do
                echo -e "\033[32m---------------\033[0m"
                ls -l|grep "$NGX_VHOST"
                cat $NGX_DIR/conf/vhost/${NGX_VHOST}.conf
                if [ $? -eq 0 ];then
                        read -p "Are you sure you want to delete it,yes or no? " INPUT
                        case $INPUT in
                                yes|YES|Yes|y|Y)
                                rm -rf $NGX_DIR/conf/vhost/${NGX_VHOST}.conf
                                rm -rf $NGX_DIR/html/$NGX_VHOST
                                echo -e "\033[32m---------------\033[0m"
                                echo -e "\033[32mThe $NGX_VHOST delete success.\033[0m"
                                $NGX_DIR/sbin/nginx -s reload
                                ;;
                                * )
                                exit 0
                                ;;
                        esac
                fi
        done
}

case $1 in
        -I )
        shift 1
        nginx_install $*
        ;;
        -A )
        shift 1
        config_vhost $*
        ;;
        -D )
        shift 1
        delete_vhost $*
        ;;
        * )
        nginx_help
        ;;
esac
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值