ELSE was unexpected at this time - if-statement

I frequently work on the Windows command line within ConEmu and occasionally, after working in a given window for some time, get the following unexpected error:
ELSE was unexpected at this time.
Currently, it occurred after pasting about 150 lines of rm commands (may be unrelated).
Once I get that error, I cannot use the ELSE statement in that window; I must start a new shell process, at which point my scripts work as expected. For example, once that error occurs in a window, I observe the following:
C:\> IF DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
ELSE was unexpected at this time.
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
ELSE was unexpected at this time.
C:\> IF DEFINED AN_ENV_VAR (ECHO YES)
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES)
YES
A new shell gives expected results:
C:\> IF DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
NO
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES) ELSE ECHO NO
YES
C:\> IF DEFINED AN_ENV_VAR (ECHO YES)
C:\> IF NOT DEFINED AN_ENV_VAR (ECHO YES)
YES
Is there any way to fix the current shell?

One of the environment variables should contain a miss adding else statement.
Please check the expanded values of the DEFINED AN_ENV_VAR.
One way to reproduce the error is
set var=123 else echo abc
if %var%==123 echo ok
When the var expanded. There will be a additional else statement. Then the error is raised.
ELSE was unexpected at this time.

Please check your path doesn't have space in any folder other wise it will through error "ELSE was unexpected at this time." for batch file running same we faced while one of our project

Related

What is the purpose of echo command after executing a program?

Why do we exit issuing an echo command while executing a program?
As far as I know, the echo command for console windows is:
$ echo $
And for windows is:
$ echo %ERRORLEVEL%
When the child process ends, the set variable ERRORLEVEL contains an integer returned by it. ERRORLEVEL is measured 0 if the process was successful (i.e. return 0). OTOH, 1 or greater if the process encountered an error.
However, I don't know what do you exactly mean by:
echo command after executing a program
I never ran this command neither I've seen anywhere.

bash script to compile and run test cases

Completely new to bash scripts, trying to make a script to compile and run all cpp files that start with "blah". What I have so far is
#1/bin/bash
for i in blah*.cpp
do
if g++ "$i" -o "${i%.cpp}.out"; then
/Users/[directory]/Desktop/"${i%.cpp}.out" #to run each testcase
else
echo "failed"
fi
done
I need to know how many blah files succeeded in running (I can either make the c++ programs return 1 or just cout "succeeded", any way to tell would be fine), and I'm having trouble figuring out how to get the return status of each program or how to use the output from each program run and see if it says "succeeded".
Have your test case return 1 for success and 0 for failure. The return value can then be used as follows:
counter=0
for i in blah*.cpp
do
if g++ "$i" -o "${i%.cpp}.out"; then
if /Users/[directory]/Desktop/"${i%.cpp}.out" ; then
counter=$((counter+1))
else
echo "failed to run"
fi
else
echo "failed to build"
fi
done
echo "$counter tests succeeded"

Informatica Command Task

I asked this question on the Informatica forums, but no one knew the answer, so now I'm coming here for some help.
To my understanding, anything in a "post-session success command" field should be passed to the appropriate command interface and executed. However, when I use an IF statement, it fails. Any ideas?
"IF 1==1 echo.bob >> f:\filename.txt"
This works when I type it manually into the terminal (DOS in this case). But when I throw it into a reusable command task, I get this:
ERROR
POST-SESS
CMN_1949
Error: [Pre/Post Session Command] Process id 2996. The shell command failed with exit code 1.
PS: Using 9.5.1 Hotfix 1
I think the problem is not in how Informatica executes commands. The problem lies in how DOS return error codes, and specifically that some commands, like IF and ECHO does'nt. (The return code Informatica picks up from DOS can be seen with echo %ERRORLEVEL% in DOS, and I'll use the name DOS here for convenience even though under Windows now this is'nt strictly correct)
Run these commands in succession:
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 0
cd c:\
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\filename.txt
echo %ERRORLEVEL%
REM "echo" does not set ERRORLEVEL => ERRORLEVEL is left unchanged
echo.bob >> c:\thisdirdontexist\filename.txt
echo %ERRORLEVEL%
The first CD set a return code, in this case to 0.
The following ECHO (with or without the IF test) does not change the return code, thus it remains 0 even though the last ECHO fails.
If the first CD command would have returned an error;
#echo off
REM "cd" sets ERRORLEVEL => ERRORLEVEL is set to 1
cd xxxxxx
echo %ERRORLEVEL%
then all the subsequent ECHO would return 1 and Informatica would fail them both.
This said it is still strange since each post-session success command in Informatica is executed under its own cmd-shell, so the initial ERRORLEVEL for every command should allways be 0. I can't explain that and unfortunately I can't actually test this in Informatica as we run under UNIX, but I'm pretty sure this is at least part of the problem.
To get around the problem you should make sure that you set the "Fail task if any command fails" option on the property-tab. This makes Informatica use the cmd/c option and since this set a proper return code Informatica should be able to pick up the error (or success) correctly. If this still doesn't work properly try change the command yourself to:
cmd /c "IF 1==1 echo.uncle >> c:\filename.txt"

Get current VirtualBox Status with Vagrant

