脚本添加删除nginx配置中的内容

[root@nodejs script]# more editnginx.sh 
#!/bin/bash
#


function back_check(){

# 备份配置和覆盖配置文件
cp -rf /etc/nginx/nginx.conf /etc/nginx/nginx.conf-$(date +%Y%m%d-%H%M%S) && \
echo "[+] /etc/nginx/nginx.conf-$(date +%Y%m%d-%H%M%S) backup OK!"

# 对比修
diff nginx.conf /etc/nginx/nginx.conf

# 覆盖线上配置
read -p "Write nginx config File(y/n)?" inString
if [ $inString == "y" ];then
  cp -rf nginx.conf /etc/nginx/nginx.conf
elif [ $inString == "n"];then
  echo "Quit!!!"
  exit 1
else
  echo "args error!!"
  exit 2
fi

nginx -t -c /etc/nginx/nginx.conf
}



# 添加节点函数
function insert_row(){
# 编辑配置文件
nginx -t -c /etc/nginx/nginx.conf
RETURN=$?

if [ $RETURN == 0 ];then
cp -rf /etc/nginx/nginx.conf nginx.conf && echo "[+] Copy nginx.conf to current directory OK!!"

sed -i -n -e :a -e '1,2!{P;N;D;};N;ba' nginx.conf

echo "        location ~/api/v$1 {
            proxy_pass http://127.0.0.1:60$1;
        }
    }
}" >>nginx.conf

fi

back_check
}





# 删除节点函数
function delete_row(){
   cp -rf /etc/nginx/nginx.conf nginx.conf && echo "[+] Copy nginx.conf to current directory OK!!"
   sed -i "/location ~\/api\/v$1/,/}/d" nginx.conf
   back_check
   #sed -n "$1p" nginx.conf
}



# 帮助选项
function help(){
  echo -e "Usage: $(basename $0) {-h|-i|-d}\n"
}


if [ -z $1 ];then
  help
fi

while getopts "i:d:h" OPTIONS; do
  case ${OPTIONS} in
    i ) insert_row $OPTARG;;
    d ) delete_row $OPTARG;;
    h ) help; exit;;
  esac
done

 

# 添加

 

# 删除

转载于:https://www.cnblogs.com/caoguo/p/5085631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值