【问题标题】:Batch file | symbol: inserting a space for no apparent reason批处理文件 |符号:无缘无故插入空格
【发布时间】:2012-11-14 19:00:42
【问题描述】:

我有一个包含这个 sn-p 的批处理文件:

systeminfo|findstr /B "Host Name:"|findstr /C:"COMPNAME-SET"
if %ERRORLEVEL% EQU 0 (
echo This computer is either a test machine or has not had it's name configured yet. Continuing will run the script in test mode.
pause
)

我不知道为什么,但顶行不起作用。输出是这样的:

Z:\>systeminfo | findstr /B "Host Name:"  | findstr /C:"IGSWIDWB-SET"
The syntax of the command is incorrect.
Z:\>

在 " 标记之后似乎有一个额外的空格。我曾经在管道和命令之间使用空格,但我删除了它们以尝试修复此错误。没有任何区别。我已确保文件是带有 Windows 样式字符的 Windows 格式,我现在不知道该怎么做。这是我正在使用的标准批处理脚本。

此代码可让我在我开发的自动设置脚本中确定它是笔记本电脑还是台式机。主机名具有特定协议,它将根据此步骤浏览一组安装。

有人知道怎么回事吗? 谢谢!

作为对 [name here] 的响应,这里是在它之前执行的代码(以及在它之后执行的一些代码);但是我已经修改了这些词,所以不清楚它是什么。我在一个喜欢我不共享内部文档的组织工作,但不介意我是否混淆它。失败的部分在第一个 systeminfo 所在的行。

::@echo off
setlocal
set BATDIR=%~dp0%
set SCRIPTVER=3.2
cls
set SUBDEPARTMENT=false

if not [%1]==[] (
    if "%1" EQU "/help" (
        echo Calling For Help.
        CALL :HELP
        goto :EOF
       )

    if "%1" EQU "/SUBDEPARTMENT" (
        set SUBDEPARTMENT=true
    echo Set SUBDEPARTMENT options to true.
    ::echo SUBDEPARTMENT Switch does not work on this version of the script.  ::CD VERSION ONLY :: Continuing without.
    echo.
    goto CONTINUE
    )
)

:Continue
color 0B
   echo XXXX AutoInstaller Script for Windows 7/XP (x86/amd64)
   echo Version %SCRIPTVER% - Build Date: 11/14/2012
   echo Estimated Install Time: 80(Core) to 180(SUBDEPARTMENT) minutes
   echo Computer will reboot to phase 2 once completed.
   echo A log file for this script can be found at %USERPROFILE%\AutoInstallerLog.txt
   echo ---------------------------------------------------------------
echo Preparing System...
echo Auto Install Log, generated on: > %USERPROFILE%\AutoInstallerLog.txt
echo AutoInstaller Script Version: %SCRIPTVER% >> %USERPROFILE%\AutoInstallerLog.txt
DATE /T >> %USERPROFILE%\AutoInstallerLog.txt
echo This log file reflects what steps completed in the ASI script. It does not mean they completed correctly so please make sure that all software is installed correctly!
echo Make sure the computer has been renamed to match the type [e.g. COMPUTER_PROTOCOL]. Make sure it  has also been rebooted it so that it takes effect.
echo SystemInfo Output: >> %USERPROFILE%\AutoInstallerLog.txt
systeminfo >> %USERPROFILE%\AutoInstallerLog.txt

::Setting script parameters
echo.
echo Configuring script parameters.

::Check if it's a test machine
echo systeminfo^|findstr /B "Host Name:"^|findstr /C:"TESTCOMP_NAME"
systeminfo|findstr /B "Host Name:"|findstr /C:"TESTCOMP_NAME"
if %ERRORLEVEL% EQU 0 (
echo This computer is either a test machine or has not had it's name configured yet. Continuing will run the script in test mode.
pause
set TestComp=1
::TestOS
   systeminfo|findstr /C:"XP Professional"
   if %ERRORLEVEL% EQU 0 (
      set OS=XP
      goto :finishTestOS
     )

    systeminfo | findstr /C:"7 Enterprise"
    if %ERRORLEVEL% EQU 0 (
      set OS=7
      goto :finishTestOS
     )
     :finishTestOS

set STYLE=workstation
goto :AdminCheck
)
::Debug Statement:
echo NEXT PArT!!! LINE 67
::::OS Version
:getOS
   systeminfo|findstr /C:"XP Professional"
   if %ERRORLEVEL% EQU 0 (
      set OS=XP
      goto :finishOS
     )

    systeminfo|findstr /C:"7 Enterprise"
    if %ERRORLEVEL% EQU 0 (
      set OS=7
      goto :finishOS
     )


    :finishOS
    echo This system is running Windows %OS%.

