批量生成10个虚拟主机配置

网友投稿 255 2022-10-30


批量生成10个虚拟主机配置

写一个脚本,批量生成10个虚拟主机配置:     /etc/           接受命令行参数,作为命令和主机名传递;           使用函数:      列出:list [-a|vhost_name]      创建:create vhost_name      删除:delete [-a|vhost_name]

#!/bin/bash#function: create param_1 param_2 param_3 ...create(){ for i ; do file="/etc/#suffix ".conf" is missing. if [[ ! -e $file ]]; then    cat << EOF >>$fileServerName /data/vhosts/logs/combined   EOF fi    done }

#del param_1 param_2 param_3 ...del(){  for i ;  do   file="/etc/[[  -e $file ]];   then    /bin/rm -i $file   fi  done}

#list param_1 param_2 param_3 ...list(){  for i ;  do   file="/etc/[[  -e $file ]];   then    echo  $i   fi  done}

vh_name=vhostTEMP=`getopt -o c:d:l:: --long create:,delete:,list:: -n 'report' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!eval set -- "$TEMP"echo "$@"while true ; do case "$1" in  -c|--create)     create_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')    shift 2    #echo $create_param    ;;  -d|--delete)     case "$2" in    all)     delete_param=$(/bin/ls /etc/| grep -Po "(?<=$vh_name)[^[:space:]]+" | tr '\n' ' ')     ;;    *)     delete_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')     ;;    esac    shift 2    #echo $delete_param    ;;  -l|--list)     # l has an optional argument. As we are in quoted mode,    # an empty parameter will be generated if its optional    # argument is not found.    case "$2" in    "")     list_param=$(/bin/ls /etc/| grep -Po "(?<=$vh_name)[^[:space:]]+" | tr '\n' ' ')     shift 2      ;;    *)          list_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')        shift 2        echo $list_param        ;;    esac    ;;  --) shift ; break ;;  *) echo "Internal error!" ; exit 1 ;; esacdone#echo "Remaining arguments:"for arg do echo 'extra argument --> '"\`$arg'" ; doneif [[ ! -z $arg ]];then echo "error,exit 1";exit 1;fi

if [[ ! -z $create_param ]];then create_files=$(echo $create_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}') create $create_filesfiif [[ ! -z $list_param ]];then list_files=$(echo $list_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}') list $list_filesfiif [[ ! -z $delete_param ]];then delete_files=$(echo $delete_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}') del $delete_filesfi


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java配置DBeaver的详细步骤
下一篇:httpd-2.2和httpd-2.4虚拟主机的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~