![Bat文件用于自动化一些Web开发](https://linux22.com/image/1365909/Bat%E6%96%87%E4%BB%B6%E7%94%A8%E4%BA%8E%E8%87%AA%E5%8A%A8%E5%8C%96%E4%B8%80%E4%BA%9BWeb%E5%BC%80%E5%8F%91.png)
在 Windows 环境中创建虚拟主机非常麻烦。您必须创建站点文件夹、编辑 httpd.conf、hosts 和 whatnow。为了自动化其中的一些操作,我决定尝试创建一个批处理文件来自动化其中的一些操作。以下是我为初学者想出的方法:
@ECHO OFF
::location of vhosts
SET BASEURL=D:\web\vhosts\
:sitenameprompt
::ask for a sitename
set /p sitename="Enter Sitename: " %=%
echo Before IF
IF EXIST %BASEURL%%sitename% echo The specified site folder already exists! Choose another.
IF EXIST %BASEURL%%sitename% goto sitenameprompt
IF NOT EXIST %BASEURL%%sitename% echo Creating folder
IF NOT EXIST %BASEURL%%sitename% MD %BASEURL%%sitename%
echo Adding virtualhost to httpd.conf
pause
(
echo ###%sitename%###
echo <VirtualHost *:80>
echo ServerAdmin admin@%sitename%
echo DocumentRoot "%BASEURL%%sitename%"
echo ServerName %sitename%.dev
echo ServerAlias %sitename%.dev
echo <Directory "%BASEURL%%sitename%">
echo Options Indexes FollowSymLinks Includes ExecCGI
echo Order allow,deny
echo Allow from all
echo </Directory>
echo </VirtualHost>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf
pause
echo Restarting apache
::httpd -k restart
现在运行此脚本时,httpd.conf 部分会出现语法错误。请记住,我对此还不太熟悉,所以这可能是显而易见的。那么我到底在哪里搞砸了呢?
答案1
<>|&
必须用插入符号批量转义丑陋的符号^
,试试这个:
@echo off
(
echo ###%sitename%###
echo ^<VirtualHost *:80^>
echo ServerAdmin admin@%sitename%
echo DocumentRoot "%BASEURL%%sitename%"
echo ServerName %sitename%.dev
echo ServerAlias %sitename%.dev
echo ^<Directory "%BASEURL%%sitename%"^>
echo Options Indexes FollowSymLinks Includes ExecCGI
echo Order allow,deny
echo Allow from all
echo ^</Directory^>
echo ^</VirtualHost^>
) >>D:\web\xampp\apache\conf\extra\httpd-vhosts.conf