Having a batch file check its own output? - maven

I'm wondering if it's possible to have a batch file check itself for a string.
I'm using a batch file to run Maven commands,and I want to check if anything failed by searching for a "FAILURE" string at the end of the script
I know you can FIND in other files, but can you have it check the current output on itself, or is the best solution to save the batch file output as a text and then search it?
As an example, if I had a batch file echo Hello World it would print Hello World, then I would want to search the output for Hello and tell me it found the string Hello.

I like vane's idea to take action upon the return code provided by mvn, but instead of using ERRORLEVEL, I like to use the || operator. The commands after || are only excecuted if the prior command failed.
::initialize error flag to undefined
set "mvnErr="
::run your Maven command and define the error flag if there was an error
call mvn {your arguments here} || set mvnErr=1
::You can now take action if there was an error
if defined mvnErr echo there was a Maven error

You can do this by checking the errorlevel after each Maven command. For example
#ECHO OFF
set hasErrors=0
REM execute maven command here
if not errorlevel 0 set hasErrors=1
REM more batch command and similar checking ...
if %hasErrors%==1 (
echo print your error info or do whatever
)

Building off of both the 2 previous answers, I think this gives the best of both worlds. The || syntax is short and easy to read and the IF statement before every command ensure processing only progresses when previous operations were successful.
set hasErrors=0
IF %hasErrors%==0 call mvn -f ./mycoservices/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycostatic/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycoweb/pom.xml install || set hasErrors=1
IF %hasErrors%==0 call mvn -f ./mycowebbundle/pom.xml install || set hasErrors=1

why not just exit the batch whenever it fail
if %ERRORLEVEL% NEQ 0 (
echo exit /b %errorlevel%
exit /b %errorlevel%
)

Related

How to return exit status of Matlab to Jenkins console output?

So far,I have tried using Jenkins to trigger matlab and do some test.But I want to let Jenkins know that if we have some error in matlab test,and communicate with Jenkins.So the console output will be failure.
Matlab version:2016b
So I put exit(1) in the last line of matlab script.But Jenkins console output still reveal SUCCESS.And I use %errorlevel% in batch command line,the result was 0 not what I expect.
I have tried use Start /wait in the beginning.Because I use Windows 10.But isn't help.
Below is my Jenkins batch command:
start /wait matlab.exe -wait -r -sd "D:\matlab" MyScript;exit -logfile OutputPrint.txt echo ExitCode is %errorlevel%
I expect that if some test cases in test manager didn't pass.It could inform Jenkins and the console output will be failure.
Thanks in advance for any help!
It seems that official support for providing an exit or error status from matlab was introduced in R2019a. I say this, because I see it in the online reference in R2019a, but it is missing in R2018b:
https://www.mathworks.com/help/releases/R2019a/matlab/ref/quit.html
vs
https://www.mathworks.com/help/releases/R2018b/matlab/ref/quit.html
However when I try it and do not give the 'force' input, it does seem to work for older versions of matlab, e.g. for R2016b:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
echo %ERRORLEVEL%
This gives me 10, which is what I expected. In your code, I see a couple of things that may give issues. I recommend to start with something simple as in my code and then add additional components if necessary and verify the errorlevel is still ok.
I see you're using start, this may block the exit code from reaching Jenkins
I see
-r -sd "D:\matlab" MyScript;exit
since the command to execute should come directly after -r, I think you meant:
-sd "D:\matlab" -r MyScript;exit
Finally because of the command "echo" after the matlab command, the errorlevel will be from the echo command. So if you want Jenkins to get the errorlevel from the MATLAB command, but you do want to run something after matlab, you should probably capture it in a separate variable and "reset" the errorlevel just before exiting the script, e.g.:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
SET MATLABERROR=%ERRORLEVEL%
echo ExitCode is %ERRORLEVEL%
SET ERRORLEVEL=%MATLABERROR%

Executing windows batch command in Jenkins keeps failing with Exit-1 state

