批量生成httpd-2.2十个虚拟主机配置,要求使用函数,创建,查看,删除虚拟主机bash shell 脚本。

[root@www ~]# vim /scripts/tenxunizhuji.sh

#!/bin/bash

vhostpeiz(){

    if [ -f /etc/httpd/conf.d/vhost$1.conf ];then

        rm -f /etc/httpd/conf.d/vhost$1.conf

        rm -rf /data/vhosts/www$1

    fi

   touch /etc/httpd/conf.d/vhost$1.conf

   mkdir -p /data/vhosts/www$1

   file="/etc/httpd/conf.d/vhost$1.conf"

   echo -e "<VirtualHost *:80>\nServerName www$1

DocumentRoot /data/vhosts/www$1\nCustomLog logs/www$1-access_log\n</VirtualHost>" >> $file

}

delvhost() {

     rm -f /etc/httpd/conf.d/vhost$1.conf

     rm -rf /data/vhosts/www$1

}

listvhost() {

   read -p "please input (a or vhostn(1-10):" select

      if [[ "$select" == "a" ]];then

      vhost=`ls -1 /etc/httpd/conf.d/vhost*.conf | cut -d / -f 5 | cut -d . -f1`

      echo $vhost

      elif [[ $select =~ vhost([1-9]|10) ]];then

         cat /etc/httpd/conf.d/$select.conf 2> /dev/null || echo "$select is not exist"

      else

          echo "$select is not exist"

          exit 100

     fi

}

read -p "please input a string(create|del|list)  " string

case $string in

  create)

      for a in `seq 1 10`;do

          vhostpeiz $a

      done

  ;;

  del)

       read -p "please input (a or vhost(1-10):" vhost

         if [[ $vhost == "a" ]];  then

             read -p "delete all vhost?[Y|N]:"  think

             case $think in

                 [Yy]|[Yy][Ee][Ss])

                       for a in `seq 1 10`;do

                           delvhost $a

                       done

                       ;;

                 [Nn]|[Nn][Oo])

              exit 100

               ;;

              esac

          elif [[ $vhost =~ vhost([1-9]|10) ]];then

               if [ -f /etc/httpd/conf.d/$vhost.conf ];then

               read -p "delete $vhost?[Y|N]:" think2

             case $think2 in

               [Yy]|[Yy][Ee][Ss])

             num=$(echo $vhost | tr -d [a-z])

             delvhost $num

             ;;

            [Nn]|[Nn][Oo])

              exit 200

            ;;

             esac

            else

                echo "$vhost is not exist"

                exit 120

            fi

          else

            echo "$vhost vhost is not exist"

        fi

  ;;

  list)

    listvhost

  ;;

  esac