下面的脚本应该管理添加和删除条目到主机文件。 .dev条目应该指向本地主机,所有其他应提示输入IP地址。如果条目已经存在,它应该问你是否要删除它。批处理文件添加/删除主机文件条目语法错误
我得到一个模糊的The syntax of the command is incorrect
错误,但我不确定它指的是哪一行。如果我在其上打开@echo
,则在下一行上吐出if (
,然后死亡。我能够发现错误的唯一方法是做一个截图,因为它会立即退出。
我已经花了几个小时在这个,所以任何援助将不胜感激!
@echo off
title ClearSight Studio
echo Virtual website setup script
echo Version 1.4
echo Last modified 12/30/2011
echo.
REM Get the name of the domain name
echo What domain name? Ctrl+C to cancel. Example: newdomain.dev.
set /p domain= Domain:
REM Set a variable for the Windows hosts file location
set hostpath=\windows\system32\drivers\etc
set hostfile=hosts
set sitespath=\clearsight\sites
set extension=%domain:~-3%
REM Make the hosts file writable
attrib -r %hostpath%\%hostfile%
REM Add the domain to point to localhost at the end of the file if it doesn't already have an entry.
find /c " %domain%" %hostpath%\%hostfile%
if errorlevel 1 (
if /i %extension%==dev (
set /p ip= What is the IP?
)
echo. >> %hostpath%\%hostfile%
echo.# Adding %domain% via batch process on %date:~10,4%-%date:~4,2%-%date:~7,2%. >> %hostpath%\%hostfile%
if %ip% (
echo.%ip% %domain% >> %hostpath%\%hostfile%
) else (
echo.127.0.0.1 %domain% >> %hostpath%\%hostfile%
)
) else if errorlevel 0 (
echo Entry found in hostfile already. Would you like to remove it?
set /p remove= Remove (Y/N)
if /i %remove%==Y (
findstr /v "%domain%" %hostpath%\%hostfile% > %hostpath%\%hostfile%
)
)
if /i %extension%==dev (
REM Create the folder if it doesn't exist
if exist %sitespath%\%domain% (
echo %sitespath%\%domain% already exists.
) else (
echo Creating %sitespath%\%domain%...
mkdir %sitespath%\%domain%
)
)
REM Clean up
attrib +r %hostpath%\%hostfile%
echo.
echo Done.
if /i %extension%==dev (
REM Do a "git" of the repo, if requested
echo Would you like to clone an external git repository named %domain% from Bitbucket?
echo This assumes the git repository was set up under the "jamonholmgren" account.
echo Do it manually if it's under someone else's account. Also, make sure you have permissions to this repo.
set /p getgit= Get git clone? (Y/N):
)
REM
if /i %getgit%==Y (
git clone [email protected]:jamonholmgren/%domain%.git %sitespath%\%domain%\
pause
) else (
echo Okay, then we're done.
pause
)
我想'ip'也必须做出不确定的在开始的地方(后'SETLOCAL ...'可能),只是为了当'如果定义的IP ...'调用安全起见,以防用户已经按下Enter键而没有输入任何内容。 – 2012-02-23 07:32:25
@AndriyM - 是的,我看到了。我认为验证输入值可能比初始化值更重要。我很懒,并没有谈论它,但我现在要修改答案。 – dbenham 2012-02-23 12:39:07