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
Related
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
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.
I installed linphone application and i am writing a bat file doing a loop through file to make calls through executing this command
echo call %%H%%Z#%%G^|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
Now the loop is working fine and everything is ok but the problem that linphone is not terminating after making the first command to move on for the remaining of the loop , when i try to use linphone directly thorough the cmd command it remain open until i use quit command like this
C:\Users\administrator>cd C:\Program Files (x86)\Linphone\bin
C:\Program Files (x86)\Linphone\bin>linphonec -s 111#1.1.1.1
WARNING: no real random source present!
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec> Establishing call id to sip:111#1.1.1.1, assigned id 1
Contacting sip:111#1.1.1.1
linphonec> Call 1 to sip:111#1.1.1.1 in progress.
linphonec> quit
Terminating...
Call ended
linphonec> Call 1 with sip:111#1.1.1.1 ended (No error).
No response.
linphonec>
C:\Program Files (x86)\Linphone\bin>
how i can terminate the program in my command above after executing the call command ?
I have not linphone to test, but if the program accepts piped commands, maybe, this could work
(echo call %%H%%Z#%%G&echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
edited to make it wait
(
echo call %%H%%Z#%%G
ping -n 11 localhost >nul 2>nul
echo quit
) | "C:\Program Files (x86)\Linphone\bin\linphonec.exe"
or
(echo call %%H%%Z#%%G& ping -n 11 localhost >nul 2>nul & echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
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%
)
I have an application that only works properly when called from a windows command prompt. Something to do with the input/output streams.
So I can call it from a bash script by passing it as an argument to cmd.
cmd /c "badapp"
This works fine - but occasionally badapp fails with network problems - and I get no feedback. Is there anyway to check the ERRORLEVEl from the bash script - or see the output from badapp on the terminal running the bash script?
Yes, $? is the variable that contains the error level.
Try echo $? for example.
An example from Cygwin bash (I'm guessing you are using Cygwin because you are using the Windows cmd in your example.)
susam#nifty /cygdrive/c/Documents and Settings/susam/Desktop
$ cmd /c "badapp"
'badapp' is not recognized as an internal or external command,
operable program or batch file.
susam#nifty/cygdrive/c/Documents and Settings/susam/Desktop
$ if [ $? -eq 0 ]
> then
> echo "good"
> else
> echo "bad"
> fi
bad