脚本添加删除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

 

# 添加

 

# 删除

posted @ 2015-12-29 14:29  ca0guo  阅读(841)  评论(0编辑  收藏  举报