I am testing out "Jenkins ver. 2.89.4" in Windows10 Machine and I have configured a simple job to test out few things. Under Build section in Jenkins, I have used "Execute Windows Batch Command" and used the following two commands. Both the below commands executes fine in the command prompt but however the Jenkins Build job keeps getting failed with Exit 1 state.
date
echo "SampleBuild job completed successfully."
Couldn't able to get the reason for the failure. And the following is what we see in console output.
Started by user Administrator
Building in workspace C:\ProgramData\Jenkins\workspace\SampleBuildJob
[SampleBuildJob] $ cmd /c call C:\WINDOWS\TEMP\jenkins6658106255890809140.bat
C:\ProgramData\Jenkins\workspace\SampleBuildJob>date
The current date is: Fri 02/23/2018
Enter the new date: (mm-dd-yy)
C:\ProgramData\Jenkins\workspace\SampleBuildJob>echo "SampleBuild job completed successfully."
"SampleBuild job completed successfully."
C:\ProgramData\Jenkins\workspace\SampleBuildJob>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Can anyone tell me, what am I missing?
Try to add (call ) to end of your Batch Command.
This will clear the errorlevel environment variable, there for force the build result to be successful.
If you want to check a specific command's result to determine the build result. Let's say you want to check dates command.
(dates) || (exit %errorlevel% )
This will fail the build if error happens in the first command.
or
(dates) && (echo "command executed succesfully!")
This will show the message only when the first command successfully executed.
Then with the changed command, you do not need (call ) any more.
This windows batch script will work:
echo %DATE%
echo %Time%
echo "command executed succesfully!"
The windows batch script show the same error to me.
Finally i have added exit 0 at the end it worked

Passing parameter from window bat to UNIX shell script

i know topic seems very familiar and there are many answers given for same, but my issue is something else.I have a .bat file in which i am passing more then 10 parameters(I know limit is 9 but i need it).It seems to be working fine.Script is below-:
#ECHO OFF
setlocal EnableDelayedExpansion
set n=0
for %%a in (%*) do (
set vector[!n!]=%%a
set /A n+=1
)
SET sadminUser=%vector[0]%
SET sadminPassword=%vector[1]%
SET dsnProvider=%vector[2]%
SET dbUserName=%vector[3]%
SET schemaFilePath=%vector[4]%
SET ddldictschemalogPath=%vector[5]%
SET repositoryName=%vector[6]%
SET ddlimpschemalogPath=%vector[7]%
SET grantUserRole=%vector[8]%
SET userId=anyuser
SET host=myhost
SET password=password
SET datatable=%vector[9]%
SET indexTable=%vector[10]%
SET ddlimpschemalogPath2=%vector[11]%
echo %ddlimpschemalogPath2%
echo y | "C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% exit
"C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8sw/DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
Now as you see i am doing echo %ddlimpschemalogPath2% which is last parameter in array,and i can see correct output as well.
Problem is when i try to pass these parameters into UNIX Shell Script.You can see i am doing in bat file using putty command line Plink.I can successfully connect to shell script as well,And i am trying to echo all the passed parameters in shell script.But facing some issue . Script is below-:
#!/bin/bash
sadminUser=$1
sadminPassword=$2
dsnProvider=$3
dbUserName=$4
schemaFilePath=$5
ddldictschemalogPath=$6
repositoryName=$7
ddlimpschemalogPath=$8
grantUserRole=$9
datatable=${10}
indexTable=${11}
ddlimpschemalogPath2=${12}
echo "$sadmimUser"
echo "$sadminPassword"
echo "$ddlimpschemalogPath2"
echo password | sudo -S -l
sudo host << EOF
// Do something else
EOF
I Found one issue. In .bat i have parameter called -: repositoryName . When i do echo %repositoryName% it gives "Siebel Repository" correct output. Now when this spaced value is passed as parameter to shell script it breaks into 2 different values so:
These two parameter in script take 2 different values-:
repositoryName=$7
ddlimpschemalogPath=$8
Output-:
Siebel
repository
And it should be one value for parameter repositoryName=$7 . Giving value Siebel Repository. Why is this happening? Can this me issue why value are up and down?
As you can see in .bat i am making call to shell script, and passing parameters taken from .bat file as below-:
DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
You can see complete line above in .bat.
Invoking .bat file -:
dlSync.bat "SAD" "glob81" "gepf_DSN" "SIEBEL" "/global/u70/globepfdev/siebel/schema.ddl" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/expschem.log" "Siebel Repository" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync1.log" "SSE_ROLE" "GLOB_DATA_SMALL" "GLOB_INDEX_SMALL" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync2.log"
Same parameter are passing out to shell script . where "Siebel Repository" breaks into 2. Thanks

