Windows批处理文件eq此时是意外的 [英] windows batch file eq was unexpected at this time

查看:150
本文介绍了Windows批处理文件eq此时是意外的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Windows批处理脚本,该脚本将安装服务.首先,我需要查找服务是否已经存在.如果服务存在,则必须检查状态.如果状态正在运行,则必须停止并删除该服务.

I am writing a windows batch script that will Install a service. First, I need to find if the service already exists. If the service exists, it has to check the state. If the state is running, it has to stop and delete the service.

这是我的代码:test.bat.我是从命令行运行的.

This is my code : test.bat. I am running this from command line.

for /F "tokens=3 delims=: " %%H in ('sc query "IBMLibertyProfile" ^| findstr "STATE" ') do (
  if /I "%%H" EQ "RUNNING" (
   sc stop "IBMLibertyProfile"
  )
)

我遇到错误:

C:> test1.bat这次的EQ意外.

C:>test1.bat EQ was unexpected at this time.

C:>如果/I%H" EQ"RUNNING"(

C:> if /I "%H" EQ "RUNNING" (

如何解决此错误?

推荐答案

尝试一下:

@rem If the service doesn't exist, exit.
@sc query IBMLibertyProfile > NUL 2>&1
@if %ERRORLEVEL% neq 0 @exit /b 0

@rem If the service is already stopped, delete it.
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :DeleteService

@rem No matter it's state, tell it to stop.
@sc stop IBMLibertyProfile

@rem Wait for it to stop.
@set _WaitLoopCount=0
:StoppedWait
@if _WaitLoopCount equ 10 @goto :ServiceWaitTimeout
@timeout /t 3 > NUL 2>&1
@sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
@if %ERRORLEVEL% neq 0 @goto :StoppedWait

@rem Delete the service and exit.
:DeleteService
@sc delete IBMLibertyProfile
@exit /b 0

:ServiceWaitTimeout
@echo Service failed to reach the STOPPED state. Reboot the computer and try again.

注意:如果您的服务表现不佳,脚本可能会挂起.我将留给您确定如何处理sc删除失败.

NOTE: If your service is not well behaved, the script might hang. I'll leave it to you to work out how to deal with sc delete failures.

这篇关于Windows批处理文件eq此时是意外的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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