We did alot of stuff with Vagrant in the recent days, and there's a problem we encountered / didn't find any answer to. It's about checking the Status of the VirtualBox with Vagrant, for example:
if config.vmbox_status == 'running'
# do this stuff
end
If there is any easy way of checking / getting the current Virtual Box status let us know.
If you want to check running status with shell script. Try this code inside your project folder
vagrant status --machine-readable | grep state,running
Then check output string or exit code
You can use egrep to get current status (other answers check for certain statuses while this approach allows you to use case or if in scrips, like that (ugly and not-bullet proof!):
VM_STATUS=$(vagrant status --machine-readable | grep ",state," | egrep -o '([a-z_]*)$')
case "${VM_STATUS}" in
running)
echo "RUNNING"
;;
poweroff)
echo "POWEROFF"
;;
*)
echo "Unhandled: ${VM_STATUS}"
;;
esac
Or with the use of awk to get the VM's current state:
VM_STATUS=`vagrant status --machine-readable | grep ",state," | awk -F, '{print $4}'`
case "${VM_STATUS}" in
running)
echo "RUNNING"
;;
poweroff)
echo "POWEROFF"
;;
*)
echo "Unhandled: ${VM_STATUS}"
;;
esac
I am not sure if you want to do this within Vagrantfile necessarily but outside of it you can fire command vagrant status BOX_NAME and get the status of box. You will get text in following format:
Current VM states:
box2 running
change directory to location of VM: cd d:/vagant_boxes
run: vagrant status

Windows Batch Commands - Advanced Error Suppression Issue

I'm programming a very large purpose-driven Windows command prompt batch program.
The problem is that I can't suppress the error's text. I could "cls" but that means that almost every command will cls the prompt and I don't want to force that on users. I also tried using >nul after it, and 2>nul in front of the command. The problem with 2>nul CMDOW.EXE /RUN is that then it doesn't store the error in the ERRORLEVEL environment variable.... I also can't have this error info showing up almost every time they use a command in the prompt.
My console does tons of things, including quick navigation and web-page/program/folder access. Recently I have been trying to implement a basic wrapper around the central batch program so that if you type something that is an unrecognized command, it will first check to see if the text string you input is the beginning of a folder's name within your current directory. If so, it will auto-move you into the folder. If not, it will display the usual error message.
I made it so that the input is no longer standard dos input, but a set /p command with a prompt that imitates the usual interface. I got it so it doesn't wait when typing a program name when not preceded by "start" if its in a PATH using CMDOW.
Everything functions now except I have a small issue that's a large visual nuisance. I prompt the user and store the input to an environment variable, then use CMDOW.exe /RUN to first attempt to execute the input text and see if it's an executable file (this covers paths, as well as .exes in the current directory). I then check ERRORLEVEL to see if this resulted in an error. If so, I move on to the next method.
I've also tried writing a little C++ program to execute for me, it works easily with winexec but idk how to obtain error code to confirm the process started sucessfully. If I can do that, then I can just send that result to an environment variable. CreateProcess() doesn't work without being absolute with the location. I can't just plug in the input text and have it work but it pauses until termination anyway I believe. ShellExecute() works but doesn't seem as simple as plugging it in from input... though it appears to have a ready method of obtaining the output. I might add I'm not great at C++, I learn what I need to to get by.
I'm not sure how to get around this issue. I'm also not sure if there exists some special method to bypass that error output while still gaining the knowledge that it didn't execute properly.
Here is my code:
#SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
#for /L %%i in (0,0,0) do #(
set zinput=
set /p zinput=^%time%[%cd:~0,1%]^>
call :EXECUTE
set zinput=
)
:EXECUTE
IF ("%zinput%")==("") GOTO :EOF
cmdow /run "%zinput%"
set ERRCODE=%ERRORLEVEL%
IF ("%ERRCODE%")==("1") call :UNDEFINED
GOTO :EOF
:UNDEFINED
%zinput%
set ERRCODE=%ERRORLEVEL%
IF %ERRCODE%==9009 GOTO ZDIR
GOTO :EOF
:ZDIR
set zDIR=
DIR /B /AD-H|sed -n "/^%zinput%/"Ip>"%aicnspath%\etc\dump.txt"
set /p zDIR=<"%aicnspath%\etc\dump.txt"
IF NOT DEFINED zDIR GOTO UNDEFINED2
cd %zDIR%
call "%aicnspath%\etc\update.bat"
cls
echo AUTO-MOVED INTO: %CD%|tr '[a-z]' '[A-Z]'|sed "s/^/%_hc%%_bc2%/"|sed "s/$/%_bc%/"
echo -----------------------------------------
echo/
GOTO :EOF
:UNDEFINED2
cls
echo/
echo The string %_hc%%_bc2%^'%zinput%^'%_bc% is not recognized by AICNS as any internal or external command, operable program or batch file.
echo/
GOTO :EOF
Try this...
CMDOW /run "%zinput%" >nul 2>&1
You should still have access to the ERRORLEVEL after this.
I don't see any reason because 2>nul CMDOW.EXE /RUN don't return the ErrorLevel value whereas CMDOW.EXE /RUN return it, unless CMDOW.EXE was written this way; this sound very strange to me.
The Batch file below check if a command is an executable file and return these values via ErrorLevel: 0 if not found, 1 if found in current directory, and 2 if found in a directory of PATH variable. I hope you can use a modified version of this Batch file instead of CMDOW.EXE program to solve your problem.
PATHOF.BAT:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM CREATE A LIST OF FILE NAMES ADDING THE EXECUTABLE EXTENSIONS
SET NAMEEXT=!PATHEXT:.=%1.!
REM SEARCHES FILE NAMES IN CURRENT DIRECTORY, IF FOUND: ERRORLEVEL=1
FOR %%N IN (%NAMEEXT%) DO IF EXIST %%N ECHO %%N & EXIT /B 1
REM SEARCHES FILE NAMES IN DIRECTORIES OF PATH VARIABLE, IF FOUND: ERRORLEVEL=2
FOR %%N IN (%NAMEEXT%) DO IF NOT "%%~$PATH:N" == "" ECHO %%~$PATH:N & EXIT /B 2
REM IF FILE NOT FOUND, ERRORLEVEL=0
ECHO '%1' is not an external command or batch file located in PATH & EXIT /B 0
Please, let me know if this method worked or if you got an additional problem using it.