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
how to check the command executed in a for statement is fail
Moderator: DosItHelp
Re: how to check the command executed in a for statement is
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
)
Re: how to check the command executed in a for statement is
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
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
Re: how to check the command executed in a for statement is
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?
Re: how to check the command executed in a for statement is
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
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
Re: how to check the command executed in a for statement is
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
Re: how to check the command executed in a for statement is
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.