::Get Form Factor
:getStyle
    systeminfo | findstr /B "Host Name:" | findstr /C:"WORKSTATION_PROTOCOL"
    if %ERRORLEVEL% EQU 0 (
      set STYLE=laptop
     ) else (
      set STYLE=workstation
     )
     :finishStyle
    echo This system is a %STYLE%.

if not defined STYLE (
    echo System style not configured correctly. The name of the computer likely does not follow protocol, it must have PROTOCOLS in it.
    goto :ERROREND
)

if not defined OS (
    echo OS not detected properly. The script may need to be updated to detect new versions of Windows.
    goto :ERROREND
)

【问题讨论】:

  • 我试过在两个管道前后加一个空格,效果很好。即使引号后有 2 个空格字符。
  • 我猜里面有一个看不见的角色。尝试重新输入有问题的行。
  • 我重新输入了它并没有修复它。 (尚未阅读发布的其他答案)。不过,感谢您的意见 :)

标签: windows batch-file pipe


【解决方案1】:

您发布的代码 sn-p 没有任何问题。它在隔离时工作得很好。

我假设您在显示的代码中看到“额外”空格,因为 ECHO 已打开。这些空间是解析器如何处理一行的产物。您不必担心它们(尽管在 ECHO 为 ON 时查看输出是诊断问题的宝贵工具)。

ECHO ON 输出针对许多批处理构造进行了修改。例如,以下完全有效的代码

if "a"=="b" >test.txt echo matched

ECHO 为 ON 时显示如下

if "a" == "b" echo matched 1>test.txt

可以看到已经添加了空格,重定向被移到了末尾,并且重定向前面插入了一个 1(代表 stdout)。这一切都很正常——命令执行得很好。 (当然它什么也不做,因为 IF 评估为 FALSE)如果 IF 条件更改为 TRUE,则匹配后的额外空间将不会包含在输出中。

您不必担心 ECHO ON 输出中出现额外的空格。但是您应该注意源代码中的多余空格。如果它出现在您的原始源代码中,则会在输出中包含一个额外的尾随空格。

我怀疑语法错误问题出在您代码的其他地方。也许您可以提供更多发生错误的代码上下文。

针对问题更新的更新答案

错误并没有发生在您认为的地方!语法错误出现在后面的复杂 IF 语句中。

您尝试在带括号的代码块中使用:finishTestOS 标签。如果括号内的标签后面没有紧跟有效的非标签行,您将始终收到语法错误。 (其实规则可能有点复杂,但这里不重要)

您可以通过简单地在标签下方放置一个 REM 语句来消除语法错误。

:finishTestOS
rem

但是 - 您仍然会遇到重大问题 - 代码不会按照您想要的方式运行。您永远不应尝试 GOTO 括号内的代码块中的标签。有关更多信息,请参阅(Windows batch) Goto within if block behaves very strangely 的已接受答案。

您遇到的另一个问题是您尝试在带括号的代码块中访问%ERRORLEVEL%。该值在解析时扩展,并且一次解析整个块,因此该值将是您进入 IF 块之前存在的 ERRORLEVEL。有很多方法可以解决。

  • 延迟扩展:if !errorlevel!==0 ...
  • if not errorlevel 1 ...(阅读 IF 语句的文档)
  • 使用&& 用于成功时的条件执行和/或|| 用于失败时的条件执行。在你的情况下systeminfo|findstr ... && set OS=...

我怀疑您可能还有其他问题,但我没有时间诊断所有问题。

【讨论】:

  • 另外,@echo 关闭也会失败。我已经评论它试图找出它死在哪里。
  • @Mgamerz - 我已经解决了你的直接问题。请参阅我的更新答案。
  • 我搞定了。我在 TestComp 部分之外使用了一个 if defined TestComp 来绕过实际检测。我使用了错误级别而不是 %errorlevel%。这有点笨拙,但它确实有效。现在我的问题是网络路径和命令提示符,但是一旦我完成它,我会为此打开另一个问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多