用if实现一键部署Nginx,当参数只有1个时才能正常安装

 1.进入/data/sh/目录创建20240819文件夹,并复制auto_install_nginx_v2.sh为auto_install_nginx_v3.sh

[root@localhost ~]# cd /data/sh/
[root@localhost sh]# ls
20240816
[root@localhost sh]# mkdir 20240819
[root@localhost sh]# ls
20240816  20240819
[root@localhost sh]# cp 20240816/
auto_install_nginx_v2.sh  if_test.sh                
[root@localhost sh]# cp 20240816/auto_install_nginx_v2.sh 20240819
[root@localhost sh]# cd 20240819
[root@localhost 20240819]# ls
auto_install_nginx_v2.sh
[root@localhost 20240819]# mv auto_install_nginx_v2.sh auto_install_nginx_v3.sh 
[root@localhost 20240819]# ls
auto_install_nginx_v3.sh
[root@localhost 20240819]# vim auto_install_nginx_v3.sh

 2.编辑auto_install_nginx_v3.sh文件

#!/bin/bash
#2024年8月14日21:06:20
#auto install nginx web
#by author www.xin.net
########################
NGX_VER=$1
NGX_SRC="/usr/src/"
NGX_DIR="/usr/local/nginx"
NGX_SOFT="nginx-${NGX_VER}.tar.gz"
NGX_URL="http://nginx.org/download"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"
#从Nginx官网下载软件包;
wget -c $NGX_URL/$NGX_SOFT -P $NGX_SRC
#Cd切换至/usr/src/;
cd $NGX_SRC
ls -l $NGX_SOFT
#通过Tar工具对其解压;
tar -xzvf $NGX_SOFT
#Cd切换至Nginx源代码目录;
cd nginx-$NGX_VER/
#创建www用户和组,解决依赖环境、库文件;
useradd -s /sbin/nologin www -M
yum install -y pcre-devel zlib-devel openssl-devel
#预编译;
./configure --prefix=$NGX_DIR/ $NGX_ARGS
#编译;
make -j4
#安装;
make -j4 install
#查看Nginx软件服务是否部署成功;
ls -l $NGX_DIR/
#启动Nginx服务进程;
$NGX_DIR/sbin/nginx
#查看Nginx进程状态;
ps -ef|grep nginx
#关闭Firewalld防火墙;
systemctl stop firewalld.service

 实现不输入参数或者不输入参数,输入以下if判断内容$#代表参数个数,实现没有参数输入时,退出脚本执行

NGX_URL="http://nginx.org/download"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"
if [ $# -eq 0 ]; then
        echo "---------------"
        echo "Usage:{/bin/sh auto_install_nginx_v3.sh 1.12.2|1.26.1|help}"
        exit
fi
#从Nginx官网下载软件包;
wget -c $NGX_URL/$NGX_SOFT -P $NGX_SRC

 在执行脚本时,会显示以下内容

 

 也可将auto_install_nginx_v3.sh改为$0,$0表示当前脚本名称如下:

 执行结果如下

 打印字体颜色

echo -e "\033[32m ----------- \033[0m"

 实现如果参数>=2也退出脚本$# -ge 2表示参数>=2个

if [ $# -eq 0 ]; then
        echo "---------------"
        echo "Usage:{/bin/sh $0 1.12.2|1.26.1|help}"
        exit
fi
if [ $# -ge 2 ];then
        echo -e "\033[32m----------------------\033[0m"
        echo -e "\033[32mUsage:{/bin/sh $0 1.12.2|1.26.1|help}\033[0m"
        exit
fi

执行结果如下

 当输入参数为1个时,即可正常安装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值