批处理文件添加/删除hosts文件条目语法错误 [英] Batch file add/remove hosts file entries syntax error

查看:192
本文介绍了批处理文件添加/删除hosts文件条目语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本应该管理添加和删除条目到主机文件。与.dev条目应该指向到本地主机和所有其他应提示输入一个IP地址。如果该项已经存在,如果你想删除它,它应该问你。

我得到一个模糊的该命令的语法不正确错误,但我不知道哪一行它引用。如果我把 @echo 就可以了,然后吐出如果(下一行,然后死去。只有这样,我可以捕获的错误就是做一个屏幕截图,因为它会立即退出。

我花了这个好几个小时,因此任何援助将大大AP preciated!

 关闭@echo标题ClearSight工作室
回声虚拟网站安装脚本
回声1.4版
回声最后修改2011/12/30
回声。REM获取域名的名称
回声是什么域名?按Ctrl + C键取消。例如:newdomain.dev。
集/ p域=域名:REM设置为Windows变量主机文件位置
集hostpath = \\ WINDOWS \\ SYSTEM32 \\ drivers \\ etc下
设置HOSTFILE =主机
设置sitespath = \\ clearsight \\网站
集扩展=%域:〜-3%REM使hosts文件写
ATTRIB -r%hostpath%\\%HOSTFILE%REM添加域名指向在文件的结尾为localhost,如果它不已经有一个条目。
查找/ C%域%%hostpath%\\%HOSTFILE%
如果错误级别1(
    如果/ I%扩展名%==开发(
        集/对IP =什么是知识产权?
    )
    回声。 >> %hostpath%\\%HOSTFILE%
    #呼应通过批处理进程%日期添加%域%:10.4〜% - %日期:〜4.2% - %日期:7,2〜%。 >> %hostpath%\\%HOSTFILE%
    如果IP%%(
        回声%IP%%域%方式>> %hostpath%\\%HOSTFILE%
    )其他(
        echo.127.0.0.1%域%>> %hostpath%\\%HOSTFILE%
    )
)否则,如果错误级别0(
    回声在入口发现HOSTFILE已。你想删除它?
    集/ P集合中删除=删除(Y / N)
    如果/ I%删除%== Y(
        FINDSTR / V%域%%hostpath%\\%HOSTFILE%GT; %hostpath%\\%HOSTFILE%
    )
)如果/ I%扩展名%==开发(
    REM创建文件夹,如果它不存在
    如果存在%sitespath%\\%域%(
        回声%sitespath%\\%域%已经存在。
    )其他(
        回声创建%sitespath%\\%域%...
        MKDIR%sitespath%\\%域%
    )
)REM清理
ATTRIB + R%hostpath%\\%HOSTFILE%回声。
回声完成。如果/ I%扩展名%==开发(
    REM做一个回购的混帐,如果要求
    回声你想从克隆名为到位桶域%%外部Git仓库?
    回声这假设是根据jamonholmgren帐户设置Git仓库了。
    回声做手工,如果它是在别人的帐户。此外,请确保您有权限此回购。
    集/ p getgit =获取混帐克隆? (Y / N):
)REM
如果/ I%getgit%== Y(
    混帐克隆git@bitbucket.org:jamonholmgren /%域%的.git%sitespath%\\%域%\\
    暂停
)其他(
    回声好吧,那我们就大功告成了。
    暂停


解决方案

您有两个问题位$ C $的C

 如果IP%%(
    回声%IP%%域%方式>> %hostpath%\\%HOSTFILE%
)其他(


  • 这是无效的语法。我presume要测试是否变量定义。正确的语法是如果定义的IP(


  • 您正试图展开%IP%在相同的,如果()块在那里它被定义。这可不行,因为IP%%的在分析时扩大,这是分配前值!解决的办法是使用延迟膨胀,而不是!IP!。延迟扩展必须先启用,可能是靠近你的脚本的顶部,使用 SETLOCAL enableDelayedExpansion


因此​​固定code会是这个样子。

  SETLOCAL enableDelayedExpansion



如果定义的IP(
    回音!IP! %域%>> %hostpath%\\%HOSTFILE%
)其他(

您有%删除%相同的延迟扩展的问题。

修改 - 扩大对舍甫琴科M的评论

您也有您如何处理用户输入的IP,并删除一个潜在的问题。这两个值应该初始化要么没有,或一个默认值,提示的值之前。如果用户没有输入任何内容,则现有的默认值(如果有的话)是preserved

此外,您可能需要确认有效的值进入,特别是对IP。如果这样做,那么你将要采取及时退出循环,并把它放在一个调用的子程序。子程序可以检查是否输入了值,环回再试,如果事实并非如此。它必须是在一个子程序,因为你可以code的一个组合块中没有像GOTO你与你的IF语句。

The following script is supposed to manage adding and removing entries into the hosts file. Entries with .dev are supposed to point to localhost and all others should prompt for an IP address. If the entry already exists it should ask you if you want to remove it.

I am getting a vague The syntax of the command is incorrect error but I'm not sure which line it's referring to. If I turn @echo on it then spits out if ( on the next line and then dies. The only way I can catch the error is to do a screenshot because it immediately exits.

I've spent several hours on this, so any assistance would be greatly appreciated!

@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 git@bitbucket.org:jamonholmgren/%domain%.git %sitespath%\%domain%\
    pause
) else (
    echo Okay, then we're done.
    pause
)

解决方案

You have two problems with this bit of code

if %ip% (
    echo.%ip%       %domain% >> %hostpath%\%hostfile%
) else (

  • It is invalid syntax. I presume you want to test if the variable is defined. The proper syntax is if defined ip (

  • You are attempting to expand %ip% in the same if() block where it was defined. This can't work because %ip% is expanded at parse time, which is before you assign the value! The solution is to use delayed expansion instead !ip!. Delayed expansion must first be enabled, probably near the top of your script, using setlocal enableDelayedExpansion

So the fixed code would look something like this

setlocal enableDelayedExpansion
.
.
.
if defined ip (
    echo.!ip!       %domain% >> %hostpath%\%hostfile%
) else (

You have the same delayed expansion issue with %remove%

Edit - expanding on Andriy M's comment

You also have a potential issue with how you deal with user input for ip and remove. Both values should be initialized either to nothing, or to a default value, prior to prompting for the value. If the user does not enter anything, then the existing default value (if any) is preserved.

Also, you may want to confirm that a valid value was entered, especially for the ip. If you do, then you will want to take the prompt out of the loop and put it in a called subroutine. The subroutine can check if a value was entered and loop back to try again if it wasn't. It must be in a subroutine because you cannot GOTO within a parenthesized block of code like you have with your IF statement.

这篇关于批处理文件添加/删除hosts文件条目语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