为什么在管道块命令中进行“IF"比较时会出现错误消息? [英] Why does the error message appear when `IF` comparison is in the piped block command?
本文介绍了为什么在管道块命令中进行“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?
更简单,
这有效,(if 3 == 3 echo yes)|sort
这不起作用,(if 3 NEQ 2 echo yes)|sort
我的部分代码.
@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
推荐答案
试试这个:
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 这里和这里.尽管您在管道之前遇到了问题,但它是相同的错误,因为 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屋!
查看全文