Error codes seemingly ignored by '&&' on Windows command prompt

I have some Batch scripts I use for automating application build processes, most of which involve chaining commands together using the && operator. Admittedly, I'm more experienced with Linux, but based on that experience some_command && other_command should result in other_command being run iff some_command returns an exit code of 0. This answer and this answer seem to agree with that. However this appears not to be the case on Windows cmd.exe, all of the scripts run regardless of the error code of the previous.
I decided to make a simple test for this to convince myself I wasn't going insane. Consider this test.bat, which returns an exit code of 1:
#echo off
EXIT /B 1
Running test.bat && echo This shouldn't print prints 'This shouldn't print'. But since the exit code is clearly 1, echo should not be called. I've tested that the error code was actually 1 using the %errorlevel% variable, they're coming out as expected (0 before I run the script, 1 after).
On Linux I tried the same thing. Here's test.sh:
#!/bin/bash
exit 1
Running ./test.sh && echo "This shouldn't print" gives no output, exactly what I expected.
What's going on here?
(Note: OS is Windows 7 Enterprise)
You need to use call to run the batch script, like this:
call test.bat && echo This shouldn't print
Without call, the && operator does not receive the ErrorLevel returned by the batch script.
When you run a batch file from within another one, you need to use call in order to return to the calling batch file; without call, execution terminates as soon as the called batch file has finished...:
call test.bat
echo This is going to be displayed.
...but:
test.bat
echo You will never see this!
When running test.bat is involved in a command line where multiple commands are combined (using the concatenation operator &, the conditional ones && and ||, or even a block of code within parentheses ()), all the commands following test.bat are ecexuted even if call was not used. This is because the entire command line/block has already been parsed by the command interpreter.
However, when call is used, the ErrorLevel value returned by the batch file is received (which is 1 in our situation) and the following commands behave accordingly:
call test.bat & echo This is always printed.
echo And this is also always printed.
call test.bat && echo This is not printed.
call test.bat || echo But this is printed.
(
call test.bat
echo This is printed too.
echo And again this also.
)
call test.bat & if ErrorLevel 1 echo This is printed.
But without call you will get this...:
test.bat & echo This is printed.
echo But this is not!
...and...:
test.bat && echo Even this is printed!
echo Neither is this!
...and...:
test.bat || echo But this is not printed!
echo And this is not either!
...and:
(
call test.bat
echo This is printed.
echo And this as well.
)
It seems that the && and || operators receive an ErrorLevel of 0 -- even in case ErrorLevel has already been set before test.bat is executed, strangely. Also when if ErrorLevel is used, the behaviour is similar:
test.bat & if ErrorLevel 1 echo This is not printed!
...and...:
set = & rem This constitutes a syntax error.
test.bat & if ErrorLevel 1 echo This is still not printed!
Note that the commands behind test.bat execute after the batch script, even without call.

capturing error message from echo in cmd prompt

I'm writting out some text to a text file within a cmd batch script like such:
echo FlagValue=Y>>flag.txt
This normally works fine but occassionally if the text file is open by a different process, an error messgae is returned saying Access Denied. What I'd like to do is stop the batch file if an error occurs with something like:
if return_code GEQ 1 GOTO ERR
But can't find a return code from echo command. Does one exist, or is there a better tactic to use to capture error message?
echo FlagValue=Y>>flag.txt || echo access_denied Ensure you have rights
or
echo FlagValue=Y>>flag.txt
if /i %errorlevel% NEQ 0 do (
echo access_denied Ensure you have rights
call sendmail.cmd
)
Sample:
C:\Users\Me\Desktop>echo Hello > MyFile.txt || echo ERROR
Access is denied.
ERROR
C:\Users\Me\Desktop>echo Hello > a.txt || echo ERROR
C:\Users\Me\Desktop>
Everytime you run a command the ERRORLEVEL environment variable is set to your command's return. So try echo %ERRORLEVEL% straight after you run your command. (Be careful as any command you run inbetween (including echo) will override the %ERRORLEVEL%.
Also, check these out for more information:
Can a batch file capture the exit codes of the commands it is invoking?
Batch Files - Error Handling

Resources