I simplified the code. The following three are work.
for /L %a in (1,1,9) do @(if %a NEQ 0 (echo %a))
&
for /L %a in (1,1,9) do @(if not %a == 0 (echo %a))
&
(for /L %a in (1,1,9) do @(if not %a == 0 (echo %a)))|sort /R
But the next one didn't work,
(for /L %a in (1,1,9) do @(if %a NEQ 0 (echo %a)))|sort /R
What's the problem of NEQ
in the piped block command?
more simplified,
This works, (if 3 == 3 echo yes)|sort
This doesn't work, (if 3 NEQ 2 echo yes)|sort
Part of my code.
@echo off
setlocal enabledelayedexpansion
set Unx_path=.....\bin\UnxUtils\
(
for /F %%a in ('^""%Unx_path%pclip.exe"^|"%Unx_path%sed.exe" -r "s/^^$/none/"^|"%Unx_path%sed.exe" -rf "script_file"^"') do @(
if not "%%a" == "none" (
"%Unx_path%grep.exe" -iEe "%%a" "file4search"|"%Unx_path%sed.exe" -r "s/^^[^,]+$/,&/";"s/^^([^.]+)[.][^.]+$/\1/"|"%Unx_path%gawk.exe" "BEGIN{FS=\",\";ORS=\" \"}{print $2}"|"%Unx_path%sed.exe" "s/%%a//I g";"s/.*/%%a &|/";"s/ -/ /g";"s/ |/\n/g";"s/ /\t/g";"s/~/\t/g"
) else (
echo none
)
)
)|"%Unx_path%gclip.exe"
exit /b
Base on the idea & method from npocmaka & jeb.
Another way was found to solve the error of
IF
command in piped code.TO ADD AN ESCAPE SPACE AT RIGHT POSITION
For
EQU
NEQ
LSS
LEQ
GTR
orGEQ
,ONE ^ has to be added after 1st compared
string/number
,At least TWO SPACE have to be added between this ^ and
EQU
NEQ
LSS
LEQ
GTR
orGEQ
.Example,
true
can be replaced by%^cmdcmdline%
to see the parser.For
IF DEFINED
&IF EXIST
IF DEFINED
orIF EXIST
,variable
orfile/folder
,Example,
true
can be replaced by%^cmdcmdline%
to see the parserBut so far I didn't know why all of them have to work like above.
Something interesting,
All = are ignored.
Updated (2016-04-23),
As npcmaka mentioned,
=
,,
are also delimiters.CASE 1
Outputs,
CASE 2
Outputs,
One space appears between
===
.CASE 3
Outputs,
Some phenomenon can be found, which may be known how to parse...
neq
is identified as variable in all CASE. (doset neq=blah
before will occur no output for all CASE)In CASE 2,
==
in Delimiter===
seems be identified as comparison and auto be added one space afterAccording to the rule in CASE 2, in CASE 3, input
neq
is lowercase and outputNEQ
is uppercase. But in CASE 1 & 2, due to existed==
beforeneq
, this time,neq
is kept in lowercase, which is not identified as comparison.So it has several steps in parser period. But has some bugs of adding or deleting delimiters. Right?
CASE 4
The next code seems trigger
cmd
executing infinitely,it runs like
if not "defined" == "echo" recursive command
try this:
or from batch file (double expansion works differently from batch file and the console):
The "mystery" was solved by jeb here and here . Though you are facing the issue before the pipe it is the same bug because the cmd creates two threads on each side of the pipe.
Here's how the for loop can be made to work:
For testing purposes the cmdcmdline variable can be used.
But this fails, when somewhere in the expression is a syntax error, as the complete code block will be dropped.
This results into
"2" can't be syntactically evaluated here
(From german"2" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
)So exactly in the case of a syntax error, you can't see the cause!
I build a small debug variable for this case
When you add the
%debug%
you will see the cmdcmdline, but the code will not be executed, so you can examine how different modification take effect.Like
but
The trick is to add
^) %%\n%%
, this closes the debug expression code block with)
and cancels further parsing of the remaing code by the linefeed%%\n%%
.I think the easiest work-around to get
if
statements working within pipes is the following (the outer surrounding pair of parentheses is mandatory):I tested it with all keywords like
not
,exist
anddefined
,else
clauses, all possible comparison operators (==
,EQU
,NEQ
, etc.), for either side of the pipe, and incmd
and batch-files.Also I also successfully tested the
if
statements embedded within the body of afor
loop (in which case the outer parentheses are no longer required).