If condition issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
redro
Posts: 11
Joined: 12 May 2010 05:24

If condition issue

#1 Post by redro » 13 May 2010 06:50

Hi All,

I have written if condition for my requirement but it is not working please assist me.

FOR /f "tokens=4*" %%a in (pcheck.txt) do
( set var = %%a)
IF %var% == "LISTENING"
echo Ecm is still running
else echo "Sending alarm"

I am getting following error:
The syntax of the command is incorrect.

thanks,
JP

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: If condition issue

#2 Post by jeb » 13 May 2010 08:00

hi redro,

redro wrote:FOR /f "tokens=4*" %%a in (pcheck.txt) do
( set var = %%a)
IF %var% == "LISTENING"
echo Ecm is still running
else echo "Sending alarm"


This can't work, because a block has to begin at the same line (look at if /?)
The second problem is your compare %var%=="LISTENING", batch isn't a "normal" language with strings, variables and so.
It only expands something. So you will get(in the best case).

Code: Select all

if Listening == "LISTENING"


You can try this

Code: Select all

setlocal
set var=empty
FOR /f "tokens=4*" %%a in (pcheck.txt) do (
    set var = %%a
)

IF "%var%"=="LISTENING" (
    echo Ecm is still running
) else (
    echo "Sending alarm"
)


jeb

redro
Posts: 11
Joined: 12 May 2010 05:24

Re: If condition issue

#3 Post by redro » 13 May 2010 08:07

hi jeb,

i have modified my code with your suggestions but i am getting else condition. In my case server is running fine.

so we have to get server is running.

Please assist me.

Thanks,
JP

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: If condition issue

#4 Post by jeb » 13 May 2010 08:42

hi redo,

try to use some debugging code, to see what you read.

Code: Select all

setlocal
set var=empty
FOR /f "tokens=4*" %%a in (pcheck.txt) do (
    set var = %%a
  echo read "%%a"
)

echo Comparing "%var%" with "LISTENING"
IF "%var%"=="LISTENING" (
    echo Ecm is still running
) else (
    echo "Sending alarm"
)


hope it helps
jeb

redro
Posts: 11
Joined: 12 May 2010 05:24

Re: If condition issue

#5 Post by redro » 13 May 2010 09:36

Hi Jeb,

It is taking only empty value and passing condition two.

FYI,...
pcheck.txt consists of the following data, from that i am getting LISTENING word. If LISTENING is exists then the server is running else down then i have to soot a mail.

TCP 0.0.0.0:9095 0.0.0.0:0 LISTENING

Please assist me.
Thanks,
JP

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: If condition issue

#6 Post by jeb » 13 May 2010 09:56

and the next round begin :)

Code: Select all

set var=%%
and
set var = %%a

are different things, (first the name of the variable is "var" in the second case it is "var " (with one space).

so your code should look like

Code: Select all

@echo off
setlocal
set var=empty
FOR /f "tokens=4*" %%a in (pcheck.txt) do (
        set var=%%a
   echo read "%%a"
)

echo Comparing "%var%" with "LISTENING"
IF "%var%"=="LISTENING" (
    echo Ecm is still running
) else (
    echo "Sending alarm"
)

redro
Posts: 11
Joined: 12 May 2010 05:24

Re: If condition issue

#7 Post by redro » 13 May 2010 10:08

Hi Jeb

Sounds good , it is working.

Could you please assist me in "sending mail please i have to add the bellow code
@echo off & setlocal
:: set the temp file location

set tempmail=%temp%\tempmail.%random%.txt
:: echo the basic headers to the temp file
echo To: "Scripting Test" ^<praveen.pullela@sirvisetti.com^>, ^<prakash.redrouthu@sirvisetti.com^> > %tempmail%
echo From: "Me" ^<support@sirvisetti.com^> >> %tempmail%
echo Subject: CVG ESB Process DOWN >> %tempmail%

:: echo the blank line that separates the header from the body text

echo.>>%tempmail%

:: echo the body text to the temp file

echo ESB DOWN ON 9099 port PLS check>> %tempmail%
echo GOOD DAY.>> %tempmail%

:: move the temp file to the mail pickup directory
:: adjust this location for your system

move %tempmail% c:\inetpub\mailroot\pickup
set tempmail=
endlocal

Please suggest me how to handle this.

Thanks,
JP

redro
Posts: 11
Joined: 12 May 2010 05:24

Re: If condition issue

#8 Post by redro » 14 May 2010 06:48

Hi Jeb,

Now i am able to send mail if process is not running. how i have to make it as automate this script for when ever the server down then this script should be through the mail. and right now i am checking for one port with the command as
@ netstat -an |find /i "9095" >pcheck.txt

but actually i have to check 3 processes are running are not. Please don't hesitate me to assist.

Thanks,
JP

Post Reply