Why does an error message appear when the `IF` comparison is in a channel block command?

I have simplified the code. The following three works.

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 is the problem NEQwith the channel block command?

more simplified

It works, (if 3 == 3 echo yes)|sort

This does not 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
+4
source share
4 answers

try the following:

set "myline=if 3 NEQ 2 echo yes"

( %myLin^e%)|sort

or from a batch file (the double extension works differently than a batch file and a console):

set "myline=if 3 NEQ 2 echo yes"

( %%myLine%%)|sort

The "mystery" was resolved here jeb here and here . Although you run into a problem in front of the pipe, this is one and the same error, because cmd creates two streams on each side of the pipe.

Here's how to make the for loop work:

set "line=if %a NEQ 0"
(for /L %a in (1,1,9) do @( %lin^e% echo %a))|sort /R
+3

cmdcmdline.
, - , .

@echo off
echo dummy | (
echo %%cmdcmdline%
if 1 NEQ 2 echo Work
)

"2" can't be syntactically evaluated here (From german "2" kann syntaktisch an dieser Stelle nicht verarbeitet werden.)

!

@echo off
setlocal
(set \n=^
%=empty=%
)
set "debug=echo ### %%cmdcmdline%% ^) %%\n%%"
echo dummy | (
    %debug%
    if 1 NEQ 2 echo Work
)

%debug%, cmdcmdline, , , .

...
| ( 
  %debug%
  break & if 1 NEQ 2 echo THIS FAILS
)

...
| ( 
  %debug%
  break ^& if 1 NEQ 2 echo This Works !!!
)

, ^) %%\n%%, ) %%\n%%.

+3

npocmaka jeb.

IF .

ESCAPE


EQU NEQ LSS LEQ GTR GEQ,

  • ^ 1- string/number,

  • ^ EQU NEQ LSS LEQ GTR GEQ TWO​​strong > SPACE.

echo pipe|IF 1234^  GTR 124 echo true

true %^cmdcmdline%, .

(IF 1234^  NEQ 124 echo right)|more

IF DEFINED IF EXIST

  • SPACE IF DEFINED IF EXIST,
  • TWO​​strong > SPACE variable file/folder,
  • ^ SPACE.

echo pipe|IF DEFINED ^  username echo true
echo pipe|IF EXIST ^  c:\windows echo true

true %^cmdcmdline%,

(IF DEFINED ^  username echo right)|more
(IF EXIST ^  c:\windows echo right)|more

, , .


- ,

echo pipe|if 123^   equ   ======  123 ======    echo %^cmdcmdline%

= .

(2016-04-23),

npcmaka, =, , .

CASE 1

echo pipe|if not defined^===neq === echo %^cmdcmdline%

,

C:\Windows\system32\cmd.exe  /S /D /c" if not defined=== neq echo %cmdcmdline%"

CASE 2

echo pipe|if not defined^ === neq === echo %^cmdcmdline%

,

C:\Windows\system32\cmd.exe  /S /D /c" if not defined == = neq === echo %cmdcmdline%"

=== .

CASE 3

echo pipe|if not defined^,,,neq ,,, echo %^cmdcmdline%

,

C:\Windows\system32\cmd.exe  /S /D /c" if not defined,NEQ echo %cmdcmdline%"

- , , ...

  • NEQ CASE. (do set neq=blah CASE)

  • CASE 2 == ===

  • CASE 2, CASE 3 NEQ , NEQ . CASE 1 2 - == NEQ, NEQ , .

, . . ?

CASE 4

cmd, ,

echo pipe|if not defined^ == echo %^cmdcmdline%

if not "defined" == "echo" recursive command

+2

, if, , ( ):

(if^ comparison_expression conditional_command_line)
(if^ comparison_expression (conditional_command_line) else other_conditional_command_line)

, not, exist defined, else , (==, EQU, NEQ ..), cmd .
if, for ( ).

+2

All Articles