为什么在管道块命令中进行“IF”比较时会出现错误消息? [英] Why does the error message appear when `IF` comparison is in the piped block command?

查看:292
本文介绍了为什么在管道块命令中进行“IF”比较时会出现错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我简化了代码。
以下三个是工作。

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

但下一个不起作用,

(for /L %a in (1,1,9) do @(if %a NEQ 0 (echo %a)))|sort /R

管道阻止命令中 NEQ 的问题是什么?

What's the problem of NEQ in the piped block command?

更简化,

这有效,(如果3 == 3回显是)| sort

这不起作用,(如果3 NEQ 2 echo yes)| sort

我的部分代码。

@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


推荐答案

试试这个:

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

神秘由jeb 这里此处。虽然你在管道之前遇到了这个问题,但它是同一个bug,因为cmd在管道的每一边创建了两个线程。

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.

以下是for循环的制作方法工作:

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

这篇关于为什么在管道块命令中进行“IF”比较时会出现错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