how to check the command executed in a for statement is fail

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

how to check the command executed in a for statement is fail

#1 Post by sambasiva » 29 Apr 2014 11:51

Hi Experts,

Please help me with below requirement.

1) I have to execute the below command and assign its output completely to a variable
jps -v |findstr "domain.home"

2)Then I have to verify whether this command is executed successfully or not and take necessary action based on the execution status.


For Example: in shell script I have the code as below for the same requirement

runjps () {
jps -v | grep "domain.home"
}
local result
result=$(runjps) --->execute the command and assign the output to a variable
if [ $? == 0 ] ; then
for jpsItem in $result ; do
printf 'PROCESS-JPS: %s\n' $jpsItem
done
else
print ' jps command failed'
fi


Thanks in advance
SS

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to check the command executed in a for statement is

#2 Post by Squashman » 29 Apr 2014 11:57

Code: Select all

FOR /F "delims=" %%G in ('jps -v ^| grep "domain.home"') do set result=%%G
IF DEFINED result (
     Execute code here
) else (
echo jps command failed
)

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: how to check the command executed in a for statement is

#3 Post by sambasiva » 29 Apr 2014 22:30

basically the output of "jps -v ^| grep "domain.home" might be multiple lines.
In that case at the end of the for statement it contains only the last line right ?


instead can we check the errorlevel at the end of for loop here ?


-SS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to check the command executed in a for statement is

#4 Post by foxidrive » 29 Apr 2014 22:34

sambasiva wrote:basically the output of "jps -v ^| grep "domain.home" might be multiple lines.
In that case at the end of the for statement it contains only the last line right ?


instead can we check the errorlevel at the end of for loop here ?


You will get the errorlevel of grep and not jps.

In what way do you expect it to fail?

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: how to check the command executed in a for statement is

#5 Post by sambasiva » 30 Apr 2014 02:57

For example jps command will not be success if JAVA_HOME is set in the path.
It fails as the java\bin directory is not found.

In that case I wanted to check the execution status of jps and take action based on that.


Thanks
SS

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how to check the command executed in a for statement is

#6 Post by foxidrive » 30 Apr 2014 03:05

sambasiva wrote:For example jps command will not be success if JAVA_HOME is set in the path.
It fails as the java\bin directory is not found.

In that case I wanted to check the execution status of jps and take action based on that.


This might work for you:

Code: Select all

jps /? >nul 2>&1
if errorlevel 9009 echo jps is not available & goto :EOF

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how to check the command executed in a for statement is

#7 Post by Squashman » 30 Apr 2014 06:22

Well I know BASH scripting and BATCH and I pretty much coded the Batch the way the BASH script would work. So you get what you ask for when you don't explain your problem with enough information up front.

Post Reply