Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

batch file - Why does the error message appear when `IF` comparison is in the piped block command?

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=.....inUnxUtils
(
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/ |/
/g";"s/ //g";"s/~//g"
  ) else (
    echo none
  )
)
)|"%Unx_path%gclip.exe"
exit /b
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this:

set "myline=if 3 NEQ 2 echo yes"

( %myLin^e%)|sort

or from batch file (double expansion works differently from batch file and the console):

set "myline=if 3 NEQ 2 echo yes"

( %%myLine%%)|sort

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...